Django登录需要两次

时间:2013-11-25 05:11:02

标签: python django login

我正在创建一个我登录的网页,并将人员添加到地址簿中。一旦我登录并点击“添加地址”按钮,我就会被重定向回登录页面,其中包含以下网址:

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/')),

也许这会导致问题?但为什么不注册我已登录?

2 个答案:

答案 0 :(得分:0)

首先在AddAddressView函数中进行更正。更新行

return render(request, 'xcard/add_address.html', {'friend_form':friend_form, 'address_form':address_form})

它会起作用

答案 1 :(得分:0)

这是我的解决方案 - 在您尝试进行身份验证之前注销。

当用户登录并使用其他用户名重新登录时,就会发生此问题。

import django.contrib.auth as djangoAuth
djangoAuth.logout(request)  # logout
user = djangoAuth.authenticate(username=username, password=password) # login