简体   繁体   English

Django,找不到页面404

[英]Django,page not found 404

When I start developement server 当我开始开发服务器时

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

Then 然后

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

^admin/
^blog/
The empty path didn't match any of these.

mysite.urls.py mysite.urls.py

urlpatterns = [
 url(r'^admin/', admin.site.urls),
 url(r'^blog/', include('blog.urls')),
]

blog/urls.py 博客/urls.py

pp_name = 'blog'
urlpatterns = [
# post views
url(r'^$', views.post_list, name='post_list'),
url(r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/'\
r'(?P<post>[-\w]+)/$',
views.post_detail,
name='post_detail'),
]

I suspect something is wrong with my templates This is what tree gives 我怀疑我的模板有问题这是树所提供的

tree mysite
mysite
├── blog
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       ├── 0001_initial.cpython-36.pyc
│   │       └── __init__.cpython-36.pyc
│   ├── models.py
│   ├── __pycache__
│   │   ├── admin.cpython-36.pyc
│   │   ├── __init__.cpython-36.pyc
│   │   ├── models.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── views.cpython-36.pyc
│   ├── templates
│   │   └── blog
│   │       ├── base.html
│   │       └── post
│   │           ├── detail.html
│   │           └── list.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── db.sqlite3
├── manage.py
└── mysite
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-36.pyc
    │   ├── settings.cpython-36.pyc
    │   ├── urls.cpython-36.pyc
    │   └── wsgi.cpython-36.pyc
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Your urls not matching the / path in main urls. 您的网址与主网址中的/路径不匹配。 Add some view to url regex r'^$' then it will work. 向url regex r'^$'添加一些视图,然后它将起作用。 For example take a view named home . 例如,以名为home的视图为例。 please import related imports. 请进口相关进口。

from . import views    

urlpatterns = [ 
        url(r'^$', views.home, name='home'),
        url(r'^admin/', admin.site.urls),
        url(r'^blog/', include('blog.urls')),
]

or you can do like 或者你可以喜欢

urlpatterns = [ 
     url(r'^admin/', admin.site.urls),
     url(r'^', include('blog.urls')),
]

To use root site path you should change urlpatterns to this: 要使用根站点路径,应将urlpatterns更改为此:

urlpatterns = [
 url(r'^admin/', admin.site.urls),
 url(r'^', include('blog.urls')),
]

Otherwise you still can get your view calling http://127.0.0.1:8000/blog url. 否则,您仍然可以通过http://127.0.0.1:8000/blog url获得视图。

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

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