Serving static files in Django

From For a production site you probably want to configure the server to serve static files directly, without going through Django. For a test site, you can use the following approach

  1. In, url for requesting any static files is defined as follows:
    STATIC_URL = ‘/static/’

    1. This is the default, so you do not need to add it in, but you may want to change it
  2. To specify the location of the static files, add the following code to
    os.path.join(BASE_DIR, “static”),

    1. Django will now look for static files in (project root)/static
  3. Create the static file, in my case in <dev root>/Projects/DjangoText/static/testimage.jpg
  4. In your html template file or generated html, before including a static file, add the following:
    {{d34bf16ac7b745ad0d2811187511ec8954163ba9b5dbe9639d7e21cc4b3adbdb} load staticfiles {d34bf16ac7b745ad0d2811187511ec8954163ba9b5dbe9639d7e21cc4b3adbdb}}
  5. Include the static file as follows:
    <img src=”{{d34bf16ac7b745ad0d2811187511ec8954163ba9b5dbe9639d7e21cc4b3adbdb} static “testimage.jpg” {d34bf16ac7b745ad0d2811187511ec8954163ba9b5dbe9639d7e21cc4b3adbdb}}” “/>

    1. Note: When Django sees the “static” marker, it knows to extend the path to include the location of the static folder
    2. Note the double double quotes
  6. Refresh the screen to test