【问题标题】:How to resolve NoReverseMatch in Django如何在 Django 中解决 NoReverseMatch
【发布时间】:2020-08-11 17:05:00
【问题描述】:

伙计们,当我尝试登录我的 Django Web 应用程序时遇到此错误

/login/ 处的 NoReverseMatch 未找到“博客主页”的反向。 '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"),

]

这也是一个名为 users 的单独应用中的登录模板代码

{% 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)

【问题讨论】:

  • 所以您的应用名为“blog”或“posts”?而且我没有看到任何尝试在给定模板中反转"blog-home",也许它在基本模板中。

标签: python django web


【解决方案1】:

您已将 app_name 的“帖子”添加到您的 urls.py 文件中。这意味着您需要使用 URL 名称“posts:blog-home”。请参阅Django docs about url namespaces。哦,很可能这个对“blog-home”的错误引用实际上是在您的基本模板“blog/base. html",您从中扩展而来(但当您尝试转到登录页面时仍会导致错误,因为随后会拉入来自基本模板的 html)。

【讨论】:

    猜你喜欢
    • 2021-08-04
    • 2021-06-14
    • 2020-04-18
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 2012-10-01
    相关资源
    最近更新 更多