预期构想是注册完成后完成登录,但是前端展示不出用户名,只有登录和注册分开写才可以展示出用户名;
username = user_infos.get("account","")
password = user_infos.get("password","")
if UserInfo.objects.filter(Q(username=username)|Q(email=username)).count()>0:
return JsonResponse({
"message": "账号已经存在",
"username": username
})
try:
transaction.set_autocommit(False)
user_profile = UserInfo()
user_profile.username = username
user_profile.email=username
user_profile.password=make_password(password)
user_profile.save()
transaction.commit()
import time
user = auth.authenticate(username=username, password=password)
except:
transaction.rollback()
return JsonResponse({
"message": "注册失败",
"username": username
})
if user is not None and user.is_active:
auth.login(request, user)
# request.session['user'] = username
return HttpResponseRedirect("/index/")
前端展示:
<ul class="fix_right">
<p>欢迎您,<a >{{ user }}</a></p> # 仅仅当注册和登录分开写函数的时候,显示正常
建议看下markdown中代码块格式,便于其他人阅读你的代码。
– 。淑女范erり 4年前