リファラーページへのリダイレクトを除いて、正常に機能しているログインページがあります。ユーザーは、アプリ内の直接リンクが記載された電子メールを受け取ります。ユーザー (この例では) はまだログインしておらず、ログイン ページにリダイレクトされます。ログインに成功すると、ユーザーはハードコーディングされたパスにリダイレクトされます。以下の例を参照してください。
メールの URL:http://localhost:8000/issueapp/1628/view/22
ログインページの URL:http://localhost:8000/login?next=/issueapp/1628/view/22
ログイン ビュー (ハードコーディングされたリダイレクトあり):
def login_user(request):
state = "Please log in below..."
username = password = ''
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
return HttpResponseRedirect('/issueapp/1628/')
else:
state = "Your account is not active, please contact the site admin."
else:
state = "Your username and/or password were incorrect."
return render_to_response(
'account_login.html',
{
'state':state,
'username': username
},
context_instance=RequestContext(request)
)
ログイン ビュー (「次の」リダイレクトあり):
def login_user(request):
state = "Please log in below..."
username = password = ''
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
return HttpResponseRedirect(request.GET['next'])
else:
state = "Your account is not active, please contact the site admin."
else:
state = "Your username and/or password were incorrect."
return render_to_response(
'account_login.html',
{
'state':state,
'username': username
},
context_instance=RequestContext(request)
)
上記のビューでは例外が発生します"Key 'next' not found in <QueryDict: {}>"
。フォームは、URL とフォームにあるにもかかわらず、「次の」変数を投稿しているようには見えません。私はどこでも検索して見ましたが、なぜ機能しないのかわかりません。何か案は?
追加の参照:
ログイン テンプレート:
{% block content %}
{{ state }}
<form action="/login/" method="post" >
{% csrf_token %}
{% if next %}
<input type="hidden" name="next" value="{{ next }}" />
{% endif %}
username:
<input type="text" name="username" value="{{ username }}" /><br />
password:
<input type="password" name="password" value="" /><br />
<input type="submit" value="Log In"/>
{% debug %}
</form>
{% endblock %}
編集: 以下は、現在私のために機能しているコードです (Paulo Bu の助けのおかげで)! **
ログイン ビュー:
def login_user(request):
state = "Please log in below..."
username = password = ''
next = ""
if request.GET:
next = request.GET['next']
if request.POST:
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
state = "You're successfully logged in!"
if next == "":
return HttpResponseRedirect('/issueapp/')
else:
return HttpResponseRedirect(next)
else:
state = "Your account is not active, please contact the site admin."
else:
state = "Your username and/or password were incorrect."
return render_to_response(
'account_login.html',
{
'state':state,
'username': username,
'next':next,
},
context_instance=RequestContext(request)
)
ログイン テンプレート:
{{ state }}
{% if next %}
<form action="/login/?next={{next}}" method="post" >
{%else%}
<form action="/login/" method="post" >
{% endif %}
{% csrf_token %}
username:
<input type="text" name="username" value="{{ username }}" /><br />
password:
<input type="password" name="password" value="" /><br />
<input type="submit" value="Log In"/>
{% debug %}
</form>