简体   繁体   中英

Serving static files 'conditionally' in in Django

I have done many projects in Django and I'm used to the normal way of serving static files. However, I have a different scenario this time, where there are sub-folders within the templates folder(eg there are folders page1/, page2/ etc. within the template folder). Each of those folders contains the template file "index.html" and then static files(css, js and images folders).

My question is, how can I get to serve each template with their corresponding static files.

Any help will be greatly appreciated.

You should be able to include the static file like any other template. Albeit, this is not the ideal approach for using static assets and is highly unorthodox for django.

One approach would be to have a block in the head and bottom of the body for extra css and extra js, respectively in your base html file. From there, you can call the block in your index.html and include your static file.

for example, in page1/index.html

{% block extra_css %}
<style>
  {% include 'page1/mycss.css' %}
</style>
{% endblock extra_css %}

Again, i would never personally do this. Using static files and referencing them is the way to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM