我正在开发基于Django框架的网站。我选择了django-allauth作为我的后端,并自定义了它提供的标准登录表单:
的myapp / forms.py:
from allauth.account.forms import LoginForm, SignupForm
from django import forms
class MyLoginForm(LoginForm):
def __init__(self, *args,
**kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
self.fields['remember'].label = 'Remember me'
self.fields['remember'].initial = True
# To use a placeholder text we'll have to use below
login_widget = forms.TextInput(attrs={'type':'email',
'placeholder':
('Enter your email'),
'autofocus':'autofocus'
})
self.fields['email'] = forms.CharField(label=("Email"),
widget=login_widget)
self.fields['email'].label = 'Email'
def clean_email(self):
email = self.cleaned_data.get('email')
base, provider = email.split('@')
domain, extent = provider.split('.')
if extent == "edu":
forms.ValidationError("Please use a valid \
personal email address")
return email
class MySignUpForm(SignupForm):
def __init__(self, *args, **kwargs):
super(SignupForm, self).__init__(*args, **kwargs)
这是我处理表单呈现的视图: another_app / views.py:
from django.shortcuts import render
from userlogin.forms import MyLoginForm, MySignUpForm
def index(request):
login = MyLoginForm()
signup = MySignUpForm()
if request.method == "POST":
login = MyLoginForm(request.POST or None)
signup = MySignUpForm(request.POST or None)
if login.is_valid():
lg_instance = login.save(commit=False)
print (lg_instance)
context = {
'login': login,
'signup': signup,
'login_url': '/profile/'
}
return render(
request,
'homepage.html',
context,
)
鉴于以上信息,我提出了以下问题:
点击提交后,网站会在/显示KeyError。可能是什么原因造成的?回溯显示在' LoginForm' class(在allauth / accounts / forms.py中)有一个名为Login的字段导致冲突。
如何实际登录用户并转到个人资料视图。
答案 0 :(得分:0)
问题在于MyLoginForm
初始化。从那里删除所有与电子邮件相关的代码。在settings.py中使用ACCOUNT_AUTHENTICATION_METHOD = 'email'
,然后使用__init__
方法仅覆盖占位符。检查here django all auth中是否已支持该电子邮件。