django 404错误-页面未找到

j2qf4p5b  于 2022-12-24  发布在  Go
关注(0)|答案(5)|浏览(166)

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

我urls.py的家庭食品文件夹里的www.example.com是

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,但我不确定。任何帮助将不胜感激。谢谢。

06odsfpq

06odsfpq1#

您得到404是因为您还没有为http://127.0.0.1:8000/定义url模式。
你应该可以在http://127.0.0.1:8000/admin/查看管理站点,在http://127.0.0.1:8000/foodPosts/查看你的食物帖子。
要为主页添加url模式,请取消注解www.example.com中的以下条目urls.py,并将homefood.views.home替换为要使用的视图的路径。

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

pkbketx92#

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

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

在应用程序urls.py中添加以下内容

url('MYAPP', views.index),

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

r1zk6ea1

r1zk6ea13#

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

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

d8tt03nd4#

与OP不直接相关,但可能对其他人有用:
在我不小心删除了path()路由(django 3.2)中的斜杠后,我的管理页面都变成了404:

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

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

9wbgstp7

9wbgstp75#

我的工作是在urls.py应用程序文件中的“用户”末尾添加/,如下所示

path('users/', get_users, name='users')

相关问题