NameError:未定义全局名称“StudentForm”

时间:2012-01-08 20:32:32

标签: python django-views

这是我的view.py文件的代码:

    from django import forms

   def student(request):
        id = Student.objects.aggregate(Max('job_no'))
        maxid =id['job_no__max']
        if maxid== None :
              maxid = 1
        else:
              maxid = maxid + 1

       if request.method == 'POST':
             form = StudentForm(request.POST)
             if form.is_valid():
                   cd = form.cleaned_data
                   firstname = cd['firstname']
                   lastname = cd['lastname']
                   father_name = cd['father_name']
                   mother_name = cd['mother_name']
                   gender = cd['gender']
                   course = cd['course']
                   category = cd['category']
                   type = cd['type']
                   branch = cd['branch']
                   class_roll_no = cd['class_roll_no']
                   date = cd['date']
                   address = cd['address']
                   city = cd['city']
                   pin_code = cd['pin_code']
                   state = cd['state']
                   email = cd['email']
                   contact_no =cd['contact_no']
                   form.save()
                   p = Auto_number(job_no = maxid)
                   template= {'form': form,'firstname':firstname,'lastname':lastname,'father_name':father_name,'maxid':maxid,'date':date,'gender':gender,'course':course,'category':category,'type':type,'branch':branch,'class_roll_no':class_roll_no,'address':address,'city':city,'pin_code':pin_code,'state':state,'email':email,'contact_no':contact_no,}
        return render_to_response('structure/fee_ok.html', template, context_instance=RequestContext(request))
   else:
           form = StudentForm()           #<-- error in this line
   return render_to_response('structure/info.html', {'form':form,'maxid':maxid},context_instance=RequestContext(request))

在这种情况下,它显示“NameError:全局名称'StudentForm'未定义”,但我不知道错误在哪里。

提前谢谢。

1 个答案:

答案 0 :(得分:6)

错误在于您没有导入名称,可能来自应用程序的forms.py。

from someapp.forms import StudentForm