Home > Software engineering >  AttributeError: 'WSGIRequest' object has no attribute 'get' I got this error ear
AttributeError: 'WSGIRequest' object has no attribute 'get' I got this error ear

Time:09-04

I am going through a django course and I got this error earlier but the tutor doesn't !!! what should I do ??? this is my code below

views.py

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
from .models import Project
from .forms import ProjectForm


def projects(request):
    objs = Project.objects.all()
    context = {"projects": objs}
    return render(request, 'projects/project.html', context)


def dynamic(request, text):
    obj = Project.objects.get(id=text)
    return render(request, 'projects/single-project.html', {'obj': obj})


def createproject(request):
    form = ProjectForm()
    context = {'form': form}
    return render(request, 'projectform.html', context)

models.py

from django.db import models
import uuid

class Project(models.Model):
    title = models.CharField(max_length=200)
    description = models.TextField(null=True, blank=True)
    demo_link = models.CharField(null=True, blank=True, max_length=2000)
    source_link = models.CharField(null=True, blank=True, max_length=2000)
    created = models.DateTimeField(auto_now_add=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
    tag = models.ManyToManyField('Tag', blank=True)
    total_vote = models.IntegerField(default=0, null=True, blank=True)
    vote_ratio = models.IntegerField(default=0, null=True, blank=True)

    def __str__(self):
        return self.title


class Review(models.Model):
    VOTE = (
        ('up', 'Up Vote'),
        ('down', 'Down Vote'),
    )
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    body = models.TextField(null=True, blank=True)
    value = models.CharField(max_length=200, choices=VOTE)
    created = models.DateTimeField(auto_now_add=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

    def __str__(self):
        return self.value


class Tag(models.Model):
    name = models.CharField(max_length=200)
    created = models.DateTimeField(auto_now_add=True)
    id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)

    def __str__(self):
        return self.name

forms.py

from django.forms import ModelForm
from .models import Project


class ProjectForm(ModelForm):
    class Meta:
        model = Project
        fields = '__all__'

urls.py (in the project folder I created another urls python file to make coding easier and this is the file that I've made)

from django.urls import path
from . import views

urlpatterns = [
    path('', views.projects),
    path('projects/<str:text>', views.dynamic, name='project'),
    path('create-project/', views.ProjectForm, name='create-project')
]

projectform.html

{% block content %}
<h2>Project Form</h2>
<form method="POST">
    {% csrf_token %}
    {{form}}
    <input type="submit">
</form>
{% endblock %}

Traceback

[03/Sep/2022 12:56:31] "GET /create-project/ HTTP/1.1" 500 98773
F:\tuts\Django\Exercises\Course\first_project\devsearch\projects\views.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
September 03, 2022 - 12:57:29
Django version 4.1, using settings 'devsearch.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /create-project/
Traceback (most recent call last):
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\core\handlers\base.py", line 220, in _get_response
    response = response.render()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\utils.py", line 67, in render
    context = context or self.get_context()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 326, in get_context
    top_errors = self.non_field_errors().copy()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 362, in non_field_errors
    return self.errors.get(
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 200, in errors
    self.full_clean()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 437, in full_clean
    self._clean_fields()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 444, in _clean_fields
    value = bf.initial if field.disabled else bf.data
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\boundfield.py", line 127, in data
    return self.form._widget_data_value(self.field.widget, self.html_name)
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 224, in _widget_data_value
    return widget.value_from_datadict(self.data, self.files, html_name)
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\widgets.py", line 297, in value_from_datadict
    return data.get(name)
AttributeError: 'WSGIRequest' object has no attribute 'get'
[03/Sep/2022 12:57:29] "GET /create-project/ HTTP/1.1" 500 98776
Internal Server Error: /create-project/
Traceback (most recent call last):
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\core\handlers\base.py", line 220, in _get_response
    response = response.render()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\utils.py", line 67, in render
    context = context or self.get_context()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 326, in get_context
    top_errors = self.non_field_errors().copy()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 362, in non_field_errors
    return self.errors.get(
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 200, in errors
    self.full_clean()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 437, in full_clean
    self._clean_fields()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 444, in _clean_fields
    value = bf.initial if field.disabled else bf.data
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\boundfield.py", line 127, in data
    return self.form._widget_data_value(self.field.widget, self.html_name)
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 224, in _widget_data_value
    return widget.value_from_datadict(self.data, self.files, html_name)
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\widgets.py", line 297, in value_from_datadict
    return data.get(name)
AttributeError: 'WSGIRequest' object has no attribute 'get'
[03/Sep/2022 12:57:31] "GET /create-project/ HTTP/1.1" 500 98776
Internal Server Error: /create-project/
Traceback (most recent call last):
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\core\handlers\base.py", line 220, in _get_response
    response = response.render()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\utils.py", line 67, in render
    context = context or self.get_context()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 326, in get_context
    top_errors = self.non_field_errors().copy()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 362, in non_field_errors
    return self.errors.get(
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 200, in errors
    self.full_clean()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 437, in full_clean
    self._clean_fields()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 444, in _clean_fields
    value = bf.initial if field.disabled else bf.data
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\boundfield.py", line 127, in data
    return self.form._widget_data_value(self.field.widget, self.html_name)
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 224, in _widget_data_value
    return widget.value_from_datadict(self.data, self.files, html_name)
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\widgets.py", line 297, in value_from_datadict
    return data.get(name)
AttributeError: 'WSGIRequest' object has no attribute 'get'
[03/Sep/2022 12:57:37] "GET /create-project/ HTTP/1.1" 500 98639
F:\tuts\Django\Exercises\Course\first_project\devsearch\projects\models.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
September 03, 2022 - 13:13:21
Django version 4.1, using settings 'devsearch.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /create-project/
Traceback (most recent call last):
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\core\handlers\base.py", line 220, in _get_response
    response = response.render()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\utils.py", line 67, in render
    context = context or self.get_context()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 326, in get_context
    top_errors = self.non_field_errors().copy()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 362, in non_field_errors
    return self.errors.get(
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 200, in errors
    self.full_clean()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 437, in full_clean
    self._clean_fields()
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 444, in _clean_fields
    value = bf.initial if field.disabled else bf.data
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\boundfield.py", line 127, in data
    return self.form._widget_data_value(self.field.widget, self.html_name)
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\forms.py", line 224, in _widget_data_value
    return widget.value_from_datadict(self.data, self.files, html_name)
  File "F:\tuts\Django\Exercises\Course\first_project\devsearch\env\lib\site-packages\django\forms\widgets.py", line 297, in value_from_datadict
    return data.get(name)
AttributeError: 'WSGIRequest' object has no attribute 'get'
[03/Sep/2022 13:13:22] "GET /create-project/ HTTP/1.1" 500 98776

CodePudding user response:

I think the issue might be your urls.py you have path('create-project/', views.ProjectForm, name='create-project') but I think what you need is path('create-project/', views.createproject, name='create-project').

  • Related