Skip to content

Commit 3e9427c

Browse files
committed
Add information about serving static files
Due to the process of updating the guides, missing information about serving static files on our platform needs to be added. Therefore, add a section about serving static files and small examples to adjust the applications.
1 parent 8a50b66 commit 3e9427c

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

Guides/Python/Django-notes.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,70 @@ To use a database, you should choose an Add-on from [the Data Storage category][
2525
## Email
2626
You can't use a local SMTP server, instead choose one of our [email Add-ons][messaging-addons].
2727

28+
## Serving Static Files
29+
30+
Since there is no webserver , i.e. Apache or Nginx running inside the container
31+
to serve the static files. We recommend to use[dj-static][dj-static] or [whitenoise][whitenoise] in combination with a WSGI
32+
server like Gunicorn. Another approach is using a CDN, e.g. Amazon S3 to serve static files for
33+
your application with [django-storage][django-storage].
34+
For further details, checkout the official package documentations.
35+
36+
The following code snippets showing how to use dj-static or whitenoise:
37+
38+
### dj-static
39+
40+
- Add the following line to the requirement.txt:
41+
42+
~~~python
43+
dj-static==0.0.6
44+
~~~
45+
46+
- Modify your settings.py:
47+
48+
~~~python
49+
STATIC_ROOT = 'staticfiles'
50+
STATIC_URL = '/static/'
51+
~~~
52+
53+
- Update your wsgi.py with the following line:
54+
55+
~~~python
56+
from django.core.wsgi import get_wsgi_application
57+
from dj_static import Cling
58+
59+
application = Cling(get_wsgi_application())
60+
~~~
61+
62+
Please make sure that your files getting served properly, by adding an empty
63+
dummy text file in the static folder of your repository, for testing purposes.
64+
65+
66+
### whitenoise
67+
68+
- Add the following line to the requirement.txt:
69+
70+
~~~python
71+
whitenoise==2.0.3
72+
~~~
73+
74+
- Modify your settings.py:
75+
76+
~~~python
77+
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
78+
~~~
79+
80+
- Update your wsgi.py with the following line:
81+
82+
~~~python
83+
from django.core.wsgi import get_wsgi_application
84+
from whitenoise.django import DjangoWhiteNoise
85+
86+
application = get_wsgi_application()
87+
application = DjangoWhiteNoise(application)
88+
~~~
89+
90+
91+
2892
[SSH-session]: https://www.cloudcontrol.com/dev-center/platform-documentation#secure-shell-(ssh)
2993
[python buildpack]: https://github.com/cloudControl/buildpack-python
3094
[pip]: http://www.pip-installer.org/
@@ -34,3 +98,6 @@ You can't use a local SMTP server, instead choose one of our [email Add-ons][mes
3498
[add-on-credentials]: https://www.cloudcontrol.com/dev-center/guides/python/add-on-credentials
3599
[cloudControl]: https://www.cloudcontrol.com/
36100
[worker]: https://www.cloudcontrol.com/dev-center/platform-documentation#scheduled-jobs-and-background-workers
101+
[dj-static]: https://github.com/kennethreitz/dj-static
102+
[whitenoise]: https://warehouse.python.org/project/whitenoise/
103+
[django-storage]: https://django-storages.readthedocs.org/en/latest/

0 commit comments

Comments
 (0)