我正在制作一个网页,我可以在其中登录并将人员添加到地址簿。一旦我登录并单击“添加地址”按钮,我将被重定向回具有以下 url 的登录页面:
http://localhost:8000/xcard/login/?next=/xcard/add_address/
如果我再次登录,我可以到达帐户页面、地址簿,然后是 add_address 簿页面,而不会陷入登录循环。我可以注销并登录并添加地址而无需重新登录两次。但是我第一次登录时必须登录两次。不知道是登录问题还是地址码问题。
View .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})
模板: 地址簿.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 %}
模板: 登录.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})
它会起作用
关于python - Django 登录需要两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20185172/