File tree Expand file tree Collapse file tree 3 files changed +33
-7
lines changed
appengine/standard_python3/django Expand file tree Collapse file tree 3 files changed +33
-7
lines changed Original file line number Diff line number Diff line change 1818# [START gaestd_py_django_app_yaml]
1919runtime : python39
2020
21+ env_variables :
22+ # This setting is used in settings.py to configure your ALLOWED_HOSTS
23+ # APPENGINE_URL: https://PROJECT_ID.uc.r.appspot.com
24+
2125handlers :
2226# This configures Google App Engine to serve the files in the app's static
2327# directory.
Original file line number Diff line number Diff line change 1414
1515import io
1616import os
17+ from urllib .parse import urlparse
1718
1819import environ
1920from google .cloud import secretmanager
5960# Change this to "False" when you are ready for production
6061DEBUG = env ("DEBUG" )
6162
62- # SECURITY WARNING: App Engine's security features ensure that it is safe to
63- # have ALLOWED_HOSTS = ['*'] when the app is deployed. If you deploy a Django
64- # app not on App Engine, make sure to set an appropriate host here.
65- ALLOWED_HOSTS = ["*" ]
63+
64+ # [START gaestd_py_django_csrf]
65+ # SECURITY WARNING: It's recommended that you use this when
66+ # running in production. The URL will be known once you first deploy
67+ # to App Engine. This code takes the URL and converts it to both these settings formats.
68+ APPENGINE_URL = env ("APPENGINE_URL" , default = None )
69+ if APPENGINE_URL :
70+ # Ensure the HTTPS is in the URL before it's used.
71+ APPENGINE_URL = urlparse (APPENGINE_URL , "https" ).geturl ()
72+
73+ ALLOWED_HOSTS = [APPENGINE_URL ]
74+ CSRF_TRUSTED_ORIGINS = [urlparse (APPENGINE_URL ).netloc ]
75+ SECURE_SSL_REDIRECT = True
76+ else :
77+ ALLOWED_HOSTS = ["*" ]
78+ # [END gaestd_py_django_csrf]
6679
6780# Application definition
6881
Original file line number Diff line number Diff line change 1414
1515import io
1616import os
17+ from urllib .parse import urlparse
1718
1819import environ
1920import google .auth
6566
6667DEBUG = env ("DEBUG" )
6768
68- # SECURITY WARNING: It's recommended that you change this setting when
69+ # [START cloudrun_django_csrf]
70+ # SECURITY WARNING: It's recommended that you use this when
6971# running in production. The URL will be known once you first deploy
70- # to Cloud Run.
71- ALLOWED_HOSTS = ["*" ]
72+ # to Cloud Run. This code takes the URL and converts it to both these settings formats.
73+ CLOUDRUN_SERVICE_URL = env ("CLOUDRUN_SERVICE_URL" , default = None )
74+ if CLOUDRUN_SERVICE_URL :
75+ ALLOWED_HOSTS = [urlparse (CLOUDRUN_SERVICE_URL ).netloc ]
76+ CSRF_TRUSTED_ORIGINS = [CLOUDRUN_SERVICE_URL ]
77+ SECURE_SSL_REDIRECT = True
78+ else :
79+ ALLOWED_HOSTS = ["*" ]
80+ # [END cloudrun_django_csrf]
7281
7382# Application definition
7483
You can’t perform that action at this time.
0 commit comments