Django

How to setup static files in Django 2.2

Pinterest LinkedIn Tumblr

There are 3 main things we need to do:

  • set STATIC_ROOT in settings.py
  • run python manage.py collectstatic (or python3.5 or python3.6 as appropriate) Example: python manage.py collectstatic
  • set up a Static Files entry on the PythonAnywhere Web tab.

we can also customise STATIC_URL, if we want to use a static URL prefix other than /static/

STATIC_ROOT = os.path.join(BASE_DIR, ‘assets’)

Set url.py to load template

from .views import indexPageView

urlpatterns = [
        path('', indexPageView, name='index')]

Now, Within Views.py

from django.http import HttpResponse
from django.shortcuts import render

def indexPageView(request):
        return render(request, "index.html")

Load Html page index.html

{% load static %}

Load CSS, Js, Images similar to this code

{% static 'css/font-awesome.css' %}
{% static 'js/style.js' %}
{% static 'images/bg.jpg' %}

Write A Comment