How do I serve specific and/or arbitrary static files from Django?

I’m building an extension to a SASS product. This extension requires:

  1. The / directory to contain index.html
  2. The / directory to contain config.json
  3. The / directory to contain customActivity.js
  4. The / directory to contain icon.png

I’d like to keep my config.json and customActivity.js files in my projects static directory, and my icon.png file in the media directory. My index.html file should be via a template.

So far, my urls.py file looks like:

from django.urls import path
from . import views

urlpatterns = [
    path("", views.index, name="index"),
]

and my views.py looks like:

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def index(request):
    return HttpResponse("some stuff") // just an example

How do write a url paths that will direct / to the index view, but /somefile.someextension to either the static or media directories?

Leave a Comment