我有一个登录表单。按下登录按钮后,发布数据将发送到视图login_auth,该视图对用户数据进行身份验证并相应地重定向。但是,按下登录按钮后,我没有被重定向到相应的页面。
views.py
def login_successful(request):
return render(request,"login_successful.html")
def login_invalid(request):
return render(request,"login_invalid.html")
def login(request):
return render(request,'login.html',c)
def loginauth(request):
username=request.POST.get("username",'')
password=request.POST.get("password",'')
user=auth.authenticate(username=username,password=password)
if user is not none:
user.login(request.user)
return redirect(login_successful)
else:
return redirect(login_invalid)
urls.py
urlpatterns = [
url(r'^registration/',views.registration),
url(r'^registration_successful/',views.registration_successful),
url(r'^home/',views.home),
url(r'^login/',views.login),
url(r'^login_successful/',views.login_successful),
url(r'^login_invalid/',views.login_invalid),
url(r'^login/auth',views.loginauth)
]
的login.html
<html>
<form action="/login/auth" method="POST">{% csrf_token %}
Username :<input type="textbox" name="username" >
Password :<input type="password" name="password">
<input type="submit" value="Login">
</form>
</html>
答案 0 :(得分:0)
您的登录网址模式缺少尾随 source /usr/share/rvm/scripts/rvm
。它应该是:
$
没有美元,url(r'^login/$', views.login),
与/login/auth
匹配,因此请求由您的登录视图处理。
在不同的网址上处理表单有点不寻常。 Django附带authentication views,包括登录视图。我建议使用它而不是自己编写。
答案 1 :(得分:0)
使用名称作为网址
views.py
def login_successful(request):
return render(request,"login_successful.html")
def login_invalid(request):
return render(request,"login_invalid.html")
def login(request):
return render(request,'login.html',c)
def loginauth(request):
username=request.POST.get("username",'')
password=request.POST.get("password",'')
user=auth.authenticate(username=username,password=password)
if user is not none:
user.login(request.user)
return redirect('login_successful')
else:
return redirect('login_invalid')
urls.py
urlpatterns = [
url(r'^registration/',views.registration),
url(r'^registration_successful/',views.registration_successful),
url(r'^home/',views.home),
url(r'^login/$',views.login),
url(r'^login_successful/',views.login_successful, name='login_successful'),
url(r'^login_invalid/',views.login_invalid, name='login_invalid'),
url(r'^login/auth',views.loginauth)
]