简体   繁体   English

如何在Django的CSS页面中添加staticfiles处理

[英]how to add staticfiles processing to css pages in django

In my CSS, I have examples like this: 在我的CSS中,我有这样的示例:

#defaultCountdown span.countdown_section {
    color:#fff;
    padding:7px 15px!important;
    margin-bottom:2px;
    font-weight:300;
    background:url(../img/bg-white.png);
    text-align:center
}

If you see the background tag, there's a url. 如果您看到背景标签,则有一个网址。 How do I serve this via staticfiles? 如何通过staticfiles提供服务?

Thanks. 谢谢。

css is a static file, as is your images and .js. css是静态文件,图像和.js也是如此。 So you use relative paths to reference them, unless you've got a specific need to use static tags for backgrounds, then you can move the style tag for background outside your css and into into your html: 因此,您可以使用相对路径来引用它们,除非您特别需要为背景使用静态标签,否则您可以将背景的样式标签移到CSS之外并移入html中:

<div class="generic" id="#defaultCountdown span.countdown_section" style="background: url('{% static 'img/bg-white.png' %}');></div>

If you need to use {% static 'url' %} in your CSS or JS, the easiest would be to define the CSS in a <style> block in your HTML template's <head> and the JS in a <script> block at the bottom of your template. 如果您需要在CSS或JS中使用{% static 'url' %} ,最简单的方法是在HTML模板的<head>中的<style>块中定义CSS,在<script>块中的JS处定义。模板的底部。 Any CSS or JS that doesn't require the use of {% static 'url' %} can still go in separate .css and .js files. 不需要使用{% static 'url' %}任何CSS或JS仍然可以放在单独的.css和.js文件中。

To answer your comment about using static in css or js files.... It could be done. 要回答您有关在css或js文件中使用static的评论。...可以做到。 Basically you would define a route and view just like for html pages. 基本上,您将定义路径和视图,就像html页面一样。

But , this is strongly discouraged for a production level site. 但是 ,强烈建议不要在生产级别的站点上使用。 You will have a significant increase in page response time if you do this rather than serving these as static files. 如果您这样做而不是将其用作静态文件,则页面响应时间将大大增加。

To accomplish this, you simply pass content_type into your HttpResponse for your related view: 为此,您只需将content_type传递到HttpResponse以获取相关视图:

return HttpReaponse(my_dynamic_css, content_type="text/css") 

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

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