Django Heroku deployment URLconf error.找不到页面 (404)



我是Django的新手,并试图在Heroku上部署我的Django应用程序。我按照教程做了所有事情,但是当我终于准备好部署我的应用程序时,我收到错误"找不到页面"。

我尝试删除我的 admin.site.urls 周围的 include((,因为它对其他人有效,但它对我不起作用。

这是我 urls.py 文件:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from profiles.views import delete

urlpatterns = [
path(r'^profiles/', include('profiles.urls')),
path(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)

这是我 settings.py 文件:

import os
import django_heroku
import django
os.environ['DJANGO_SETTINGS_MODULE'] = 'firstDjango.settings'
from django.core.wsgi import get_wsgi_application 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY="bb33e5a618aa4f6e549b8207ad1d8fa7cd8d1015e13c8780"
DEBUG = True
ALLOWED_HOSTS = ['ewuradjango.herokuapp.com']

# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#third party
#own 
# 'pages'
'profiles',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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',
'whitenoise.middleware.WhiteNoiseMiddleware',
]
ROOT_URLCONF = 'firstDjango.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media',
],
},
},
]
WSGI_APPLICATION = 'firstDjango.wsgi.application'

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}


AUTH_PASSWORD_VALIDATORS = [
{
'NAME':         'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True


STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
STATIC_TMP = os.path.join(BASE_DIR, 'static')

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
django_heroku.settings(locals())

这是详细错误:

Page not found (404)
Request Method:    GET
Request URL:   https://ewuradjango.herokuapp.com/
Using the URLconf defined in firstDjango.urls, Django tried these URL patterns, in this order:
^profiles/
^admin/
^media/(?P<path>.*)$
The empty path 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.

看起来您还没有为根路径"/"设置路由。尝试访问 https://ewuradjango.herokuapp.com/admin 或 https://ewuradjango.herokuapp.com/profiles,因为您在第一个代码块中设置了它们的路径。

要添加根路径,请尝试如下操作:

urlpatterns = [
path(r'', admin.site.urls),
path(r'^profiles/', include('profiles.urls')),
path(r'^admin/', admin.site.urls),
] + static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)

这应该将您的根路径定向到您的管理页面。

最新更新