从根页面到子页面的Django Noob URL

从根页面到子页面的Django Noob URL,django,django-views,Django,Django Views,我有一个Django项目,我想为所有网站和网页使用一个应用程序。我的项目如下所示: project src project urls.py views.py ... web migrations #package urls.py views.py ... templates web index.html # I want this to be m

我有一个Django项目,我想为所有网站和网页使用一个应用程序。我的项目如下所示:

project
  src
    project
      urls.py
      views.py
      ...
    web
      migrations #package
      urls.py
      views.py
      ...
      templates
        web
          index.html # I want this to be my root page
          page2.html # This is the second page I'm trying to link to
我正在尝试在
index.html
中创建一个链接,该链接将带我进入
page2.html
。这就是我正在做的

project->url.py
中,我有
url(r'^$',包括('web.url',namespace=“web”),
。这会将所有页面请求定向到url
http://127.0.0.1:8000/
至页面
index.html

project->views.py
为空,因为我希望
web
应用程序提供所有网页

web->url.py
中,我有
url(r'^$',views.index,name='home\u page')
,它与
web->views.py
和函数相关

def index(request):
   print("Main index Page")
   return render(request, 'web/index.html', {})
返回正确的页面

在我为
page2.html
添加到
index.html
的链接之前,这一切都很正常。该链接如下所示:
{%url'web:page2%}
。我更新
web->url.py
。我将以下函数添加到
web->views.py

def page2(request):
   print("Page2")
   return render(request, 'web/page2.html', {})
现在我明白了

Reverse for 'page2' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['$page2/?$']
突出显示“{%url”web:page2“%”行


当我删除链接时,一切正常。我的逻辑/设置有什么问题?

您需要添加一个额外的URL模式:

urls = [
    url(r'^page2/$', views.page2, name='page2'),
    url(r'^$', views.index, name='home_page'),
]
或者,传递一个参数,用于标识要呈现给视图的页面。当前,当URL与page2匹配时,您没有将URL映射到要调用的函数,只映射到主页