美好的一天!
经常运行views.py时,会出现这种错误:
TypeError:FirstForm()缺少1个必需的位置参数:“ request”
Views.py
def FirstForm(request):
if request.method == 'GET':
form = FirstForm()
else:
form = FirstForm(request.POST)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
date = form.cleaned_data['date']
number = form.cleaned_data['number']
try:
send_mail(email, (name, date, number), settings.EMAIL_HOST_USER, ['daribian@list.ru'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('success')
return render(request, 'index.html', {'form': form})
def successView(request):
return HttpResponse('Success!')
Сn你告诉我问题出在哪里吗?
答案 0 :(得分:0)
您的视图与表单具有相同的名称,因此,如果您调用FirstForm
,则将调用该视图,而不是表单。
因此最好将视图命名为不同的名称,例如first_view
:
def first_view(request):
if request.method == 'GET':
form = FirstForm()
else:
form = FirstForm(request.POST, request.FILES)
if form.is_valid():
name = form.cleaned_data['name']
email = form.cleaned_data['email']
date = form.cleaned_data['date']
number = form.cleaned_data['number']
try:
send_mail(email, (name, date, number), settings.EMAIL_HOST_USER, ['daribian@list.ru'])
except BadHeaderError:
return HttpResponse('Invalid header found.')
return redirect('success')
return render(request, 'index.html', {'form': form})
注意:根据PEP-8,您应该使用 snake_case 而不是 camelCase 编写函数,因此{{1} },而不是
。success_view