Issue with Django-forms: 'WSGIRequest' object has no attribute 'get'

混江龙づ霸主 提交于 2021-01-27 14:18:56

问题


I am getting this error in the Django debug-view:

'WSGIRequest' object has no attribute 'get'

This is for a login-script, which most of is copied from the admin-code, mostly for practice/tweaking-reasons. The code I have, in views.py, is following:

@sensitive_post_parameters()
@csrf_protect
@never_cache
def login(request, template_name="main/login.html",
          authentication_form=LoginForm,
          redirect_field_name=REDIRECT_FIELD_NAME,
          current_app=None, extra_context=None):
    """
    Displays the login form and handles the login action.
    """
    redirect_to = request.REQUEST.get(redirect_field_name, '')

    if request.method == "POST":
        form = authentication_form(request, data=request.POST)
        if form.is_valid():

            # Ensure the user-originating redirection url is safe.
            if not is_safe_url(url=redirect_to, host=request.get_host()):
                redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)

            # Okay, security check complete. Log the user in.
            user = get_user(request.POST.get('email'))
            auth_login(request, user)

            return HttpResponseRedirect(redirect_to)
    else:
        form = authentication_form(request)
    current_site = get_current_site(request)

    context = {
        'form': form,
        redirect_field_name: redirect_to,
        'site': current_site,
        'site_name': current_site.name,
    }
    if extra_context is not None:
        context.update(extra_context)
    return TemplateResponse(request, template_name, context,
                            current_app=current_app)

Here is my form:

class LoginForm(forms.Form):
    email = forms.CharField(max_length=100, label="email", widget=forms.TextInput(attrs={'class': 'form-control'}), required=True)
    password = forms.CharField(max_length=32, label="password", widget=forms.PasswordInput(attrs={'class': 'form-control'}), required=True)

And I am receiving my error on line 13 in login.html

{% extends 'base_main.html' %}
{% load staticfiles %}

{% block title %}Login{% endblock %}
{% block stylesheet %}
    {{ block.super }}
    <link rel="stylesheet" type="text/css" href="{% static 'main/css/login.css' %}">
{% endblock %}

{% block body %}
    <div class="container">
        {% if form %}
            <!--
            <form class="form-signin" role="form" method="POST" action="/check_login/">{% csrf_token %}-->
                <h2 class="form-signin-heading">Please sign in</h2>
                {{ form }} <!-- Line 13 -->
                <!--<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
            </form>
            -->
        {% endif %}
    </div>
{% endblock %}

Anyone got any ideas to how I could solve this?

Update

Traceback:
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  139.                 response = response.render()
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  82.         content = template.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  305.                 return nodelist.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/template/debug.py" in render
  91.             output = force_text(output)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/utils/encoding.py" in force_text
  100.                 s = s.__unicode__()
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in __str__
  103.         return self.as_table()
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in as_table
  223.             errors_on_separate_row = False)
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in _html_output
  148.         top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in non_field_errors
  249.         return self.errors.get(NON_FIELD_ERRORS, self.error_class())
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in errors
  121.             self.full_clean()
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
  273.         self._clean_fields()
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/forms.py" in _clean_fields
  282.             value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File "/home/ryuu/Programming/Python/exobuild/venv/local/lib/python2.7/site-packages/django/forms/widgets.py" in value_from_datadict
  207.         return data.get(name, None)

Exception Type: AttributeError at /login/
Exception Value: 'WSGIRequest' object has no attribute 'get'

回答1:


The problem is in these lines:

form = authentication_form(request, data=request.POST)
...
form = authentication_form(request)

Your LoginForm class' init method does not take a request object like the AuthenticationForm from django.contrib.auth.forms does.

So you could just do:

form = authentication_form(data=request.POST)
...
form = authentication_form()

...but probably in fact your current LoginForm class does not do enough and you should look at the Django one (particularly the stuff around checking session cookie etc), possibly sub-class it.



来源:https://stackoverflow.com/questions/21728766/issue-with-django-forms-wsgirequest-object-has-no-attribute-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!