【问题标题】:How to navigate from current html page to another html page in django如何在 django 中从当前 html 页面导航到另一个 html 页面
【发布时间】:2019-03-17 17:46:30
【问题描述】:

我正在开发 django 应用程序,当我需要从我的 index.html 页面导航到另一个 about.html 页面时遇到了问题。我在下面以这种方式设置所有内容:

urls.py(我的项目)

from django.urls import path, include
urlpatterns = [
    path('', include('weather_app.urls')),
    path('about/', include('weather_app.urls'))
]

urls.py (myapp)

from django.urls import path
from . import views

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

在index.html中,一切正常,我就不放代码了。

在 view.py 中:

from django.shortcuts import render
import requests

def about(request):
   return render(request, "about.html")

我的 index.html 中有以下代码:

<a href="/about.html/">About </a> 

点击“关于”链接时,我无法获取 about.html 页面,如上所示。

有人可以帮我吗?

提前谢谢..

【问题讨论】:

  • 您没有 URL 为“/about.html/”的页面。您有一个 URL 为“/about/”。
  • 删除项目urls.py 中的“关于”网址。除非您的意思是/about/about/,否则您编写它的方式是行不通的。

标签: django python-3.x django-templates


【解决方案1】:

您必须使用this tag 以避免手动呈现网址。 在您的代码中:

&lt;a href="{% url 'about' %}"&gt;About &lt;/a&gt;

让我知道这是否有效!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    相关资源
    最近更新 更多