' WSGIRequest'对象没有属性' get'覆盖AUTHENTICATIONFORM时出错

时间:2016-07-20 13:23:45

标签: python django django-forms

我是Django和Python的新手。我对默认AuthenticationForm有一些问题,所以我遵循了这个问题:How do I extend the Django "login" form?。现在,我收到一些错误,上面写着: --'WSGIRequest' object has no attribute 'get'--

这是我的forms.py

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import AuthenticationForm
from .models import Profile


class LoginForm(AuthenticationForm):
    error_messages = {
        'invalid_login': ("Please enter a correct %(username)s and password. "
                          "Note that both fields may be case-sensitive."),
        'inactive': ("This account is inactive."),
    }


class UserEditForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email')

和urls.py:

from .forms import LoginForm

urlpatterns = [
    # url(r'^login/$', views.user_login, name='login'),
    url(r'^login/$', auth_views.login, {'authentication_form': LoginForm}, name='login'),
    url(r'^logout/$', auth_views.logout, name='logout'),
    url(r'^logout-then-login/$', auth_views.logout_then_login, name='logout_then_login'),
    url(r'^$', views.dashboard, name='dashboard'),

编辑:

Traceback:

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in get_response
  174.                     response = self.process_exception_by_middleware(e, request)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py" in get_response
  172.                     response = response.render()

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\response.py" in render
  160.             self.content = self.rendered_content

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\response.py" in rendered_content
  137.         content = template.render(context, self._request)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\backends\django.py" in render
  95.             return self.template.render(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render
  206.                     return self._render(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\loader_tags.py" in render
  173.         return compiled_parent._render(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in _render
  197.         return self.nodelist.render(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\loader_tags.py" in render
  69.                 result = block.nodelist.render(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render
  992.                 bit = node.render_annotated(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render_annotated
  959.             return self.render(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in render
  1043.             output = self.filter_expression.resolve(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in resolve
  709.                 obj = self.var.resolve(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in resolve
  850.             value = self._resolve_lookup(context)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\template\base.py" in _resolve_lookup
  913.                             current = current()

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\forms\forms.py" in as_p
  281.             errors_on_separate_row=True)

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\forms\forms.py" in _html_output
  180.         top_errors = self.non_field_errors()  # Errors that should be displayed above all fields.

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\forms\forms.py" in non_field_errors
  289.         return self.errors.get(NON_FIELD_ERRORS, self.error_class(error_class='nonfield'))

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\forms\forms.py" in errors
  153.             self.full_clean()

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\forms\forms.py" in full_clean
  362.         self._clean_fields()

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\forms\forms.py" in _clean_fields
  374.                 value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))

File "C:\Users\Jorj\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\forms\widgets.py" in value_from_datadict
  231.         return data.get(name)

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

这是view.py:

from django.shortcuts import render
from .forms import UserRegistrationForm, UserEditForm, ProfileEditeForm
from django.contrib.auth.decorators import login_required
from .models import Profile
from django.contrib import messages


@login_required
def dashboard(request):
    return render(request, 'account/dashboard.html', {'section': 'dashboard'})


@login_required
def edit(request):
    if request.method == 'POST':
        user_form = UserEditForm(instance=request.user, data=request.POST)
        profile_form = ProfileEditeForm(instance=request.user.profile,
                                        data=request.POST,
                                        files=request.FILES)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(request, 'Profile updated successfully')
        else:
            messages.error(request, 'Error updating your profile')
    else:
        user_form = UserEditForm(instance=request.user)
        profile_form = ProfileEditeForm(instance=request.user.profile)
    return render(request, 'account/edit.html', {'user_form': user_form, 'profile_form': profile_form})


def register(request):
    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)
        if user_form.is_valid():
            new_user = user_form.save(commit=False)
            new_user.set_password(user_form.cleaned_data['password'])
            new_user.save()
            pofile = Profile.objects.create(user=new_user)
            return render(request, 'account/register_done.html', {'new_user': new_user})

    else:
        user_form = UserRegistrationForm()
    return render(request, 'account/register.html', {'user_form': user_form})

我是否忘记了扩展超级课程的一些事情?

1 个答案:

答案 0 :(得分:0)

我解决了,我将表单名称更改为LoginFormi,错误消失了。也许django的错误