Django Framework- 找不到页面
Django Framework- page not found
为什么我会收到 404?
这是我的 mysite/urls.py 文件:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$/', include('personal.urls')),
]
这是我的 mysite/personal/urls.py 文件
from django.conf.urls import url
from . import views
urlpatterns =[url(r'^$',views.index, name='index')]
mysite/personal/views.py
from django.shortcuts import render
def index(request):
return render(request, 'personal/home.html')
之后我创建了模板文件夹,如 templates/personal/header。html。该文件看起来像这样
<body class="body" style="background-color:#f6f6f6">
<div>
{% block content %}
{% endblock %}
</div>
</body>
现在关于同一文件夹中的home.html
{% extends "personal/header.html" %}
{% block content %}
<p>Hey! Welcome to my website! Well, I wasn't expecting guests. Um, my name is HVS. I am a programmer.</p>
{% endblock %}
我还在 mysite 子目录的 settings.py 文件中安装了该应用程序,但当我 运行
">python manage.py runserver"
它 运行 好的 bt 页面告诉我 url 找不到 404!
这是我的浏览器输出:
我正在使用 Windows 8、python 3 和 django 1.9
这里的 url 永远不会匹配
r'^$/'
$
匹配字符串结尾。这意味着 /
永远不可能跟在它之后。
你可能想要
url(r'^personal/', include('personal.urls')),
您应该从包含 personal.urls
:
的主 urls.py
中删除 $
url(r'^/', include('personal.urls')),
$
说明符将始终匹配字符串的末尾(即您的 URL),并且您将无法包含超出它的任何其他路径。
根据 django docs:
The include() function allows referencing other URLconfs. Note that
the regular expressions for the include() function doesn’t have a $
(end-of-string match character) but rather a trailing slash
所以你应该把它改成这样:
url(r'^/', include('personal.urls'))
为什么我会收到 404?
这是我的 mysite/urls.py 文件:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$/', include('personal.urls')),
]
这是我的 mysite/personal/urls.py 文件
from django.conf.urls import url
from . import views
urlpatterns =[url(r'^$',views.index, name='index')]
mysite/personal/views.py
from django.shortcuts import render
def index(request):
return render(request, 'personal/home.html')
之后我创建了模板文件夹,如 templates/personal/header。html。该文件看起来像这样
<body class="body" style="background-color:#f6f6f6">
<div>
{% block content %}
{% endblock %}
</div>
</body>
现在关于同一文件夹中的home.html
{% extends "personal/header.html" %}
{% block content %}
<p>Hey! Welcome to my website! Well, I wasn't expecting guests. Um, my name is HVS. I am a programmer.</p>
{% endblock %}
我还在 mysite 子目录的 settings.py 文件中安装了该应用程序,但当我 运行
">python manage.py runserver"
它 运行 好的 bt 页面告诉我 url 找不到 404!
这是我的浏览器输出:
我正在使用 Windows 8、python 3 和 django 1.9
这里的 url 永远不会匹配
r'^$/'
$
匹配字符串结尾。这意味着 /
永远不可能跟在它之后。
你可能想要
url(r'^personal/', include('personal.urls')),
您应该从包含 personal.urls
:
urls.py
中删除 $
url(r'^/', include('personal.urls')),
$
说明符将始终匹配字符串的末尾(即您的 URL),并且您将无法包含超出它的任何其他路径。
根据 django docs:
The include() function allows referencing other URLconfs. Note that the regular expressions for the include() function doesn’t have a $ (end-of-string match character) but rather a trailing slash
所以你应该把它改成这样:
url(r'^/', include('personal.urls'))