title: UnboundLocalError in django
body: <p>i am relatively new in django .i have problem with django..</p>
  
  <p>The error is <strong>UnboundLocalError at /app/rest/submitreq_val/
  local variable 'sub_req' referenced before assignment</strong></p>
  
  <p>i want to validate my form.</p>
  
  <p>this is my view...</p>
  
  <pre><code>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))
  </code></pre>
  
  <p>this is my template</p>
  
  <pre><code>&lt;form action = "." class="bs-example form-horizontal" method = "post"&gt;
                      {{sub_req.as_p}}
                      &lt;div class="form-group"&gt;
                          &lt;label class="col-lg-3 control-label" for="inputText"&gt;Request&lt;/label&gt;
                          &lt;div class="col-lg-8"&gt;
                              &lt;input id="inputText" class="form-control" type="Text" placeholder="Request Name" ng-model="request.name"&gt;&lt;/input&gt;
                          &lt;/div&gt;
                      &lt;br /&gt;
                      &lt;br /&gt;
                      &lt;/div&gt;
                      &lt;div class="form-group"&gt;
                          &lt;label for="id_category" class="col-lg-3 control-label"&gt;Category&lt;/label&gt;
                          &lt;div class="col-lg-8"&gt;
                              &lt;select class="form-control" id="id_category" ng-model="selectedCategory" ng-options="cat.pk as cat.name for cat in category"&gt;
                                  &lt;option value=""&gt;Select Category&lt;/option&gt;
                              &lt;/select&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          &lt;label for="id_subcategory" class="col-lg-3 control-label"&gt;Sub-Category&lt;/label&gt;
                          &lt;div class="col-lg-8"&gt;
                              &lt;select class="form-control" id="id_subcategory" ng-model="selectedSubCategory" ng-options="subcat.id as subcat.name for subcat in subcategory"&gt;
                              &lt;option value=""&gt;Select SubCategory&lt;/option&gt;
                              &lt;/select&gt;
  
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div&gt;
                          &lt;center&gt;
                              &lt;button type="submit" class="btn btn-primary" ng-click="addRequest()"&gt;Add Request&lt;/button&gt;
                          &lt;/center&gt;
                      &lt;/div&gt;
                  &lt;/form&gt;
  </code></pre>
  
  <p>this is my forms.py</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>and this is my model</p>
  
  <pre><code>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')
  </code></pre>
  
  <p>now why this error is occuring?? if you have any answer then please help me.</p>