简体   繁体   English

注销页面在 Django 中不起作用

[英]Logout Page not working in Django

I'm trying to create a logout page for django.我正在尝试为 django 创建一个注销页面。

This is the views.py file:这是 views.py 文件:

def index(request):
    if not request.user.is_authenticated():
        return redirect('webapp/login.html')
    else:
        result = Hello_World.delay()
        somethingDownByCelery = result.get(timeout=2)
        context = {'somethingDownByCelery': somethingDownByCelery, 'userName': request.user.username}
        return render(request, 'webapp/index.html', context)

def loginUser(request):
    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)
            return redirect('webapp/index.html')
        else:
            return redirect('webapp/disabled.html')
    else:
        condition = "Invalid Login"
        context = {'condition', condition}
        return render(request, 'webapp/index.html', context)

def logoutUser(request):
    logout(request)
    return redirect('webapp/index.html')

This is the index page after the logout is initiated.这是启动注销后的索引页面。

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'WebApp/style.css' %}"/>


Hello World, this will call celery!
<html>
    <br>
</html>
{{ somethingDownByCelery }}

<html>
    <br>
    <br>
</html>
Hello! {{ userName }}

<html>
    <br>
    <br>
</html>

<form action="{% url 'WebApp:logout'%}" method="post">
    {% csrf_token %}
    <p> Logout </p>
    <input type="submit" value="Submit">
</form>

What should happen is that the user would logout, and get redirected to the index page, whereas since the user is not logged in, it will redirect the user to a login page.应该发生的情况是,用户会注销,并被重定向到索引页面,而由于用户未登录,它会将用户重定向到登录页面。

However, it only shows me: The view django.contrib.auth.logout didn't return an HttpResponse object.但是,它只显示给我:视图 django.contrib.auth.logout 没有返回 HttpResponse 对象。

This is the project root urls.py这是项目根 urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
from WebApp import views
from StripCal import views
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'Dashboard_Web.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^webapp/', include('WebApp.urls', namespace="WebApp")),
)

This is the app's urls.py这是应用程序的 urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
from WebApp import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^login', views.login, name='login'),
    url(r'^logout', views.logout, name='logout'),
)

try this:尝试这个:

from django.shortcuts import HttpResponseRedirect

def logoutUser(request):
   logout(request)
   return HttpResponseRedirect('/loginpage/')

instead of webapp/index.html , you should give the URL of your login page like /loginpage/ inside HttpResponseRedirect而不是webapp/index.html ,您应该在HttpResponseRedirect登录页面的 URL,例如/loginpage/

Instead of writing your own, use Djangos builtin logout view for this.与其编写自己的,不如使用 Django 的内置注销视图。

#urls.py

from django.contrib.auth.views import logout

url(r'^sign-out/$', logout, {'template_name': 'index.html', 'next_page': '/'}, name='sign-out'),

Docs are found here and now you can link to this without the use of a form and Django will take care of doing the redirection for you. 文档可以在这里找到,现在您可以在不使用表单的情况下链接到该文档,Django 将负责为您进行重定向。

I know that this is a old post, but no answer addressed what went wrong in the code so here it is.我知道这是一篇旧帖子,但没有答案解决代码中出了什么问题,所以就在这里。 In the webapp urls.py file view must be pointing to logoutUser view and not logout view.在 webapp urls.py文件中,视图必须指向 logoutUser 视图而不是 logout 视图。 and similarly with the login view与登录视图类似

from django.contrib import admin
from WebApp import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^login', views.loginUser, name='login'),
    url(r'^logout', views.logoutUser, name='logout'),
)

Now the url function is taking the intended view method as an argument现在 url 函数将预期的视图方法作为参数

First things first.先说第一件事。 Your function should look similar to this one:你的函数应该类似于这个:

def logout_view(request):
    logout(request)
    return redirect('/login')

To use logout, you have to import logout like this:要使用注销,您必须像这样导入注销:

from django.contrib.auth import logout

If you are redirecting user on the basis of User.is_authenticated, it would never redirect because if the user is not logged in, an anonymous user it created.如果您根据 User.is_authenticated 重定向用户,它将永远不会重定向,因为如果用户未登录,则会创建一个匿名用户。 You can do something like ths:你可以做这样的事情:

def home(request):
if request.user.is_anonymous == True:
    return redirect('/login')
else:
    return render(request,"index.html",{'nav':request.user})

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

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