Skip to content

Commit 59e3bb8

Browse files
committed
fix(static-files): fix issue with static files on local and production deployments
1 parent 22049cc commit 59e3bb8

File tree

9 files changed

+46
-120
lines changed

9 files changed

+46
-120
lines changed

app.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"environments": {
44
"review": {
55
"addons": [
6-
"heroku-postgresql:hobby-basic"
6+
"heroku-postgresql:hobby-basic",
7+
"heroku-redis:hobby-basic"
78
]
89
}
910
}

compose/local/django/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.8-slim-buster
1+
FROM python:3.8.10-slim-buster
22

33
ENV PYTHONUNBUFFERED 1
44
ENV PYTHONDONTWRITEBYTECODE 1

config/settings/base.py

+32-23
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
"django.middleware.security.SecurityMiddleware",
118118
"corsheaders.middleware.CorsMiddleware",
119119
"csp.middleware.CSPMiddleware",
120+
"whitenoise.middleware.WhiteNoiseMiddleware",
120121
"django.contrib.sessions.middleware.SessionMiddleware",
121122
"django.middleware.locale.LocaleMiddleware",
122123
"django.middleware.common.CommonMiddleware",
@@ -127,6 +128,25 @@
127128
"django.middleware.clickjacking.XFrameOptionsMiddleware",
128129
]
129130

131+
# CSP MIDDLEWARE CONFIGURATION
132+
# ------------------------------------------------------------------------------
133+
CSP_SCRIPT_SRC = [
134+
"'self'",
135+
"https://stackpath.bootstrapcdn.com",
136+
"https://cdn.jsdelivr.net",
137+
"https://code.jquery.com",
138+
"https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js",
139+
"https://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"
140+
]
141+
CSP_STYLE_SRC = [
142+
"'self'",
143+
"https://stackpath.bootstrapcdn.com"
144+
]
145+
CSP_IMG_SRC = [
146+
"'self'",
147+
]
148+
149+
130150
# STATIC
131151
# ------------------------------------------------------------------------------
132152
# https://docs.djangoproject.com/en/dev/ref/settings/#static-root
@@ -154,18 +174,11 @@
154174
TEMPLATES = [
155175
{
156176
# https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-TEMPLATES-BACKEND
157-
"BACKEND": "django.template.backends.django.DjangoTemplates",
177+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
158178
# https://docs.djangoproject.com/en/dev/ref/settings/#template-dirs
159-
"DIRS": [str(APPS_DIR / "templates")],
160-
"OPTIONS": {
161-
# https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders
162-
# https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
163-
"loaders": [
164-
"django.template.loaders.filesystem.Loader",
165-
"django.template.loaders.app_directories.Loader",
166-
],
167-
# https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
168-
"context_processors": [
179+
'DIRS': [(str(APPS_DIR / "templates"))],
180+
'OPTIONS': {
181+
'context_processors': [
169182
"django.template.context_processors.debug",
170183
"django.template.context_processors.request",
171184
"django.contrib.auth.context_processors.auth",
@@ -175,22 +188,18 @@
175188
"django.template.context_processors.tz",
176189
"django.contrib.messages.context_processors.messages",
177190
"tmh_registry.utils.context_processors.settings_context",
191+
'csp.context_processors.nonce'
192+
],
193+
# https://docs.djangoproject.com/en/dev/ref/settings/#template-loaders
194+
# https://docs.djangoproject.com/en/dev/ref/templates/api/#loader-types
195+
'loaders': [
196+
'django.template.loaders.filesystem.Loader',
197+
'django.template.loaders.app_directories.Loader'
178198
],
179199
},
180-
}
200+
},
181201
]
182202

183-
# https://docs.djangoproject.com/en/dev/ref/settings/#form-renderer
184-
FORM_RENDERER = "django.forms.renderers.TemplatesSetting"
185-
186-
# http://django-crispy-forms.readthedocs.io/en/latest/install.html#template-packs
187-
CRISPY_TEMPLATE_PACK = "bootstrap4"
188-
189-
# FIXTURES
190-
# ------------------------------------------------------------------------------
191-
# https://docs.djangoproject.com/en/dev/ref/settings/#fixture-dirs
192-
FIXTURE_DIRS = (str(APPS_DIR / "fixtures"),)
193-
194203
# SECURITY
195204
# ------------------------------------------------------------------------------
196205
# https://docs.djangoproject.com/en/dev/ref/settings/#session-cookie-httponly

config/settings/production.py

+8-46
Original file line numberDiff line numberDiff line change
@@ -55,39 +55,6 @@
5555
"DJANGO_SECURE_CONTENT_TYPE_NOSNIFF", default=True
5656
)
5757

