Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 注销页面在Django中不工作_Python_Django - Fatal编程技术网

Python 注销页面在Django中不工作

Python 注销页面在Django中不工作,python,django,Python,Django,我正在尝试为django创建注销页面 这是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)

我正在尝试为django创建注销页面

这是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')
这是启动注销后的索引页

{% 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>
这是应用程序的URL.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'),
)
试试这个:

from django.shortcuts import HttpResponseRedirect

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

webapp/index.html
不同,您应该在
HttpResponseRedirect
中提供登录页面的URL,如
/loginpage/
,而不是编写自己的,使用Djangos内置注销视图

#urls.py

from django.contrib.auth.views import logout

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

现在您无需使用表单就可以链接到这篇文章,Django将负责为您进行重定向。

我知道这是一篇老文章,但没有回答代码中出现的问题,所以这里就是。 在webapp
url.py中,文件视图必须指向logoutUser视图,而不是logout视图。
与登录视图类似

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'),
)
现在,url函数将预期的视图方法作为参数

第一件事。 您的函数应类似于此函数:

def logout_view(request):
    logout(request)
    return redirect('/login')
要使用注销,必须按如下方式导入注销:

from django.contrib.auth import logout
如果您基于user.is_身份验证重定向用户,它将永远不会重定向,因为如果用户未登录,它将创建一个匿名用户。 你可以这样做:

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

为什么不这样做:
返回HttpResponseRedirect('webapp/index.html')
而不是
返回重定向('webapp/index.html')
?还有,为什么要发布以注销?即使使用
get
,您也会收到一个请求。因为如果有人进行预取,结果会很有趣,因为您实际上根本没有进入logoutUser视图,错误消息证明了这一点:不知何故,您将进入django.contrib.auth.logout,而这不是一个视图。你能发布你的URL.py吗?不幸的是,HttpResponseRedirect也不能工作。我不确定为什么会发生这种情况。@user1157751查看我的更新。您应该提供登录页面的URL。谢谢您的帮助!但是,我没有做HttpReportRedict('/login.html')也不起作用。@user1157751这是因为您有
url(r'^login',views.login,name='login')
而不是
url(r'^login.html',views.login,name='login')
谢谢您的帮助!我宁愿用“原始”的方式,因为我对Django视图还不熟悉。是的,不用担心,但是当它提供给你的时候,当你可以把时间花在更好地使用它的时候,为什么要在一些简单的东西上重新发明轮子呢,这是框架。它为您提供了所需的工具。为了让它看起来不那么像黑魔法,这里是源代码,谢谢你的源文件!
def home(request):
if request.user.is_anonymous == True:
    return redirect('/login')
else:
    return render(request,"index.html",{'nav':request.user})