Python 未找到 Django 404 错误页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20102227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django 404 error-page not found
提问by user3014093
My project is named homefood, and when I runserver I get this error.Anybody have any clue how to fix this error.
我的项目名为 homefood,当我运行服务器时出现此错误。任何人都知道如何修复此错误。
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/
Using the URLconf defined in homefood.urls, Django tried these URL patterns, in this order:
^foodPosts/
^admin/
The current URL, , didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
My settings.py file looks like this...
我的 settings.py 文件看起来像这样......
import dj_database_url
"""
Django settings for homefood project.
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_ROOT = 'staticfiles'
# SECURITY WARNING: keep the secret key used in production secret!
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
)
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = ['*'] # Allow all host headers
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
'django.contrib.sites',
'foodPosts',
'registration',
'profiles',
'homefood',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickHymaning.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'homefood.urls'
WSGI_APPLICATION = 'homefood.wsgi.application'
#= dj_database_url.config()
#'default': dj_database_url.config(default='mysql://localhost')}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_db',
'USER': 'root',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
}#= dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/New_York'
USE_I18N = True
USE_L10N = True
USE_TZ = True
#Django-registration additions, for account registration
ACCOUNT_ACTIVATION_DAYS=7
EMAIL_HOST = 'localhost'
EMAIL_PORT = 102
EMAIL_HOST_USERNAME = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
DEFAULT_FROM_EMAIL = '[email protected]'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
) #static asset configuration
and my urls.py in my homefood folder is
我的 homefood 文件夹中的 urls.py 是
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'homefood.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^foodPosts/',include('foodPosts.urls')),
url(r'^admin/', include(admin.site.urls)),
)
I think my problem is either in my urls.py, or my settings.py but I am unsure. Any help would be greatly appreciated. Thanks.
我认为我的问题出在我的 urls.py 或我的 settings.py 中,但我不确定。任何帮助将不胜感激。谢谢。
采纳答案by Alasdair
You are getting the 404 because you haven't defined a url pattern for http://127.0.0.1:8000/yet.
您收到 404 是因为您还没有定义 url 模式http://127.0.0.1:8000/。
You should be able to view the admin site at http://127.0.0.1:8000/admin/and your food posts at http://127.0.0.1:8000/foodPosts/.
您应该能够在 上查看管理站点http://127.0.0.1:8000/admin/和在上查看您的食物帖子http://127.0.0.1:8000/foodPosts/。
To add a url pattern for the homepage, uncomment the following entry in your urls.py, and replace homefood.views.homewith the path to the view you want to use.
要为主页添加 url 模式,请取消注释 urls.py 中的以下条目,并替换homefood.views.home为您要使用的视图的路径。
url(r'^$', 'homefood.views.home', name='home'),
回答by Fernando Silva
Basically the answer to this to add a entry in the project urls.pyfile as blank example:
基本上答案是在项目urls.py文件中添加一个条目作为空白示例:
path('', include('MYAPP.urls')),
and in the app urls.pyyou add this
并在应用程序urls.py 中添加此
url('MYAPP', views.index),
make sure in the settings.pyyou include your app also make sure in the app urls.pyyou import your views
确保在settings.py 中包含您的应用程序还确保在应用程序urls.py 中导入您的视图

