【问题标题】:Django. Url cant find view姜戈。网址找不到视图
【发布时间】:2021-03-05 10:25:57
【问题描述】:

当我尝试打开另一个视图时,我需要有关 {% url (url to template) %} 的帮助,但我看到了 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)

【问题讨论】:

    标签: python html django url django-urls


    【解决方案1】:
    <a href='{% url "pages:home_view" %}'><img id = "img1" src="{% static 'logo.png' %}" alt="logo"></a>
    

    你可以这样做。

    【讨论】:

      【解决方案2】:

      视图的名称是home_view:

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

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

      &lt;a href="{% url <b>'home_view'</b> %}"&gt;

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

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

      【讨论】:

        【解决方案3】:

        url_name 应该是 home_view 而不是 homepath 函数中的 name 参数用于引用视图。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-20
          • 2021-12-23
          • 2017-02-08
          • 2020-08-17
          • 2012-08-19
          • 2017-01-14
          • 2022-01-13
          • 1970-01-01
          相关资源
          最近更新 更多