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)
        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>

应该发生的是用户注销,并被重定向到索引页面,而由于用户未登录,它将把用户重定向到登录页面。

但是,它只向我显示: View django.contrib.auth.logout 没有返回 HttpResponse 对象。

这是项目根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")),
)

这是应用程序的 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'),
)

最佳答案

试试这个:

from django.shortcuts import HttpResponseRedirect

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

而不是 webapp/index.html,您应该在 HttpResponseRedirect 中提供登录页面的 URL,如 /loginpage/

关于python - 注销页面在 Django 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25274104/

相关文章:

python - 从 Python 调用 C DLL 函数时出错

python - 如何从特定时间开始按周分组

python - 找出进程写入的位置

python - Tensorflow SxN矩阵乘以SxD矩阵输出SxNxD数组

python - 将两个列表中的项目配对

Django:如何从模板中的QueryDict获取数组?

django - 如何在构建时链接 docker 容器?

ajax - Cappuccino 、Django、AJAX 并将它们组合在一起 - 回顾我的架构!

django-extensions - 获取 manage.py shell_plus 以保存历史记录?

python - Django - 按 CharField 值长度过滤查询集