如何解决 Django 中的“找不到页面(404)”错误?

问题描述 投票:0回答:7

我的项目名为 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 = '[email protected]'

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

python django http-status-code-404 django-urls django-settings
7个回答
27
投票

您收到 404 错误是因为您尚未为

http://127.0.0.1:8000/
定义 url 模式。

您应该能够在

http://127.0.0.1:8000/admin/
查看管理网站,在
http://127.0.0.1:8000/foodPosts/
查看您的美食帖子。

要为主页添加 url 模式,请取消注释 urls.py 中的以下条目,并将

homefood.views.home
替换为您要使用的视图的路径。

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

3
投票

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

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

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

url('MYAPP', views.index),

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


2
投票

我也遇到了这个错误,解决方案是将相关应用程序的

name=
文件中的
urls.py
参数的双引号更改为单引号!

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

1
投票

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

在我不小心从

path()
路由(django 3.2)中删除尾部斜杠后,我的管理页面全部变成了 404:

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

通过恢复到

'admin/'
可以轻松修复此问题。


0
投票

只需在“用户”末尾添加 / 对我来说很有效 在 urls.py app 文件中 看起来像这样

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

0
投票

我在下面遇到了同样的错误:

找不到页面(404)

因为我没有把

/
放在
test/<int:id>
后面,如下图:

urlpatterns = [     # ↓ Here
    path('test/<int:id>', views.test, name="test")
]

所以,我把

/
放在
test/<int:id>
后面,如下所示,然后错误就解决了:

urlpatterns = [      # ↓ Here
    path('test/<int:id>/', views.test, name="test")
]

0
投票

只有在定义了 NO URL 时,您才会看到登陆页面:

一旦您配置了 URL(例如

foodPosts/
),
/
路线将不会显示登陆页面。

因此您看不到带有此链接的火箭:http://127.0.0.1:8000/

© www.soinside.com 2019 - 2024. All rights reserved.