当我输入“Logout”按钮时,发生Page not found错误

时间:2017-10-02 11:30:14

标签: python html django

当我点击“退出”按钮时,会发现Page not found错误。

views.py:

@login_required
def logout_view(request):
    logout(request)
    return render(request, 'registration/accounts/top.html')
index.html中的

<div class="container">
        <ul class="top-menu">
            <form name="logout" method="post" action="{% url 'accounts:logout_view' %}">
            <button type="submit" class="btn btn-primary btn-lg" style="color:white;background-color: #F62459;border-style: none;">Logout</button>
            <input name="next" type="hidden"/>
            </form>
        </ul>
</div>

但我放了Logout按钮,发现Page not found(404)错误.Traceback是

找不到页面(404)请求方法:POST 请求网址:http://localhost:8000/static/accounts/logout_view

我的理想系统是当我输出注销按钮时,它会发送top.html。

项目树

accounts
|migrations
|static
|templates
  |registration
  |accounts
    |base.html
    |detail.html

我的代码有什么问题?我应该如何解决这个问题?

urls.py是

urlpatterns = [
    url(r'^top$', views.top,name='top'),
    url(r'^detail$', views.detail,name='detail'),
    url(r'^login/$', login,
        {'template_name': 'registration/accounts/login.html'},
        name='login'),
    url(r'^logout/$', views.logout_view, name='logout'),
]

1 个答案:

答案 0 :(得分:1)

您将退出网址命名为“logout”:

url(r'^logout/$', views.logout_view, name='logout'),

因此,在撤销网址时,您也应该将其称为“注销”:

<form name="logout" method="post" action="{% url 'accounts:logout' %}">