Я пытаюсь сделать действительно базовый c сайт в Django. Прямо сейчас вам просто нужно ввести свое имя, и оно будет сохранено. Но даже если я следовал инструкциям здесь , он выдает ошибку '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 / вотум / :
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>
Любая помощь будет принята с благодарностью.