【问题标题】:Django Heroku deployment URLconf error. Page not found (404)Django Heroku 部署 URLconf 错误。找不到页面 (404)
【发布时间】:2019-08-14 17:15:26
【问题描述】:

我是 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.

【问题讨论】:

  • 在部署之前您是否在本地尝试过?正如错误所说,您没有根页面的路径,所以这在任何地方都行不通;您的问题与 Heroku 无关。
  • @DanielRoseman 谢谢。我现在得到了。问题是根路径

标签: django url heroku


【解决方案1】:

看起来您还没有为根路径“/”设置路由。尝试访问 https://ewuradjango.herokuapp.com/adminhttps://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)

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

【讨论】:

  • 谢谢。我现在得到了。这就是问题@StevensQiu
猜你喜欢
  • 2020-02-17
  • 1970-01-01
  • 2013-12-04
  • 1970-01-01
  • 1970-01-01
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多