如何解决如何在Django中解决NoReverseMatch
伙计们,当我尝试登录Django Web应用程序时遇到此错误
位于/ login /的NoReverseMatch 找不到“ blog-home”的反向链接。 “ blog-home”不是有效的视图函数或模式名称。
这是应用程序的urls.py代码
from django.urls import path
from .views import (PostListView,PostCreateView,PostUpdateView,PostDetailView,PostDeleteView,UserPostListView)
from . import views
app_name = 'posts'
urlpatterns = [
path('',PostListView.as_view(),name="blog-home"),path('user/<str:username>/',UserPostListView.as_view(),name="user-posts"),path('post/<int:pk>/',PostDetailView.as_view(),name="post-detail"),path('post/new/',PostCreateView.as_view(),name="post-create"),path('post/<int:pk>/update',PostUpdateView.as_view(),name="post-update"),path('post/<int:pk>/delete',PostDeleteView.as_view(),name="post-delete"),path('about/',views.about,name="blog-about"),]
这也是另一个名为用户的应用程序中的登录模板代码
{% extends "blog/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="content-section">
<form method="POST">
{% csrf_token %}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Log In</legend>
{{ form|crispy }}
</fieldset>
<div class="form-group">
<button class="btn btn-outline-info" type="submit">Login</button>
<small class="text-muted ml-2">
<a href="{% url 'password_reset' %}">Forgot Password?</a>
</small>
</div>
</form>
<div class="border-top pt-3">
<small class="text-muted">
Need An Account? <a class="ml-2" href="{% url 'register' %}">Sign Up</a>
</small>
</div>
</div>
{% endblock %}
下面是项目urls.py
from django.contrib import admin
from django.contrib.auth import views as auth_views
from django.urls import path,include
from django.conf import settings
from django.conf.urls.static import static
from users import views as user_view
urlpatterns = [
path('admin/',admin.site.urls),path('register/',user_view.register,name="register"),path('profile/',user_view.profile,name="profile"),path('groups/',include("groups.urls",namespace="groups")),path('login/',auth_views.LoginView.as_view(template_name='users/login.html'),name="login"),path('logout/',auth_views.LogoutView.as_view(template_name='users/logout.html'),name="logout"),path('password-reset/',auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'),name="password_reset"),path('password-reset/done/',auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'),name="password_reset_done"),path('password-reset-confirm/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'),name="password_reset_confirm"),path('password-reset-complete/',auth_views.PasswordResetCompleteView.as_view(template_name='users/password_reset_complete.html'),name="password_reset_complete"),path('',include("blog.urls")),]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
解决方法
您已在您的urls.py文件中添加了{posts} app_name
。这意味着您需要使用URL名称'posts:blog-home'。请参阅Django docs about url namespaces。哦,很可能这种对'blog-home'的错误引用实际上在您的基本模板“ blog / base.html”(您从中扩展(但尝试进入登录页面时仍然会导致错误,因为从基本模板中提取的html会随后出现)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。