姜戈。 Url 找不到视图

Django. Url cant find view

我需要有关 {% url(url 到模板)%} 的帮助,当我尝试打开另一个视图时,我看到了 NoRewerseMatch 错误。

这是我的 html 文件:

{% load static %}
<head>
    <link href='{% static "navbar_style.css" %}' rel="stylesheet", type="text/css">
</head>
<header>
    <nav class="headlist">
--->    <a href='{% url "home" %}'><img id = "img1" src="{% static 'logo.png' %}" alt="logo"></a>
        <ul>
            <li><a href="#">O nas</a></li>
            <li><a href="#">Kontakt</a></li>
            <li><a>Zajęcia</a></li>
        </ul>
    </nav>
</header>

我的应用程序(页面)/urls.py

from django.contrib import admin
from django.urls import path
from . import views
app_name = 'pages'
urlpatterns = [
    path('', views.home_view, name='home_view'),
    path('index/', views.index_view, name='index_view'),
]

views.py

import...

# Create your views here.
def home_view(request):
    listoftexts = Text.objects.order_by('-pub_date')
    context = {
        'listoftexts': listoftexts
    }
    return render(request, 'pages/home.html', context)
def index_view(request):
    listoftexts = Text.objects.order_by('-pub_date')
    context = {
        'listoftexts': listoftexts
    }
    return render(request, 'pages/index.html', context)

视图的名称是home_view:

path('', views.home_view<b>, name='home_view'</b>),

因此您可以将 {% url … %} template tag [Django-doc] 用于:

<a href="{% url <b>'home_view'</b> %}">

如果您在 urls.py 中使用 app_name,您还需要在 url 中添加前缀:

<a href="{% url <b>'pages:home_view'</b> %}">

url_name 应该是 home_view 而不是 homepath 函数中的名称参数是您用来引用视图的参数。

<a href='{% url "pages:home_view" %}'><img id = "img1" src="{% static 'logo.png' %}" alt="logo"></a>

你可以这样做。