Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法将网页从主页切换到关于网页_Python_Django_Django Templates_Django Generic Views - Fatal编程技术网

Python 无法将网页从主页切换到关于网页

Python 无法将网页从主页切换到关于网页,python,django,django-templates,django-generic-views,Python,Django,Django Templates,Django Generic Views,我正在尝试用django创建一个小型web应用程序,我是这方面的新手,我正在尝试创建一个主页和主页上的一个about按钮,将页面重新加载到about页面 这是我的index.html <!DOCTYPE html> <html> <head> <title>Simple App</title> <h1> Testing the simple app</h1> </head> <body>

我正在尝试用django创建一个小型web应用程序,我是这方面的新手,我正在尝试创建一个主页和主页上的一个about按钮,将页面重新加载到about页面

这是我的index.html

<!DOCTYPE html>
<html>
<head>
  <title>Simple App</title>
<h1> Testing the simple app</h1>
</head>
<body>
  <a href="/about/">About </a>
</body>
</html>
和url.py

from django.conf.urls import url
from django.contrib import admin
from homepage import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'', views.HomePageView.as_view()),
    url(r'^about/$',views.AboutPageView.as_view()),
]

但是,当我单击“关于”按钮时,在
url.py
中什么也没有发生,您可以直接告诉通用模板视图要使用的布局名称,如下所示:

from django.urls import include, path
from django.views.generic import TemplateView

urlpatterns = [
    path("", TemplateView.as_view(template_name="homepage.html"), name="home"),
    path("about/", TemplateView.as_view(template_name="about.html"), name="about" ]
使用命名url比直接编码url要好,因为它们可能在将来发生变化

然后在homepage.html中,将其称为:

<a href="{% url 'about' %}">About</a>

当我更改它时,我得到一个NoReverseMatch found错误“about.html”不是一个有效的视图函数或模式名称检查,您在URL.py中添加了
name=“about”
,我没有使用路径,因为我使用的是django谢谢。它起作用了!现在,这将足以建立我的基本技能,但我有一个任务,利用表格和数据库来存储和访问数据。如果我在这方面有任何困难,如果您也能帮助我,我将不胜感激。
from django.urls import include, path
from django.views.generic import TemplateView

urlpatterns = [
    path("", TemplateView.as_view(template_name="homepage.html"), name="home"),
    path("about/", TemplateView.as_view(template_name="about.html"), name="about" ]
<a href="{% url 'about' %}">About</a>
url(r'^$',TemplateView.as_view(template_name="homepage.html"), name="home"),
url(r'^about/$',TemplateView.as_view(template_name="about.html"), name="about"),