Skip to main content

Serve SPAs with Django

Project description

Django serve SPA

Serve SPAs with Django

Quick start

pip install dj-kaos-spa

Build your SPA and copy the contents of the build into SPA_ROOT (e.g. path/to/django/spa/).

# settings.py
SPA_ROOT = BASE_DIR / 'spa'  # where the built SPA code will live
SPA_URL = ''  # To serve from /

# urls.py
from kaos_spa import settings as spa_settings

urlpatterns += [
    path(spa_settings.SPA_URL, include('kaos_spa.urls')),
]

Runserver, and visit SPA_URL (/ by default). Your SPA will show up.

Notes

dj-kaos-spa is meant to be used for development and isn't recommended for production use. For production, serve the SPA on its own using tools like npm serve.

dj-kaos-spa is mostly tested with create-react-app apps, but in theory should work with any SPA.

Utility scripts

dj-kaos-spa comes bundled with some utility scripts that makes building and copying your SPA project much more convenient.

build-spa

Build your SPA and put it in your django project.

Set the following environment variables

# required
SPA_SRC_DIR=/path/to/your/spa/
SPA_IN_DJANGO_DIR=/path/to/your/django/project/spa/  # what the SPA_ROOT django setting refers to

# optional
SPA_URL  # If you are not serving on /
SPA_BUILD_CMD="npm run build"
SPA_BUILD_DIR_PATH='build/'  # Where the build command builds the app in its src directory

run

build-spa

It'll build your SPA and put it in the right folder in your django project.

cm-spa-build

Commits the new build into Git with commit message from arg. If no arg is supplied, commit message defaults to "New SPA build".

Set the following environment variables

# required
SPA_IN_DJANGO_DIR=/path/to/your/django/project/spa/  # what the SPA_ROOT django setting refers to

run

cm-spa-build

Development and Testing

IDE Setup

Add the example directory to the PYTHONPATH in your IDE to avoid seeing import warnings in the tests modules. If you are using PyCharm, this is already set up.

Running the Tests

Install requirements

pip install -r requirements.txt

For local environment

pytest

For all supported environments

tox

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

django-serve-spa-0.1.0a4.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

django_serve_spa-0.1.0a4-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file django-serve-spa-0.1.0a4.tar.gz.

File metadata

  • Download URL: django-serve-spa-0.1.0a4.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.9

File hashes

Hashes for django-serve-spa-0.1.0a4.tar.gz
Algorithm Hash digest
SHA256 2f612b15a757cdeae2b95cd2140de3b92bcfc246d3cf64addbea9d78f63c8327
MD5 0ec5ff1f7af33db325010fdea94de591
BLAKE2b-256 e7ea220bce4e24b3a61384874964be81bdd417b6937ec90f9085fdc2f48dbd5d

See more details on using hashes here.

File details

Details for the file django_serve_spa-0.1.0a4-py3-none-any.whl.

File metadata

  • Download URL: django_serve_spa-0.1.0a4-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.9

File hashes

Hashes for django_serve_spa-0.1.0a4-py3-none-any.whl
Algorithm Hash digest
SHA256 d145a492aabecf103135fa94f0abb99f286b3b375506fc904cbdea9f62728c90
MD5 eae6f4379b41c5023826ec5cc0b5af45
BLAKE2b-256 7aaba2d6cd9a513b1559b53f8120142a6efe9bc294bc240265b7a13cee355fd4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page