我知道这个问题已经回答了重定向是一个或另一种方式,但我仍然无法弄清楚如何在用户登录后重定向。我知道Django自带了内置的网站,但我需要一个自定义登录表单,它看起来像这样(这是HTML):的Django的Python登录后
{% if user.is_authenticated %}
<!-- Authenticate account menu -->
{% else %}
<h3>Login</h3>
<form action="/app/login/" method="post" accept-charset="utf-8">
<label for="username">Username</label>
<input type="text" name="username" value="" id="username" />
<label for="password">Password</label>
<input type="password" name="password" value="" id="password" />
<p><input type="submit" value="Login →"></p>
</form>
{% endif %}
我views.py看起来像这样:
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import Context, loader
from django.contrib.auth import authenticate, login
from django.views.generic.simple import *
def index(request):
if request.method == 'POST':
user = authenticate(username=request.POST['username'], password=request.POST['password'])
if user is not None:
if user.is_active:
login(request, user)
# success
if request.POST['next']:
return HttpResponseRedirect(request.POST['next'])
else:
return HttpResponseRedirect('/')
else:
# disabled account
return direct_to_template(request, 'inactive_account.html')
else:
# invalid login
return render_to_response("app/index.html")
return render_to_response("app/index.html")
我并没有完全写代码自己。我发现重定向发生在这里的html文件中:<form action="/app/login/
。但django说它无法找到网址。总而言之,我不得不说我是网络编程+ django + python的新手,并且不完全清楚这个概念。 感谢您的帮助!
您在'authenticate(username = request.POST ['username']''中缺少'''',并且您的缩进不正确。请更新您的问题,并发布您的'urls.py'。 –