找不到Django 404错误页

2024-10-17 06:31:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我的项目名为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文件夹中的myurl.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)),


 )

我想我的问题不是在我的url.py中,就是在我的settings.py中,但我不确定。任何帮助都将不胜感激。谢谢。


Tags: thepathdjangointrueurladminos
2条回答

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

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

在应用程序中添加

url('MYAPP', views.index),

确保在settings.py中包含应用程序也确保在应用程序url.py中导入视图

您得到404是因为您还没有为http://127.0.0.1:8000/定义url模式。

您应该可以在http://127.0.0.1:8000/admin/查看管理站点,并在http://127.0.0.1:8000/foodPosts/查看食物帖子。

要为主页添加url模式,请在url.py中取消注释以下条目,并用要使用的视图路径替换homefood.views.home

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

相关问题 更多 >