gpt4 book ai didi

python - Django 网址给出 404 错误

转载 作者:太空宇宙 更新时间:2023-11-03 13:44:36 24 4
gpt4 key购买 nike

每当我尝试访问我网站上的“blog/”时,Django 都会给我一个 404 错误,但我已经定义了我想要的 URL,它们应该匹配。

主要 urls.py:

from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()
from blog import views

urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mySiteProject.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),

url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
)

博客.urls.py:

from django.conf.urls import patterns,url
from blog import views

urlpatterns = patterns(
url(r'^$',views.index,name='index')
)

404页面:

Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/blog/

Using the URLconf defined in mySiteProject.urls, Django tried these URL patterns, in this order:

^admin/

The current URL, blog/, didn't match any of these.

网站结构:

mySiteProject
blog
admin.py
models.py
tests.py
views.py
urls.py
__init__.py
mySiteProject
wsgi.py
settings.py
urls.py
__init__.py
manage.py
db.sqlite3

已安装的应用:

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'blog'
)

最佳答案

patterns 需要一个前缀作为其第一个参数,后跟零个或多个参数。所以这个:

urlpatterns = patterns(url(r'^$',views.index,name='index'))  # won't work

blog.urls.py 中应该如下所示:

urlpatterns = patterns('', url(r'^$', views.index, name='index'))  # now has a prefix as first argument

在当前状态下,blog.urls.py 中的patterns 函数将返回一个空的pattern_list,这意味着 url(r'^blog/', include('blog.urls')) 将不返回任何模式。

关于python - Django 网址给出 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22901474/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com