local variable 'routingForm' referenced before assignment

Jibin :

Am trying to do a form submit,but getting the error"local variable 'routingForm' referenced before assignment".please help me to solve this.

*****forms.py*****

   from django import forms
   class routingForm(forms.Form):
   areaDigit = forms.CharField(label='areaDigit', max_length=100)
   product = forms.CharField(label='product', max_length=100)

*****views.py*****

from django.shortcuts import render
from .forms import routingForm

# Create your views here.
from django.http import HttpResponse,HttpResponseRedirect
from .models import Product,Routing_Dest_Area
def get_route_list(request):
     #areaDigit= request.POST.get('areaDigit', False)
    #data=Routing_Dest_Area.objects.filter(areaDigit_pk=request.POST['areaDigit'])
    if request.method == "POST":
        #Get the posted form
        routingForm = routingForm(request.POST)

        if routingForm.is_valid():
            areaDigit = routingForm.cleaned_data['areaDigit']
    else:
        MyLoginForm = routingForm()

    return render(request, 'routing/test.html',{'areaDigit':areaDigit})

*****home.html*****

<form method="POST" action="{% url 'get_route_list'%}" id="routingForm" name="routingForm">
            {% csrf_token %}
                <div class="form-content">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <input type="text" class="form-control" placeholder="Area String *"  
                                 name"areaDigit" id="areaDigit" value="{{areaDigit}}"/>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                            <label for="sel1">Select list (select one):</label>
                                 <select class="form-control" id="Product" name="product">
                                   <option> 1</option>
                                   <option> 21</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <button type="submit" class="btnSubmit">Submit</button>

Form data value in the network tab of inspect element

Willem Van Onsem :

You can not write routingForm = routingForm(request.POST) since that makes routingForm a local variable, and it is used before it is assigned.

I however strongly suggest that you write the RoutingForm in PerlCase, so starting with an uppercase:

# app/forms.py

from django import forms

class RoutingForm(forms.Form):
    areaDigit = forms.CharField(label='areaDigit', max_length=100)
    product = forms.CharField(label='product', max_length=100)

Variables in the function itself, are usually written in snake_case. So then the view looks like:

# app/views.py

from django.shortcuts import render
from .forms import RoutingForm

from django.http import HttpResponse, HttpResponseRedirect
from .models import Product, Routing_Dest_Area


def get_route_list(request):
    areaDigit = None
    if request.method == 'POST':
        #Get the posted form
        routing_form = RoutingForm(request.POST)
        if routing_form.is_valid():
            areaDigit = routing_form.cleaned_data['areaDigit']
    else:
    return render(request, 'routing/test.html',{'areaDigit':areaDigit})

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=361546&siteId=1