簡體   English   中英

在 Python Django 中正確鏈接到 index.html

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

我有一個 Python Django 項目,其中包含以下文件。

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/網站/views.py

from django.shortcuts import render

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

proj1/網站/urls.py

from django.urls import path
from . import views

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

在 proj1/website/templates 我有幾個.html 文件。 如何鏈接到 my.html 文件中的那些? 我目前在 index.html 中寫了以下內容

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

當我按下 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.

我究竟做錯了什么? 我是否需要使用完整路徑指定 index.html 所在的位置,或者這里有什么問題? 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"),
]

試試這個,它應該工作。
公式為{% url '<namespace or app name>:<view name>' %}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM