简体   繁体   English

在Django中“有条件地”提供静态文件

[英]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. 我已经在Django中完成了许多项目,并且习惯了提供静态文件的常规方法。 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). 但是,这次我有另一种情况,模板文件夹中有子文件夹(例如,模板文件夹中有文件夹page1 /,page2 /等。) Each of those folders contains the template file "index.html" and then static files(css, js and images folders). 每个文件夹都包含模板文件“ index.html”,然后包含静态文件(css,js和images文件夹)。

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. 尽管这不是使用静态资产的理想方法,但对于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. 一种方法是在主体的头部和底部分别放置一个块,以在基本html文件中分别放置额外的css和额外的js。 From there, you can call the block in your index.html and include your static file. 从那里,您可以在index.html中调用该块,并包含您的静态文件。

for example, in page1/index.html 例如,在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. 使用静态文件并引用它们是方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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