0

これは本当に私を困惑させています。どんな助けでも大歓迎です。

UnboundLocalError at /addauthor
local variable 'form' referenced before assignment
Request Method: POST
Request URL:    http://127.0.0.1:8000/addauthor
Django Version: 1.4
Exception Type: UnboundLocalError
Exception Value:    
local variable 'form' referenced before assignment
Exception Location: C:\Users\Nir\desktop\club\blog\views.py in addauthorView, line 8
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.6

これが私のviews.pyです:

from django.shortcuts import render
from blog.forms import ContactForm
from django.http import HttpResponseRedirect

def addauthorView(request):
     if request.method == 'POST':
     f = ContactForm(request.POST)
     if form.is_valid():
          first_name = form.cleaned_data['firstname']
          last_name = form.cleaned_data['lastname']
          user_email = form.cleaned_data['email']
          c = Contact(firstname=first_name, lastname=last_name, email=user_email)
          c.save()
          return HttpResponseRedirect('thanks/')
     else:
          form = ContactForm(request.POST)
          return render(request, 'addauthor.html', {'form': ContactForm})
else:
    return render(request, 'addauthor.html', {'form': ContactForm})


def thanksView(request):
    return render(request, 'thanks.html')

リクエストに応じて django プロジェクトに他のものを投稿できますが、問題はビューから来ているようで、その理由はわかりません。

4

1 に答える 1

3

form変数を定義せずに使用しています。交換:

f = ContactForm(request.POST)

と:

form = ContactForm(request.POST)
于 2014-03-19T05:11:00.513 に答える