Python (Django) Classroom Series – 19/Sept/2021

Serving Static Files

  • Web applications generally will have css files, images and to add interactivity we would have java scrip files, these files are referred as static files which are developed as part of the application.
  • When developing web applications using other frameworks or languages, you might need to pull all the static files in a specific directory that your webserver hosts.
  • Django can manage static assests for us to make this process easier, it provides tools for serving them with its development server during development & when the application goes to production, it can also collect your assets and copy them to a folder for dedicate webserver to host
  • The functionality is provide by Django’s built-in staticfiles app. It add several features for working with and serving static files
    • The static template tag to automatically build the static URL for an asset and include it in your HTML
    • A view (called static) that serves static files in development
    • Static file finders to customize where assets are found on your filesystem
    • The collectstatic management command which finds all the static files and moves them to single directory for deployment
    • The findstatic management command, which shows which static file on disk is loaded for a particular request
  • Exercise:
    • Lets take a single static file logo.png
    • Ensure in the has the following conditions met
      • Has DEBUG set to true
      • Contains django.contrib.staticfiles in its INSTALLED_APPS Preview
    • The URL mapping that is created is roughly equivalent to following map in your url patters path(settings.STATIC_URL, django.conf.urls.static). Any URL statring with settings.STATIC_URL(which is /static/ by default) gets mapped to static view Preview
    • There are three times when Django needs to locate static files on disk and for this it uses a static file finder which is a class that implements a methods for converting URL paths to disks and iterates through project directory to find static
    • The first time Django needs to locate static files on disk is when the Django static view recieves a request to load a particular file. For example if your URL PATH is /static/logo.png it is converted to the path books/static/ on the disk
    • The second time is when using the collectstatic management command.
    • The last time of a static file finder is during the execution fo findstatic management command.
    • Django comes with some built-in finders. The list of finders Django uses is defined in STATICFILES_FINDERS setting in
    • We will be looking into AppDirectoriesFinder and FileSystemFinder.
    • Refer Here for the changeset containing static files as icon
    • Exercise: Add image slider to home.html with any four images of your changes Refer Here for the html. Ensure all the images are static files. when user enters the home http://localhost:8000 the view home should serve this page.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin