user8681386 user8681386 - 3 years ago 161
HTML Question

When I put "Logout" button,Page not found error happens

When I click "Logout" button,Page not found error is raised.

def logout_view(request):
return render(request, 'registration/accounts/top.html')

in 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"/>

But I put Logout button,Page not found (404) error happens.Traceback is

Page not found (404)Request Method: POST
Request URL: http://localhost:8000/static/accounts/logout_view

My ideal system is when I put logout button, it sends top.html.

project tree


What is wrong in my code?How should I fix this? is

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

Answer Source

You name the logout url 'logout':

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

Therefore you should also refer to it as 'logout' when reversing the url:

<form name="logout" method="post" action="{% url 'accounts:logout' %}">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download