404 error in django when visiting / Runserver retu

2019-03-03 18:15发布

When I syncdb and runserver everything works correctly in Django, but when I try to visit the webpage that it is on http://127.0.0.1:8000/ it returns a 404 error.

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Using the URLconf defined in MyBlog.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, , didn't match any of these.

The strange part is that when I visit /admin on the page it works fine. I dont understand what is failing here. Any help would be awesome!

2条回答
贪生不怕死
2楼-- · 2019-03-03 18:29

You need an URL route to the homepage. The urlpatterns variable in MyBlog.urls should have a tuple pair like (r'^$', app.views.show_homepage), where show_homepage is a function defined in views.py. For more info about the URL dispatcher, you can read about it here: https://docs.djangoproject.com/en/dev/topics/http/urls/

查看更多
姐就是有狂的资本
3楼-- · 2019-03-03 18:40

Check out chapter 3 of Django's Writing your first Django app tutorial.

In short, you need to specify (in urls.py) which code Django should run for particular URLs; there are no default URLs defined (you'll see a line including the admin URLs in urls.py).

Edit your urls.py so it looks something like

from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', TemplateView.as_view(template_name="home.html"),
)

(you'll also need to create home.html in one of the directories specified in TEMPLATE_DIRS)

查看更多
登录 后发表回答