/处没有反向匹配,并且搜寻器不是注册的名称空间 [英] no reverse matching at / and crawler is not a registered namespace
问题描述
我正在尝试制作一个Codeforce搜寻器,而我只是添加了用户身份验证,但以某种方式未能实现. Reverse not match and crawler is not a registered namespace
是我遇到的错误.我不知道确切需要将哪些文件放在这里,所以请问我,如果需要的话,我会发布它们.我只是一个初学者,我需要帮助.
I am trying to make a codeforces crawler and I am just adding user authentication in the somehow failed to implement. Reverse not match and crawler is not a registered namespace
is the error I'm getting. I don't know what files exactly are needed to put here so please ask me I will post them if you need it. I'm just a beginner and I need help.
crawler/urls.py
app_name = 'crawler'
urlpatterns = [
path('',views.index,name='index'),
path('formpage/',views.search_form_view , name='searchform'),
path('formpage/<str:handle>',views.person, name= 'person'),
path('user_login/',views.user_login,name ="user_login"),
path('logout/',views.user_logout,name="logout"),
]
base.html
<body>
<nav class="navbar navbar-expand-sm bg-dark navbar-dark">
<!-- Brand -->
<a class="navbar-brand" href="{% url 'crawler:index'%}">Crawler</a>
<!-- Links -->
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'crawler:searchform'%}">Search</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link 2</a>
</li>
{% if user.is_authenticated %}
<li class="nav-item">
<a class="nav-link" href="{%url 'crawler : logout'%}">Log Out</a>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{%url 'crawler :user_login'%}">Login</a>
{% endif %}
</li>
</li>
</ul>
</nav>
<br>
{% block body_block %}
{% endblock %}
</body>
views.py
@login_required
def user_logout(request):
logout(request)
return HttpResponse(reverse('index'))
webcrawler/urls.py
app_name = 'crawler'
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('crawler.urls',namespace= "crawler")),
]
推荐答案
此处分为两部分.
/处没有反向匹配
问题在于,在您的主url和应用urls.py中,您都将两个URL注册为",这意味着在(空字符串)处将有一个匹配项,但在'/'中将不存在一个匹配项. .要解决此问题,只需在 main urls.py中添加"/",这是一种更好的做法.
The point is that, in both your main and app urls.py, you registered both URLs to be '', which means that there will be a match at (empty string) but won't be one at '/'. To fix this, simply add '/' to the main urls.py as it's a better practice.
抓取工具不是注册的名称空间
在调用URL时,它应该是{% url 'crawler:index' %}
或{% url 'crawler:searchform' %}
之类的东西,因为搜寻器是主要的命名空间,但是它下面有多个URL,因此您需要在搜寻器命名空间之后传递一个附加参数.
As you call an URL, it should be {% url 'crawler:index' %}
or {% url 'crawler:searchform' %}
or something because crawler is the main namespace but there are multiple URLs under it so you need to pass an additional parameter after your crawler namespace.
这篇关于/处没有反向匹配,并且搜寻器不是注册的名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!