Home > Blockchain >  Referencing static files in Django
Referencing static files in Django

Time:04-05

code example
tooltip file
searching for the correct path

FILES:

settings.py STATICFILES_DIRS = [ BASE_DIR / "static" , ]

base.html {% load static %}

How do I reference my static files in the following line of html code?...

<li ><a href="#" data-toggle="tooltip" data-placement="top" title="Facebook"><span ></span></a></li>

i.e. {% static 'website/...' %}

CodePudding user response:

As described in the docs, assuming your filepath is BASE_DIR/static/sub_dir/example.pdf you can reference it like this:

<li >
    <a href="{% static 'sub_dir/example.pdf' %}" data-toggle="tooltip" data-placement="top" title="Facebook">
        <span ></span>
    </a>
</li>

It is good practice to place your static files in a sub-directory named same as your app name. That way, it will be much easier to reference the files from different apps later on. That is why the example in the docs says {% static 'my_app/example.jpg' %}.

Also please make sure you go through the documentations and follow all the steps mentioned there.

  • Related