标签:one username too error turn profile int word from
from django.contrib.auth.decorators import login_required
@login_required
def my_view(request):
...
@login_required官网解释:If the user isn’t logged in, redirect to settings.LOGIN_URL, passing the current absolute path in the query string
即将settings.LOGIN_URL设置为你的登陆页面地址
对于我的情况如下:
settings.py
LOGIN_URL = "/login/"
urls.py
url(r‘^$‘, views.index, name="index"),
url(r‘^login/$‘, views.user_login, name="login"),
views.py
@login_required
def index(request):
return render(request, "index1.html")
def user_login(request):
if request.method == "POST":
# try:
# m = models.UserProfile.objects.get(email=request.POST[‘username‘])
# except Exception:
# print("username doesn‘t exist")
# return render(request, "login.html")
username = request.POST["username"]
password = request.POST["password"]
user = authenticate(request, email=username, password=password)
error_msg = "账号或密码错误,请重新输入"
none_msg = "please input username and password"
if user is not None:
if user.is_active:
login(request, user)
# request.session[‘member_id‘] = m.id
return redirect("/") # redirect to 127.0.0.1:8000/
else:
print(none_msg)
return render(request, "login.html", {"error_msg": none_msg})
else:
print(error_msg)
return render(request, "login.html", {"error_msg": error_msg})
else:
return render(request, "login.html")
标签:one username too error turn profile int word from
原文地址:https://www.cnblogs.com/vickey-wu/p/8807376.html