I am attempting to build a simple single-page web app where a web server just serves static content, unless a route begins with "/api" - in which case requests are routed to django.
File structure like so:
ROOT
- manage.py
- static/ (static content)
- index.html
- app.min.js
- ...etc
- djangoapp/ (django "project")
- __init__.py
- settings.py
- urls.py
- wsgi.py
- api/ (django "app")
- __init__.py
- admin.py
- apps.py
- models.py
- tests.py
- views.py
In development (DEBUG=true), I would like django to serve the static content as a traditional web server would. That means content not under /api should just be served from /static, and a GET for '/' should return index.html.
I have taken these steps:
- created the root django "project" with
django-admin startproject djangoapp - created a django "app" with
./manage.py startapp api - created a folder, static, with a dummy index.html and dummy.html.
- altered the django "app" to create a simple "hello I am the api" endpoint, at /api/hello, and created the appropriate routes
From this point, I can successfully set up a docker-compose cluster to perform as expected - calls to the nginx edge for '/' load the static index.html in ROOT/static/index.html, calls to '/api/hello' are proxied to a uwgi server and return accordingly. However, I cannot for the life of me get django to do this from its ./manage.py runserver
I quickly found a guide to do this from Django itself here: https://docs.djangoproject.com/en/1.10/howto/static-files/
However, none of the suggestions listed on that doc actually work.
setting
- 'STATIC_URL' to '/'
- 'STATIC_ROOT' to the project's on-disk physical root via
os.path.join(BASE_DIR, "static")
...does not alter the behavior in any way - requests to a static page like GET /dummy.html do not route
while altering the project's urls.py to include the result of...
django.conf.urls.static.static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
...also does not alter behavior in any way. Just 404s.
I found that by omitting a STATIC_ROOT value, and setting a STATICFILES_DIRS value to the physical on-disk document root would successfully serve static files - but break all other endpoints (they throw 404).
Environment:
- Debian jessie (stable)
- python 2.7.13 (installed via pyenv)
- django 1.10.6 (installed via pip)
/api/urls? – Håken Lid Mar 31 '17 at 15:38