Django:如何在用户使用默认的 UserCreationForm 注册后立即登录?
Posted
技术标签:
【中文标题】Django:如何在用户使用默认的 UserCreationForm 注册后立即登录?【英文标题】:Django: How to log user in right after they had registered with somewhat default UserCreationForm? 【发布时间】:2022-01-18 12:26:07 【问题描述】:我正在尝试在用户注册应用后立即登录,这样他们就不必立即手动登录。我已经从 django 的 UserCreationForm 创建了 RegisterForm 并且它可以正常工作 - 如果表单有效,它会创建一个 User 对象,但我不知道如何访问创建的 User 对象以使用 login 函数记录它,这需要,除了请求之外,还有一个用户对象。注意:我在 models.py 中编辑了默认用户。这是我的代码:
class RegisterForm(UserCreationForm):
def __init__(self, *args, **kwargs):
super().__init__(*args,**kwargs)
self.fields['username'].widget.attrs.update('class':'form-control','id':'username', 'aria-describedby':'usernameHelp')
self.fields['password1'].widget.attrs.update('class':'form-control','id':'password1', 'aria-describedby':'password1Help')
self.fields['password2'].widget.attrs.update('class':'form-control','id':'password2','aria-describedby':'password2Help')
class Meta:
model = User
fields = ['username', 'password1', 'password2', 'email', 'first_name', 'last_name',
'photo', 'amazon', 'twitter', 'facebook', 'instagram', 'youtube']
widgets =
'email':EmailInput(attrs='class':'form-control', 'id':'email', 'aria-describedby':'emailHelp'),
'first_name':TextInput(attrs='class':'form-control', 'id':'first_name',),
'last_name':TextInput(attrs='class':'form-control','id':'last_name', 'aria-describedby':'nameHelp'),
'photo':ClearableFileInput(attrs='class':'form-control','id':'photo', 'aria-describedby':'photoHelp'),
'amazon':URLInput(attrs='class':'form-control', 'id':'amazon', 'aria-describedby':'amazonHelp'),
'twitter':URLInput(attrs='class':'form-control', 'id':'twitter', 'aria-describedby':'twitterHelp'),
'facebook':URLInput(attrs='class':'form-control', 'id':'facebook', 'aria-describedby':'facebookHelp'),
'instagram':URLInput(attrs='class':'form-control', 'id':'instagram', 'aria-describedby':'instagramHelp'),
'youtube':URLInput(attrs='class':'form-control', 'id':'youtube', 'aria-describedby':'youtubeHelp')
这是视图:
def register(request):
# POST
if request.method == "POST":
# In addition to our form we must make sure to get the files too, if photo is uploaded
form = RegisterForm(request.POST, request.FILES or None)
if form.is_valid():
form.save()
#user = User.objects.get() ???
#login(request, user)
return HttpResponseRedirect(reverse('index'))
else:
return render(request, "astator/register.html",
"form":form,
"message":"Something went wrong. Please try filling out the fields again. Make sure that your passwords match and that they satisfy the requirements listed bellow."
)
# GET
else:
form = RegisterForm()
return render(request, "astator/register.html", "form":form)
【问题讨论】:
【参考方案1】:在您的 form 中,您可以访问您的 user 对象,这样您就可以执行类似的操作:
if form.is_valid():
user = form.save()
login(request, user)
return HttpResponseRedirect(reverse('index'))
这是有关保存方法的文档中的说明:Django Save Method
【讨论】:
【参考方案2】:与一般的表单建模类似,UserCreationForm 在您保存表单时返回用户模型实例。
if form.is_valid():
user = form.save()
login(request, user)
return HttpResponseRedirect(reverse('index'))
【讨论】:
【参考方案3】:您可以创建一个 html 页面,然后从该 url 调用这个 classView,您可以使用 django 身份验证更方便
class LoginForm(AuthenticationForm):
username = UsernameField(widget=forms.TextInput(
attrs='autofocus': True, 'class': 'form-control'))
password = forms.CharField(label=_("Password"), strip=False, widget=forms.PasswordInput(
attrs='autocomplete': 'current-password', 'class': 'form-control'))
【讨论】:
不!这个没用,作者用的是FBV以上是关于Django:如何在用户使用默认的 UserCreationForm 注册后立即登录?的主要内容,如果未能解决你的问题,请参考以下文章