我正在尝试在Django中建立一个真正的基础站点。
现在,您只需要输入您的姓名即可保存。
但是,即使我按照here的说明进行操作
它会引发错误'WSGIRequest' object has no attribute 'get'
我的views.py
:
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.http import HttpResponse
from .forms import NameForm
from django.views.decorators.csrf import csrf_protect
@csrf_protect
def get_name(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = NameForm(request.POST)
# check whether it's valid:
if form.is_valid():
with open("name.txt" , "w") as f:
f.write(form.cleaned_data)
return HttpResponseRedirect('/nmindex/')
@csrf_protect
def vote_page(request):
return render(request, 'name.html', {'form': NameForm(request)})
forms.py
:
from django import forms
from django.views.decorators.csrf import csrf_protect
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
urls.py
:
from django.contrib import admin
from django.urls import path, include
from nmindex import views
urlpatterns = [
path('data/', views.get_name),
path('vote/', views.vote_page),
path('admin/', admin.site.urls),
]
还有模板name.html
:
<form action="/data/" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
但是当我打开http://localhost:8000/vote/时:
AttributeError at /vote/
'WSGIRequest' object has no attribute 'get'
Request Method: GET
Request URL: http://localhost:8000/vote/
Django Version: 3.0.5
Exception Type: AttributeError
Exception Value:
'WSGIRequest' object has no attribute 'get'
Exception Location: C:\Users\Sid\Envs\namesproj\lib\site-packages\django\forms\widgets.py in value_from_datadict, line 258
Python Executable: C:\Users\Sid\Envs\namesproj\Scripts\python.exe
Python Version: 3.8.2
Python Path:
['C:\\Users\\Sid\\names',
'C:\\Users\\Sid\\Envs\\namesproj\\Scripts\\python38.zip',
'c:\\users\\sid\\appdata\\local\\programs\\python\\python38-32\\DLLs',
'c:\\users\\sid\\appdata\\local\\programs\\python\\python38-32\\lib',
'c:\\users\\sid\\appdata\\local\\programs\\python\\python38-32',
'C:\\Users\\Sid\\Envs\\namesproj',
'C:\\Users\\Sid\\Envs\\namesproj\\lib\\site-packages']
Server time: Sat, 2 May 2020 08:00:03 +0000
Error during template rendering
In template C:\Users\Sid\names\templates\name.html, error at line 3
'WSGIRequest' object has no attribute 'get'
1 <form action="/data/" method="post">
2 {% csrf_token %}
3 {{ form }}
4 <input type="submit" value="Submit">
5 </form>
任何帮助将不胜感激。
答案 0 :(得分:1)
问题出在您的vote_page
方法中。您无法使用request
作为数据实例化表单。它期望像QueryDict
这样的字典式对象,例如带有request.POST
或request.GET
的对象,因此NameForm(
将不起作用。request)
def vote_page(request):
if request.method == 'POST':
form = NameForm(request.POST)
if form.is_valid():
# …
return redirect('name-of-some-view')
else:
form = NameForm()
return render(request, 'name.html', {'form': form})
注意:如果POST请求成功,则应进行
redirect
[Django-doc] 实现Post/Redirect/Get pattern [wiki]。 这样可以避免在用户刷新页面时发出相同的POST请求。 浏览器。