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:

  1. created the root django "project" with django-admin startproject djangoapp
  2. created a django "app" with ./manage.py startapp api
  3. created a folder, static, with a dummy index.html and dummy.html.
  4. 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)

The core issue seems to surround the default INSTALLED_APP entry, django.contrib.staticfiles. Disabling it in settings.py was the solution.


Step by step instructions to building a "/api only" django 1.10 that will serve static content in DEBUG mode.

  1. Have python 2.7/3.5+ and django installed.
  2. Create a new django project with django-admin startproject PROJECTNAME
  3. cd into PROJECTNAME, create the API "app" with ./manage.py startapp api
  4. Create a staticfiles folder with mkdir static, populate it with your transpiled/bundled web client.
  5. Open PROJECTNAME/settings.py, and change the following settings:
    • Remove an entry from the INSTALLED_APPS array: django.contrib.staticfiles
    • Add an entry to the TEMPLATES.DIRS array: os.path.join(BASE_DIR, "static")
    • Change STATIC_URL to STATIC_URL = '/'
    • Change STATIC_ROOT to STATIC_ROOT = os.path.join(BASE_DIR, "static")
  6. Route your api app's endpoints with the following steps

Create a proof-of-concept API endpoint in 'api/views.py' as follows

from django.http import HttpResponse


def hello(request):
    return HttpResponse('{"response":"Hello. I am the API!"}', "application/json")

...and set up your api app's internal routing in api/urls.py like so:

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^hello$', views.hello, name='hello'),
]

Add your api app to the project's url structure by editing PROJECTNAME/urls.py as follows:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^api/', include('api.urls')),
    url(r'^admin/', admin.site.urls),
]
  1. Add DEBUG-only routes for static content by altering PROJECTNAME/urls.py as follows:
import os
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin

urlpatterns = [
    url(r'^api/', include('api.urls')),
    url(r'^admin/', admin.site.urls),
]

if settings.DEBUG:
    from django.views.generic.base import TemplateView
    urlpatterns += [url(r'^$', TemplateView.as_view(template_name='index.html'))]

    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT, show_indexes=True)

...you should now be able to run your django site in "dev mode", with manage.py runserver, and have django serve your static content. In production (DEBUG != True), none of that content will be served - as it should be handled by a web server specializing in delivering static content.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.