当我在detail.html中输入注销按钮时出现错误,找不到页面(404)。 我在header.html中写道
<header class="clearfix">
<h1 class="title">WEB SITE</h1>
<div class="collapse navbar-collapse head_content" id="navbarNavDropdown">
<ul class="navbar-nav top-menu">
<li class="nav-item top-menu-item"><i class="fa fa-plus" aria-hidden="true">
<a class="nav-link icon_head" href="/accounts/see">SEE</a></i>
</li>
<li class="nav-item dropdown top-menu-item"><i class="fa fa-eye" aria-hidden="true">
<a class="nav-link dropdown-toggle icon_head" href="" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
KNOW
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<div class="dropdown-content">
<a class="dropdown-item" href="/accounts/nowknow">KNOW1</a>
</div>
<div class="dropdown-content">
<a class="dropdown-item" href="/accounts/pastknow">KNOW2</a>
</div>
</div>
</i>
</li>
</ul>
</div>
<a class="logout_button" href="/accounts/logout_view">LOGOUT</a>
</header>
我在views.py中写道
@login_required
def logout_view(request):
logout(request)
return redirect('regist')
@login_required
def regist(request):
return render(request, 'registration/regist.html')
@login_required
def see(request):
return render(request, 'registration/see.html')
@login_required
def nowknow(request):
return render(request, 'registration/nowknow.html')
@login_required
def pastknow(request):
return render(request, 'registration/pastknow.html')
@login_required
def detail(request):
return render(request, 'registration/detail.html')
在urls.py中
urlpatterns = [
url(r'^product$', views.logout_view,name='logout_view'),
url(r'^past_result$', views.see,name='see'),
url(r'^privacy_policy$', views.nowknow,name='nowknow'),
url(r'^privacy_policy$', views.pastknow,name='pastknow'),
url(r'^detail$', views.detail,name='detail'),
]
我在index.html中写道
<html lang="ja">
<head>
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<title>WEB SITE</title>
</head>
<body class="relative">
{% include '../header.html' %}
<main>
<p>HELLO WORLD</p>
</main>
</body>
</html>
当我执行index.html并放置LOGOUT链接时,注销就完成了。但我在detail.html中写道
<html lang="ja">
<head>
<meta charset="UTF-8">
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<title>DETAIL</title>
</head>
<body class="relative">
{% include '../header.html' %}
<main>
<p>HOGE</p>
</main>
</body>
</html>
并把LOGOUT链接,错误发生。在这个时候,请求URL是http://localhost:8000/accounts/detail/logout_view.I真的无法理解为什么会发生这个错误。我写了2个HTML同样的方式,所以我不知道为什么一个HTML是好的但是另一个导致错误。我该如何解决这个问题?
答案 0 :(得分:3)
出于某种原因,您已将logout_view视图的URL定义为“product”,因此这是您需要使用的URL。
要避免此类问题,您应始终使用{% url %}
标记生成网址,而不是在模板中对其进行硬编码:
<a class="logout_button" href="{% url 'logout_view' %}">LOGOUT</a>