所以基本上我有一个Django项目存储在一个名为project的文件夹中,其中我有两个文件夹munbase和munbase
munbase包含我的设置.py assgi.py wsgi.py等等。相同的设置.py看起来像这样:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
# SECURITYWARNING: keep the secret key used in production secret!
SECRET_KEY = 'some secret key'
# SECURITYWARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['0.0.0.0'] #ip adress redacted for obv reasons
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'MUNbase'
]
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',
]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
]
ROOT_URLCONF = 'munbase.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'munbase.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/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.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
这个文件夹有一个urls.py作为:
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static # to import static in deployment
from django.conf import settings # to import static in deployment
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("MUNbase.urls"))
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
我的文件夹MUNbase(包含大部分实际代码(有一个类似于的urls.py文件
from django.contrib import admin
from django.urls import path, include
from . import views
from django.conf import settings # to import static in deployment
from django.conf.urls.static import static # to import static in deployment
urlpatterns = [
#paths demarcated with commas here
]
handler404 = 'MUNbase.views.error_404_view'
尽管我尝试了多种配置,并在部署过程中多次运行collectstatic,但我无法加载静态资源,这些资源保存在以下文件夹中:
.../PROJECT/static
解决这个问题的任何帮助都将是一个很大的帮助。仅供参考,我在AWS lightsail 上使用Linux bitnami服务器
- 如果您想从s3存储桶中托管静态文件,请参阅如何从s3存储库中为静态文件提供服务器
- 如果你不想使用s3 bucket,你可以使用whiteoise从你自己的应用程序中提供静态文件
- 查看设置文件并根据需要进行修改
- 希望,它对你有帮助。如果您对此有任何疑问,请发表评论