Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
服务器错误(500)访问/admin/with DEBUG=False,前端反应,后端Django(Django rest框架)-Heroku_Heroku_Django Rest Framework_Django Admin_Server Error_Whitenoise - Fatal编程技术网

服务器错误(500)访问/admin/with DEBUG=False,前端反应,后端Django(Django rest框架)-Heroku

服务器错误(500)访问/admin/with DEBUG=False,前端反应,后端Django(Django rest框架)-Heroku,heroku,django-rest-framework,django-admin,server-error,whitenoise,Heroku,Django Rest Framework,Django Admin,Server Error,Whitenoise,我有一个应用程序,在前端和后端使用Django rest框架使用ReactJs 该应用程序在Debug=True时工作正常,但当我部署并设置Debug=False时,我可以看到该应用程序工作正常,但是我无法访问管理面板,因为我收到服务器错误(500)。同样奇怪的是,应用程序正确地从API获取数据,但如果我尝试直接访问API,我也会得到服务器错误(500) 我正在使用WhiteNoise处理静态文件,下面是我的代码。在部署之前,我尝试了不同的选择。。在本地执行npm run build和colle

我有一个应用程序,在前端和后端使用Django rest框架使用ReactJs

该应用程序在Debug=True时工作正常,但当我部署并设置Debug=False时,我可以看到该应用程序工作正常,但是我无法访问管理面板,因为我收到服务器错误(500)。同样奇怪的是,应用程序正确地从API获取数据,但如果我尝试直接访问API,我也会得到服务器错误(500)

我正在使用WhiteNoise处理静态文件,下面是我的代码。在部署之前,我尝试了不同的选择。。在本地执行npm run build和collectstatic(在Heroku中禁用\u collectstatic=1),我还尝试将“postinstall”:“npm run build”添加到package.json文件中,在Heroku中禁用\u collectstatic=0

有人知道我的代码有什么问题吗?见下面我的代码

设置.py

import os
import dj_database_url
from pathlib import Path

import cloudinary
import cloudinary.uploader
import cloudinary.api

# if working locally import env.py file
try:
    import env
except ImportError:
    pass


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY =  os.environ.get("SECRET_KEY")

DEBUG = False

ALLOWED_HOSTS = ['127.0.0.1', 'myappname.herokuapp.com']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'whitenoise.runserver_nostatic',
    'rest_framework',
    'corsheaders',
    'admin_reorder',
    'inicio',
    'sobre_mi',
    'actividades',
    'galeria',
    'contacto',
    'colabora',
    'footer',
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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',
    'admin_reorder.middleware.ModelAdminReorder',
]

ROOT_URLCONF = 'myappname.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'build')],        
        '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',
            ],
        },
    },
]

WSGI_APPLICATION = 'myappname.wsgi.application'

DATABASES = {
        'default': dj_database_url.parse(os.environ.get('DATABASE_URL'))
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

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',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'es-es'

TIME_ZONE = 'Europe/Madrid'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

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

STATICFILES_STORAGE = (
    'whitenoise.storage.CompressedManifestStaticFilesStorage')

WHITENOISE_ROOT = os.path.join(BASE_DIR, 'build')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticatedOrReadOnly'
    ]
}

CORS_ORIGIN_ALLOW_ALL = True

cloudinary.config( 
  cloud_name = os.environ.get("CLOUD_NAME"), 
  api_key = os.environ.get("API_KEY"), 
  api_secret = os.environ.get("API_SECRET") 
)

ADMIN_REORDER = (
    'auth',
    'users',
    'inicio',
    'sobre_mi',
    'actividades',
    'galeria',
    'colabora',
    'contacto',
    'footer'
)

url.py

from django.contrib import admin
from django.urls import path, include, re_path
from django.views.generic import TemplateView
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api-auth/', include('rest_framework.urls')),
    path('api/inicio/', include('inicio.api.urls')),
    path('api/sobremi/', include('sobre_mi.api.urls')),
    path('api/actividades/', include('actividades.api.urls')),
    path('api/galeria/', include('galeria.api.urls')),
    path('api/colabora/', include('colabora.api.urls')),
    path('api/footer/', include('footer.api.urls')),
    path('api/contacto/', include('contacto.api.urls'))
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
# urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


# this will handle any other routes (React-Router-Dom )
urlpatterns += [re_path(r'^.*', TemplateView.as_view(template_name='index.html'))]
package.json

{
  "name": "frontend",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.5",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "aos": "^2.3.4",
    "django-react-csrftoken": "^1.0.4",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-icons": "^3.11.0",
    "react-router-dom": "^5.2.0",
    "react-scripts": "4.0.0",
    "web-vitals": "^0.2.4",
    "zepto-browserify": "^1.1.6-1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "postinstall": "npm run build"
  },
  "engines": {
    "node": "12.16.2",
    "npm": "6.14.4"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}
程序文件

release: python manage.py migrate
web: gunicorn myappname.wsgi --log-file -