如何解决Django中的“Page not found(404)”错误?

rt4zxlrg  于 2023-05-19  发布在  Go
关注(0)|答案(6)|浏览(952)

我的项目名为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的homefood文件夹中的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,但我不确定。任何帮助将不胜感激。谢谢。

xn1cxnb4

xn1cxnb41#

你得到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'),
jgovgodb

jgovgodb2#

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

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

然后在appurls.py中添加这个

url('MYAPP', views.index),

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

rxztt3cl

rxztt3cl3#

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

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

cnh2zyt34#

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

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

这很容易通过恢复到'admin/'来修复。

r6hnlfcb

r6hnlfcb5#

我只需要在urls. pyapp文件中的'users'末尾添加/就可以了,看起来像这样

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

clj7thdc6#

下面是同样的错误:
页面未找到(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")
]

相关问题