DjangoUnboundLocalError:赋值之前引用了局部变量“ forms”

时间:2019-01-21 14:01:04

标签: python django

我正在尝试使表单从用户那里获取有关反馈的信息。这是我的代码:

我是Django的新手,请您能帮我吗?

下面是我的forms.py文件:-

from django import forms

class FeedBackForm(forms.Form):
    name=forms.CharField()
    rollno=forms.IntegerField()
    email=forms.EmailField()
    feedback=forms.CharField(widget=forms.Textarea)

以下是我的views.py文件:-

from django.shortcuts import render
from . import forms
# Create your views here.
def feedback_view(request):
    form=forms.FeedBackForm()
    if request.method=='POST':
        forms=forms.FeedBackForm(request.post)
        if form.is_valid():
            print('Form Validation Success and printing Feedback info')
            print('Student Name:',form.cleaned_data['name'])
            print('Student Roll No:',form.cleaned_data['rollno'])
            print('Student Mail ID:',form.cleaned_data['email'])
            print('Student Feedback:',form.cleaned_data['feedback'])

    return render(request,'testapp/feedback.html',{'form':form})

它抛出错误以下

UnboundLocalError at /feedback/
local variable 'forms' referenced before assignment
Request Method: GET
Request URL:    http://127.0.0.1:8000/feedback/
Django Version: 2.1.4
Exception Type: UnboundLocalError
Exception Value:    
local variable 'forms' referenced before assignment
Exception Location: F:\Django\feedbackproject\testapp\views.py in feedback_view, line 5
Python Executable:  C:\Users\Prashant\AppData\Local\Programs\Python\Python37-32\python.exe
Python Version: 3.7.0
Python Path:    
['F:\\Django\\feedbackproject',
 'C:\\Users\\Prashant\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
 'C:\\Users\\Prashant\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
 'C:\\Users\\Prashant\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
 'C:\\Users\\Prashant\\AppData\\Local\\Programs\\Python\\Python37-32',
 'C:\\Users\\Prashant\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']
Server time:    Mon, 21 Jan 2019 13:54:35 +0000

0 个答案:

没有答案