简体   繁体   English

在 Python Django 中正确链接到 index.html

[英]Link correctly to index.html in Python Django

I have a Python Django project with the following files.我有一个 Python Django 项目,其中包含以下文件。

proj1/urls.py proj1/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('website.urls')),
]

proj1/website/views.py proj1/网站/views.py

from django.shortcuts import render

def index(request):
    return  render(request, 'index.html', {})

proj1/website/urls.py proj1/网站/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name="index"),
]

Under proj1/website/templates I have a few.html files.在 proj1/website/templates 我有几个.html 文件。 How do I link to those in my.html files?如何链接到 my.html 文件中的那些? I have currently the following written in index.html我目前在 index.html 中写了以下内容

<a href="index.html">Home</a>

I get error message when I press the link for the index.html though.当我按下 index.html 的链接时,我收到错误消息。

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/index.html
Using the URLconf defined in leam.urls, Django tried these URL patterns, in this order:

admin/
[name='index']
The current path, index.html, didn't match any of these.

What am I doing wrong?我究竟做错了什么? Do I need to specify where the index.html is located with a full path, or what is wrong here?我是否需要使用完整路径指定 index.html 所在的位置,或者这里有什么问题? The index.html loads with run manage.py. index.html 通过运行 manage.py 加载。

<a href="{% url 'website:index' %}">Home</a>
from django.urls import path
from . import views

app_name = 'website'

urlpatterns = [
    path('', views.index, name="index"),
]

Try this, it should work.试试这个,它应该工作。
The formula is {% url '<namespace or app name>:<view name>' %}公式为{% url '<namespace or app name>:<view name>' %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM