首页 > 解决方案 > django login get AuthenticationForm 不包含 get

问题描述

我正在尝试构建一个简单的登录表单,但我总是得到'AuthenticationForm' object has no attribute 'get'。我是使用 django 框架的新手,但我不确定如何解决问题......任何提示而不改变太多逻辑?

view.py

from django.shortcuts import render, redirect
from django.views.generic import TemplateView
from django.contrib.auth import authenticate
from django.contrib.auth.forms import AuthenticationForm
from django.http import HttpResponse

class loginPage(TemplateView):
    template_name = 'login.html'
    def post(self, request):
        form = AuthenticationForm(request, data=request.POST)
        if form.is_valid():
            user = authenticate(
                request,
                username=form.get('email'),
                password=form.get('password')
            )

            if user is not None:
                return HttpResponse("Testing")
            else:
                print(user)
        else:
            return HttpResponse("loginPage") ## always goes here

登录html

<form method="post">
{% csrf_token %}
<input class="form-control" name="email" placeholder="Email" /></div>
<input class="form-control" type="password" name="password" placeholder="Password" />
</form>```

标签: pythondjango

解决方案



if form.is_valid():
    username=form.cleaned_data['email']
    password=form.cleaned_data['password']
    user = authenticate(
                request,
                username=username,
                password=password            
    )

如果这个工作,试试这个


推荐阅读