Sid
Sid

Reputation: 2189

Django - 'WSGIRequest' object has no attribute 'get'

I'm trying to make a really basic site in Django. Right now you just have to enter your name and it gets saved. But even though I followed the instructions here it throws the error 'WSGIRequest' object has no attribute 'get'

My 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),
]

And the template name.html:

<form action="/data/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="Submit">
</form>

But when I open 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>

Any help would be greatly appreciated.

Upvotes: 3

Views: 4810

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476614

The problem is in your vote_page method. You can not instantiate a form with request as data. It expects a dictionary-like object like a QueryDict, for example with request.POST or request.GET, so NameForm(request) will not work.

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})

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.

Upvotes: 2

Related Questions