将视图作为参数传递给 url 过滤器

问题描述 投票:0回答:1

发生的情况是,我希望在我的标题模板中嵌入到我的主页中,以包含指向其他模板的链接,根据我的研究,我发现使用 url 过滤器并向其传递一个视图应该可以工作,但在我的如果它不起作用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <header>
        <nav>
            <li>
              <ul><a href="{% url 'Home'%}">Principal</a></ul>
              <ul><a href="{% url 'Informacion'%}">Informacion</a></ul>
              <ul><a href="{% url 'Imagenes'%}">Imagenes</a></ul>
            </li>
        </nav>
    </header>
</body>
</html>

from django.shortcuts import render

# Create your views here.

def Home(request):
    return render(request,"principal.html")

def Informacion(request):

    return render(request,"Informacion.html")

def Imagenes(request):
    
    return render(request,"images.html")

这会产生“未找到”“Home”的反向。 “Home”不是有效的视图函数或模式名称。

html django url
1个回答
0
投票

路线名称在您的

urls.py
中定义,您的文件应如下所示:

from django.urls import path
from .views import home, informacion, imagenes

urlpatterns = [
   path("/", home, "home"),
   path("/informacion/", informacion, "informacion"),
   path("/imagenes/", imagenes, "imagenes")
]
© www.soinside.com 2019 - 2024. All rights reserved.