我知道這個問題已經回答了重定向是一個或另一種方式,但我仍然無法弄清楚如何在用戶登錄後重定向。我知道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'。 –