Using a Django Variable in a CSS File

Using a Django variable in a CSS file

You basically have two options:

  1. Serve your CSS dynamically, with an entry in urls.py, etc., just as if it were an HTML page. Your template file will simply be CSS instead of HTML, but will use normal Django template syntax, etc.

  2. Shortcut: Reference your background image with a relative path. This may or may not be possible for your environment, but it's a convenient way of having a static CSS file reference different paths depending on where it's hosted.

Use Django template syntax in a css file

As you correctly pointed out, Django templates can be used for any text file, not just HTML. However, you need to make sure they're rendered by the template engine, providing a specific url and a view.

That way, you can expect to have all variables and tags interpolated, and in particular to have "static" replaced by settings.STATIC_URL.
However, I wouldn't insist in prepending "/static/" to the url of the CSS file itself ... that would be cheating, as you're rather rendering the file dynamically.

In practice:

project/urls.py

from django.urls import path
from django.views.generic import TemplateView

urlpatterns = [
...
path('css/home_global.css', TemplateView.as_view(
template_name='home_global.css',
content_type='text/css')
),
...
]

The view is rather trivial, and has been inlined in urls.py.
Please note I also specified the appropriate mimetype 'text/css'.

Here, I prepended a 'css/' prefix to the url, but this is not necessary, and you don't need a "css" folder in your project;
just make sure that the template engine can find "home_global.css"; that is, put it in the /template/ subfolder of any installed app,
or even in the project if it is listed among the installed apps:

project/templates/home_global.css

{% load static %}

body {
background-image: url("{% static 'citator/citator.jpg' %}");
}

You can check immediately the result by navigating to this url with your browser:

http://127.0.0.1:8000/css/home_global.css

which renders the document as follows:

body {
background-image: url("/static/citator/citator.jpg");
}

and include it in main template as required:

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/css/home_global.css" type="text/css">
...

Should you need to render many css documents, it might be convenient to treat the filename as parameter, then use a single view for all documents. In this case I would opt for a function based view, for simplicity:

urls.py:

from django.urls import path
from . import views

urlpatterns = [
...
path('css/<str:filename>.css', views.css_renderer),
...
]

where:

views.py

from django.shortcuts import render

def css_renderer(request, filename):
return render(request, filename + '.css', {}, content_type="text/css")

and in your main template:

<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/css/home_global.css" type="text/css">
<link rel="stylesheet" href="/css/another.css" type="text/css">
<link rel="stylesheet" href="/css/yet_another.css" type="text/css">
...

use context variable in internal css of django template

I found the answer. My syntax was not proper while showing data.

I just added '' around {{ object.property }} and it is working fine.

So my new code is simply.

        @page{
@bottom-right {
content: "Invoice No :- " '{{ obj.property }}' " Page No." counter(page) "/" counter(pages)
}
}


Related Topics



Leave a reply



Submit