简体   繁体   English

在Django中访问404错误/运行服务器未返回任何错误

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

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. 当我syncdb和runserver时,一切都可以在Django中正常工作,但是当我尝试访问位于http://127.0.0.1:8000/的网页时,它将返回404错误。

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. 奇怪的是,当我在页面上访问/admin时,它工作正常。 I dont understand what is failing here. 我不明白这里出了什么问题。 Any help would be awesome! 任何帮助都是极好的!

You need an URL route to the homepage. 您需要一个URL路由到主页。 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. MyBlog.urls中的urlpatterns变量应具有一个元组对,例如(r'^ $',app.views.show_homepage),其中show_homepage是views.py中定义的函数。 For more info about the URL dispatcher, you can read about it here: https://docs.djangoproject.com/en/dev/topics/http/urls/ 有关URL调度程序的更多信息,您可以在这里阅读: https : //docs.djangoproject.com/en/dev/topics/http/urls/

Check out chapter 3 of Django's Writing your first Django app tutorial. 查阅Django的第3章编写您的第一个Django应用程序教程。

In short, you need to specify (in urls.py ) which code Django should run for particular URLs; 简而言之,您需要(在urls.py )指定Django应该针对特定URL运行的代码; there are no default URLs defined (you'll see a line including the admin URLs in urls.py ). 没有定义默认网址(您将在urls.py看到包含管理网址的行)。

Edit your urls.py so it looks something like 编辑您的urls.py ,使其看起来像

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 ) (您还需要在TEMPLATE_DIRS指定的目录之一中创建home.html

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

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