Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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()...)

Solution 2:

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.

Post a Comment for "Unboundlocalerror In Django"