58-
# # STORAGES
59-
# # ------------------------------------------------------------------------------
60-
# # https://django-storages.readthedocs.io/en/latest/#installation
61-
# INSTALLED_APPS += ["storages"] # noqa F405
62-
# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
63-
# AWS_ACCESS_KEY_ID = env("AWS_ACCESS_KEY_ID")
64-
# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
65-
# AWS_SECRET_ACCESS_KEY = env("AWS_SECRET_ACCESS_KEY")
66-
# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
67-
# AWS_STORAGE_BUCKET_NAME = env("AWS_STORAGE_BUCKET_NAME")
68-
# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
69-
# AWS_QUERYSTRING_AUTH = False
70-
# # DO NOT change these unless you know what you're doing.
71-
# _AWS_EXPIRY = 60 * 60 * 24 * 7
72-
# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
73-
# AWS_S3_OBJECT_PARAMETERS = {
74-
# "CacheControl": f"max-age={_AWS_EXPIRY}, s-maxage={_AWS_EXPIRY}, must-revalidate"
75-
# }
76-
# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#settings
77-
# AWS_S3_REGION_NAME = env("AWS_DEFAULT_REGION", default=None)
78-
# # https://django-storages.readthedocs.io/en/latest/backends/amazon-S3.html#cloudfront
79-
# AWS_S3_CUSTOM_DOMAIN = env("AWS_S3_CUSTOM_DOMAIN", default=None)
80-
# aws_s3_domain = AWS_S3_CUSTOM_DOMAIN or f"{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com"
81-
# # STATIC
82-
# # ------------------------
83-
# STATICFILES_STORAGE = "tmh_registry.utils.storages.StaticRootS3Boto3Storage"
84-
# COLLECTFAST_STRATEGY = "collectfast.strategies.boto3.Boto3Strategy"
85-
# STATIC_URL = f"https://{aws_s3_domain}/static/"
86-
# # MEDIA
87-
# # ------------------------------------------------------------------------------
88-
# DEFAULT_FILE_STORAGE = "tmh_registry.utils.storages.MediaRootS3Boto3Storage"
89-
# MEDIA_URL = f"https://{aws_s3_domain}/media/"
90-
9158
# TEMPLATES
9259
# ------------------------------------------------------------------------------
9360
# https://docs.djangoproject.com/en/dev/ref/settings/#templates
@@ -119,20 +86,15 @@
11986
# Django Admin URL regex.
12087
ADMIN_URL = env("DJANGO_ADMIN_URL")
12188

122-
# Anymail
89+
# EMAIL
12390
# ------------------------------------------------------------------------------
124-
# https://anymail.readthedocs.io/en/stable/installation/#installing-anymail
125-
INSTALLED_APPS += ["anymail"] # noqa F405
12691
# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
127-
# https://anymail.readthedocs.io/en/stable/installation/#anymail-settings-reference
128-
# https://anymail.readthedocs.io/en/stable/esps/amazon_ses/
129-
EMAIL_BACKEND = "anymail.backends.amazon_ses.EmailBackend"
130-
ANYMAIL = {}
131-
132-
# Collectfast
133-
# ------------------------------------------------------------------------------
134-
# https://github.com/antonagestam/collectfast#installation
135-
INSTALLED_APPS = ["collectfast"] + INSTALLED_APPS # noqa F405
92+
EMAIL_BACKEND = env(
93+
"DJANGO_EMAIL_BACKEND",
94+
default="django.core.mail.backends.smtp.EmailBackend"
95+
)
96+
# https://docs.djangoproject.com/en/dev/ref/settings/#email-timeout
97+
EMAIL_TIMEOUT = 5
13698

13799
# LOGGING
138100
# ------------------------------------------------------------------------------
@@ -212,4 +174,4 @@
212174

213175
# Datadog
214176
# ------------------------------------------------------------------------------
215-
INSTALLED_APPS += ["ddtrace.contrib.django"]
177+
INSTALLED_APPS += ["ddtrace.contrib.django"] # noqa F405

requirements/base.txt

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ django-redis==4.12.1 # https://github.com/jazzband/django-redis
1717
django-timezone-field==4.0 # https://github.com/mfogel/django-timezone-field/
1818
django-cors-headers==3.5.0 # https://github.com/adamchainz/django-cors-headers
1919
django-csp==3.7 # https://github.com/mozilla/django-csp
20+
whitenoise==5.2.0 # https://github.com/evansd/whitenoise
21+
2022
# Django REST Framework
2123
djangorestframework==3.12.2 # https://github.com/encode/django-rest-framework
2224
drf-yasg==1.20.0 # https://github.com/axnsan12/drf-yasg

runtime.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
python-3.8.7
1+
python-3.8.10

tmh_registry/static/css/project.css

-13
This file was deleted.

tmh_registry/static/js/project.js

-1
This file was deleted.

tmh_registry/static/sass/project.scss

-34
This file was deleted.

0 commit comments

Comments
 (0)