简体   繁体   English

Django网址给出了404错误

[英]Django urls giving 404 error

Django is giving me a 404 error whenever I try to access "blog/" on my site, but I've defined the URLs I want and they should be matching that. 每当我尝试访问我的网站上的“blog /”时,Django都会给我一个404错误,但我已经定义了我想要的URL,他们应该匹配它。

Main urls.py: 主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)),
)

blog.urls.py: blog.urls.py:

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

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

404 page: 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.

Site structure: 场地结构:

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: 已安装的应用:

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

    'blog'
)

patterns requires a prefix as its first argument followed by zero or more arguments. patterns需要前缀作为其第一个参数,后跟零个或多个参数。 So this: 所以这:

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

in blog.urls.py should look like this: blog.urls.py应该如下所示:

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

In its present state, the patterns function in blog.urls.py will return an empty pattern_list , which means that url(r'^blog/', include('blog.urls')) will return no patterns. 在其目前的状态下, patterns在功能blog.urls.py将返回一个空pattern_list ,这意味着url(r'^blog/', include('blog.urls'))将不返回图案。

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

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