gpt4 book ai didi

python - Django - 'WSGIRequest' 对象没有属性 'get'

转载 作者:行者123 更新时间:2023-12-05 06:17:36 31 4
gpt4 key购买 nike

我正在尝试用 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>

如有任何帮助,我们将不胜感激。

最佳答案

问题出在你的vote_page方法。您不能使用 request 实例化表单作为数据。它需要一个类似字典的对象,如 QueryDict ,例如 request.POSTrequest.GET , 所以 NameForm(<s>request</s>)将不起作用。

def vote_page(request):
if request.method == 'POST':
form = <b>NameForm(request.POST)</b>
if form.is_valid():
# …
return redirect(<i>'name-of-some-view'</i>)
else:
form = NameForm()
return render(request, 'name.html', {'form': <b>form</b>})

Note: In case of a successful POST request, you should make a redirect [Django-doc] to implement the Post/Redirect/Get pattern [wiki]. This avoids that you make the same POST request when the user refreshes the browser.

关于python - Django - 'WSGIRequest' 对象没有属性 'get',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61556248/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com