Simple Django configuration to serve a single-page app
Project description
django-spa
Simple Django configuration to serve a single-page app (SPA).
The following SPA settings are handled in django-spa:
index.html served on /
all /static/... files served on /...
Django’s urls still work (Django admin, templates, Django REST framework APIs)
everything else goes to / for frontend routing (e.g. react-router)
Usage
First set up WhiteNoise, as django-spa overrides some of its functionality.
Add django-spa to your requirements.txt:
django-spa
Update settings.py with the django-spa middleware:
MIDDLEWARE = [ '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', 'spa.middleware.SPAMiddleware', ]
Use the django-spa static file storage:
STATICFILES_STORAGE = 'spa.storage.SPAStaticFilesStorage'
You should be good to go!
Credits
Used some parts of the solution suggested in this WhiteNoise issue for serving index.html on /. This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.
License
History
0.2.0 (2017-09-01)
Fix #3
0.1.1 (2017-04-08)
Metadata fixes.
0.1.0 (2017-04-08)
First release on PyPI.
Initial django-spa middleware.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_spa-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04ea7876ae734a338f0839b4e1b6d3ded90a6c566c62375845a8bd74ce6e6ed3 |
|
MD5 | a54cbb3bb1ee8e602478453170448438 |
|
BLAKE2b-256 | 7f961b6ba9d3e2d81659870e3d26a64c3b36ea4ac05d757ee2af3a825bcd49d0 |