简体   繁体   English

使用Django将URL映射到网站根

[英]Mapping URLs to Site Root with Django

I currently have a Django app called "misc" where I have my sites index page, as well as some other pages (most of which contain static information). 我目前有一个名为“ misc”的Django应用,其中有我的网站索引页面以及一些其他页面(其中大多数包含静态信息)。 Right now my misc/views.py looks like this: 现在我的misc / views.py看起来像这样:

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

def why(request):
    return render(request, 'misc/why.html', {}) 

and my urlpatterns in misc/urls.py looks like this: 我在misc / urls.py中的 urlpatterns看起来像这样:

url(r'^$', views.index, name='index'),
url(r'^why/$', views.why, name='why'),   

and my main urls.py looks like this: 我的主要urls.py看起来像这样:

url(r'^$', include('misc.urls')),

so when I go to http://127.0.0.1:8000/ I see my index page just fine, however when I go to http://127.0.0.1:8000/why/ I do not see my why.html page, just a "Page not found" error. 因此,当我访问http://127.0.0.1:8000/时,我可以看到我的索引页面,但是当我访问http://127.0.0.1:8000/why/时,看不到我的why.html页面,只是“找不到页面”错误。 Any idea on how I can resolve this? 关于如何解决此问题的任何想法吗? Thanks. 谢谢。

Your url pattern is matching an empty string: 您的网址格式与一个空字符串匹配:

url(r'^$', include('misc.urls')),

^ matches the beginning of a string, and $ matches the end, so only completely empty strings will work. ^匹配字符串的开头, $匹配结尾,因此仅完全空的字符串有效。 As Dima points out in the comments, remove the $ and it will work. 正如Dima在注释中指出的那样,删除$使用。

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

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