找不到Django 404错误页面

时间:2013-11-20 17:03:00

标签: python django http-status-code-404 django-urls

我的项目名为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.

我的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.clickjacking.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 = 'testing@example.com'

STATIC_URL = '/static/'


STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),

)                                           #static asset configuration

和我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)),


 )

我认为我的问题出在我的urls.py或者我的settings.py中,但我不确定。任何帮助将不胜感激。感谢。

4 个答案:

答案 0 :(得分:20)

您正在获取404,因为您还没有为http://127.0.0.1:8000/定义网址模式。

您应该可以在http://127.0.0.1:8000/admin/查看管理网站,在http://127.0.0.1:8000/foodPosts/查看您的食物信息。

要为首页添加网址格式,请在urls.py中取消注释以下条目,并将homefood.views.home替换为您要使用的视图的路径。

url(r'^$', 'homefood.views.home', name='home'),

答案 1 :(得分:0)

基本上的答案是在项目 urls.py 文件中添加一个条目作为空白示例:

path('', include('MYAPP.urls')),

并在应用程序 urls.py 中添加

url('MYAPP', views.index),

确保在 settings.py 中包含您的应用程序,并确保在应用程序 urls.py 中导入视图

答案 2 :(得分:0)

我也遇到此错误,解决方案是将相关应用的name=文件中urls.py参数的双引号更改为单引号!

path('register', views.register, name='register')

答案 3 :(得分:0)

与 OP 不直接相关,但可能对其他人有用:

在我不小心从 path() 路由 (django 3.2) 中删除了尾部斜杠后,我的管理页面都变成了 404:

urlpatterns = [
    path('admin', admin.site.urls),  # trailing slash missing...
    path('', include('myapp.urls'))
]

通过恢复到 'admin/' 可以轻松解决此问题。