[英]Django login required twice
我正在創建一個我登錄的網頁,並將人員添加到地址簿中。 一旦我登錄並點擊“添加地址”按鈕,我就會被重定向回登錄頁面,其中包含以下網址:
http://localhost:8000/xcard/login/?next=/xcard/add_address/
如果我再次登錄,我可以訪問帳戶頁面,地址簿,然后添加add_address書頁,而不會被登錄循環捕獲。 我可以注銷並登錄並添加地址而無需重新登錄兩次。 但是我第一次登錄時我必須做兩次。 不確定登錄或添加地址代碼是否有問題。
Views.py
class LoginView(View):
def get(self, request):
''' if user is authenticated '''
if request.user.is_authenticated():
return render(request, 'xcard/account.html')
else:
return render(request, 'xcard/login.html')
def post(self, request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
state = "The email or password is incorrect"
if user is not None:
login(request, user)
return HttpResponseRedirect('/xcard/account/')
else:
return render(request, 'xcard/login.html', {'state':state})
class AddAddressView(View):
def get(self,request):
address_form = AddressForm()
friend_form = FriendForm()
return render(request, 'xcard/add_address.html', {'friend_form':friend_form, 'address_form':address_form})
def post(self,request):
address_form = AddressForm(request.POST)
friend_form = FriendForm(request.POST)
if address_form.is_valid() and friend_form.is_valid():
new_address = address_form.save()
new_friend = friend_form.save(commit=False)
new_friend.address = new_address
new_friend.save()
return HttpResponseRedirect('/xcard/address_book')
else:
return render(request, 'xcard/add_address.html', {'state' : "Failed", 'friend_form':friend_form, 'address_form':address_form})
模板:address_book.html
{% include "xcard/header.html" %}
{% block main %}
<div class="container">
<h3 class="text-info"><u>Your Account</u></h3>
<a href="/xcard/add_address/" role="button" class="btn btn-primary" data-toggle="modal">Add</a>
<a href="/xcard/import_address/" role="button" class="btn btn-primary" data-toggle="modal">Import</a>
</div>
{% endblock %}
模板:login.html
{% extends "xcard/base.html" %}
{% block main %}
<div class="container">
<div class="row space">
<p class="text-center lead text-warning">
Login page</p>
<p class="text-center text-info">Trusted worldwide!</p>
</div>
<div class="row">
<div class="span offset4">
<form class="well" action="/xcard/login/" method="post">
{% csrf_token %}
<p class="lead">Sign In</p>
<fieldset class="login_page">
<p class="text-error"><strong>{{ state }}</strong></p>
<label class="control-label" for ="inputIcon">Email</label>
<div class="controls">
<div class="input-prepend">
<span class="add-on"><i class="icon-user"></i></span>
<input type="text" class="span3" id="ernainputIcon" required name="username" placeholder="Username...."/><br/><br/>
</div>
</div>
<label>Password</label>
<div class="controls">
<div class="input-prepend">
<span class="add-on"><i class="icon-lock"></i></span>
<input type="password" class="span3" id="inputIcon" required name="password" placeholder="Password...."/><br/><br/><br />
</div>
</div>
<button class="btn btn-primary">Sign In</button>
Not a user?
<a href="/xcard/registration/">Sign up</a>
</fieldset>
</form>
</div>
</div>
</div>
{% endblock %}
我剛剛在urls.py中找到了這個
url(r'^add_address/$', login_required(AddAddressView.as_view(), login_url='/xcard/login/')),
也許這是導致問題的原因? 但為什么不注冊我已經登錄?
首先在AddAddressView函數中進行更正。 更新行
return render(request, 'xcard/add_address.html', {'friend_form':friend_form, 'address_form':address_form})
它會工作
這是我的解決方案 - 在您嘗試進行身份驗證之前注銷。
當用戶登錄並使用其他用戶名重新登錄時,就會發生此問題。
import django.contrib.auth as djangoAuth
djangoAuth.logout(request) # logout
user = djangoAuth.authenticate(username=username, password=password) # login
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.