404 error in django when visiting / Runserver returns no errors though

£可爱£侵袭症+ 提交于 2019-12-20 06:22:21

问题


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!


回答1:


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/




回答2:


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)



来源:https://stackoverflow.com/questions/11832178/404-error-in-django-when-visiting-runserver-returns-no-errors-though

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!