简体   繁体   English

Django登录/注销

[英]Django login/logout

I'm trying to create a login and logout page with django. 我正在尝试使用django创建登录和注销页面。 The problem I have is that when I submit the form, it doesn't go the url which I specified. 我遇到的问题是,当我提交表单时,它不会是我指定的URL。 When I click the login button, I want it to go to http://127.0.0.1:8000/home/ but instead, it goes to http://127.0.0.1:8000/?next=/home/ . 当我点击登录按钮时,我希望它转到http://127.0.0.1:8000/home/ ,而是转到http://127.0.0.1:8000/?next=/home/

Below is my login/logout code in my view.py: 以下是我的view.py中的登录/注销代码:

def login(request):
    def errorHandler(error):
    return render_to_response('login.html', {'error' : error})
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username = username, password = password)
        if user is not None:
            if user.is_active:
                authLogin(request, user)
                fullName = user.get_full_name
                return render_to_response('logged_in.html', {'full_name': fullName})
            else:
                error = 'Account disabled.'
        return errorHandler(error)
        else:
            error = 'Invalid details entered.'
        return errorHandler(error)

    return render_to_response('login.html')

@login_required
def logout(request):
    authLogout(request)
    return render_to_response('logged_in.html')

my login.html: 我的login.html:

{% extends "base.html" %}

{% block content %}

  {% if error %}
    <p><b><font color="red">Error: </font></b>{{ error }}</p>
  {% endif %}

  <form action="/home/" method="post">
    <label for="username">User name:</label>
    <input type="text" name="username" value="" id="username">
    <label for="password">Password:</label>
    <input type="password" name="password" value="" id="password">

    <input type="submit" value="Login" />
    <input type="hidden" name="next" value="{{ next|escape }}" />
  </form>

{% endblock %}

my logged_in.html: 我的logged_in.html:

{% extends "base.html" %}

{% block name %}{{ full_name }} is {% endblock %}

{% block content %}
    <p><a href='/'>Logout</a></p>
{% endblock %}

url: 网址:

(r'^$', 'myapp.views.login'),
(r'^home/$', 'myapp.views.logout'), 

Please help 请帮忙

The problem is that you don't actually login the user, the form sends unauthenticated user to /home/ , which can't process, because it requires login. 问题是您实际上没有登录用户,表单将未经身份验证的用户发送到/home/ ,这无法处理,因为它需要登录。 :-) So it redirects user to login view, storing the destination in next parameter. :-)因此它将用户重定向到登录视图,将目标存储next参数中。

Solution : <form action="" method="post"> 解决方案<form action="" method="post">

也许你应该尝试删除名为=“next”的隐藏输入http://docs.djangoproject.com/en/1.2/topics/auth/#django.contrib.auth.views.login

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM