user8681386 user8681386 - 1 year ago 81
HTML Question

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

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

views.py:

@login_required
def logout_view(request):
logout(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"/>
</form>
</ul>
</div>


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

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


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

urls.py is

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'),
]

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