zogo
zogo

Reputation: 515

UnboundLocalError in django

i am relatively new in django .i have problem with django..

The error is UnboundLocalError at /app/rest/submitreq_val/ local variable 'sub_req' referenced before assignment

i want to validate my form.

this is my view...

def submitreq(request):

if request.method == "POST":
    sub_req = SubreqForm(request.POST)

if sub_req.is_valid():
    success = True
    request = sub_req.cleaned_data['request']
    category = sub_req.cleaned_data['category']
    sub_category = sub_req.cleanded_data['sub_category']
else:
    sub_req = subreqForm()
    ctx = {'sub_req': sub_req}
    return render_to_response("rest/submit_req.html", ctx,context_instance=RequestContext(request))

this is my template

<form action = "." class="bs-example form-horizontal" method = "post">
                    {{sub_req.as_p}}
                    <div class="form-group">
                        <label class="col-lg-3 control-label" for="inputText">Request</label>
                        <div class="col-lg-8">
                            <input id="inputText" class="form-control" type="Text" placeholder="Request Name" ng-model="request.name"></input>
                        </div>
                    <br />
                    <br />
                    </div>
                    <div class="form-group">
                        <label for="id_category" class="col-lg-3 control-label">Category</label>
                        <div class="col-lg-8">
                            <select class="form-control" id="id_category" ng-model="selectedCategory" ng-options="cat.pk as cat.name for cat in category">
                                <option value="">Select Category</option>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="id_subcategory" class="col-lg-3 control-label">Sub-Category</label>
                        <div class="col-lg-8">
                            <select class="form-control" id="id_subcategory" ng-model="selectedSubCategory" ng-options="subcat.id as subcat.name for subcat in subcategory">
                            <option value="">Select SubCategory</option>
                            </select>

                        </div>
                    </div>
                    <div>
                        <center>
                            <button type="submit" class="btn btn-primary" ng-click="addRequest()">Add Request</button>
                        </center>
                    </div>
                </form>

this is my forms.py

from django import forms
from django.core.exceptions import ValidationError

from request.models import Request

class SubreqForm(forms.Form):
request = forms.CharField(max_length=100)
category = forms.CharField()
sub_category = forms.CharField()

and this is my model

from django.db import models
from base.models import BaseModel
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.contrib.contenttypes import generic
from user_profile.models import Profile
import datetime
from photo.models import *



class Request(BaseModel):
  description = models.TextField(blank=True,null=True)
  category = models.ForeignKey(Category)
  sub_category = models.ForeignKey(SubCategory)


from django.db.models.signals import post_save
from .handlers import *

post_save.connect(
 request_handler,
 sender=Request,
 dispatch_uid='request.handlers.request_handler')

now why this error is occuring?? if you have any answer then please help me.

Upvotes: 0

Views: 11465

Answers (2)

Mike DeSimone
Mike DeSimone

Reputation: 42805

Pretty sure it's supposed to look like this:

def submitreq(request):

    if request.method == "POST":
        sub_req = SubreqForm(request.POST)

        if sub_req.is_valid():
            # Submitted form is valid, so do what it says.
            success = True
            # The following line is blatantly wrong.
            # Don't replace the request object with something submitted by form.
            request = sub_req.cleaned_data['request']
            category = sub_req.cleaned_data['category']
            sub_category = sub_req.cleanded_data['sub_category']
    else:
        # Not a POST (probably a GET) so make a blank form.
        sub_req = subreqForm()

    # Set up the context and render the response.
    ctx = {'sub_req': sub_req}
    return render_to_response("rest/submit_req.html",
        ctx,context_instance=RequestContext(request))

I think the OP messed up the indentation in their source, not just the question.

Upvotes: 0

shx2
shx2

Reputation: 64308

It is hard to say because the indentation in your question needs to be fixed, but I'm going to guess this is the problem:

if request.method == "POST":
    sub_req = SubreqForm(request.POST)

if sub_req.is_valid():

In case request.method != "POST", you don't assign to sub_req, but you still reference it (in if sub_req.is_valid()...)

Upvotes: 2

Related Questions