How to render a template using dynamic CSS?

I want to create a dynamic CSS file in a view, and then display a template that loads this CSS file. Depending on the arguments provided to the view, CSS may have different meanings in certain places each time the view is called. How can I do it? (I have to add that I have no experience writing files in Python / Django.)

Here is a simplified example of how I think it should work:

# urls.py urlpatterns = patterns('', (r'^myview/(?P<color>[0-9a-f]{6})/$', create_css_file), ) # views.py def create_css_file(request, color): raw = "@charset 'UTF-8';\n\n" raw += "body {\n" raw += " color: #" + color + ";\n" raw += "}\n\n" f = open('mydynamic.css', 'r+') f.write(raw) return render_to_response('mytemplate.html', locals()) # mytemplate.html {% extends "base.html" %} {% block head %} <link rel="stylesheet" media="screen" href="{{ f.name }}" /> {% endblock %} 

For some reason, this does not work, although in the source code of the HTML page it looks like the CSS file is loaded correctly. f even gets into the template correctly, because I can see its contents when I change the line <link>... to

 <link rel="stylesheet" media="screen" href="{{ f }}" /> 

( f instead of f.name ). But HTML is displayed without the right color setting. Can someone tell me why this is so?

I suspected some kind of problem, and I played a little with different paths, but to no avail.

Please do not advise me to prepare some hard-coded CSS files (as I found in answers to similar questions), because there will be several hundred possibilities.

+6
source share
3 answers

I went with the @CatPlusPlus suggestion: calculating the required values ​​in the view and passing the template a very long string ( raw ) that contains all the CSS. In the template, I include it like this:

 <style media="screen">{{ raw|safe }}</style> 

Thank you all for your efforts!

+1
source

If you absolutely need to, you can simply create the css file dynamically.

You can create an entry in urls.py. You can name the URLs that you want it to look like a static .css file in the outside world, but it would be created dynamically.

 (r'^(?P<color>[0-9a-f]{6})/dynamic.css$', dynamic_css) def dynamic_css(request, color): """ Create a css file based on a color criteria, or any other complicated calculations necessary """ # do custom element positionting. return render_to_response('dynamic.css', {'color': color}) # dynamic.css body { background-color: {{ color }} } 

There is no reason to write css files for this. Now you can just turn it on

<link rel="styleshee" type="text/css" href="/purple/dymamic.css" />

In your template.

As already mentioned, this should not be used only to change one color. This can be done in your template. If you had to do something like this, it would probably be nice to implement caching, since every time a page is requested it should dynamically generate .css, which could be performance overhead. This is more of an example showing that you can call urls.py entries anything. And include them in whatever way you want in html, i.e. if you need a dynamic custom javascript file, you can create an entry in urls.py and then create a view that creates the .js file.

+11
source

views.py:

 def create_css_file(request, color): f = color return render_to_response('mytemplate.html', locals()) 

template:

 <body style = "color:{{f}}!important;"> 

Do not create a css file on the fly, this is not necessary.

+3
source

Source: https://habr.com/ru/post/917718/


All Articles