1.

How can you set up static files in Django?

Answer»

Static files MAY include CSS, JavaScript, and images. Django refers to these images, JavaScript, or CSS files as static files. Django provides django.contrib.staticfiles to manage static files.

You may want to serve these static files alongside your site. 

  • ENSURE that django.contrib.staticfiles is included in installed apps.
  • Ensure that in your settings file, define the STATIC_URL to the folder PATH where static files will be placed and make sure the folder exists.

STATIC_url = '/static/'

  • Use the static template tag to BUILD the URL in your template, for the given relative path using the configured STATICFILES_STORAGE.

{% LOAD static %}
<img src = "{% static "my_app/example.jpg" %}" alt ="My image"/>

  • Store static files in a folder say static in your app. You can define a list of directories (STATICFILE_DIRS) in your settings file where Django will also look for static.


Discussion

No Comment Found