[Answered ]-Django: Is it possible to attach media files (css, javascript etc) to a View-class?

2๐Ÿ‘

I imagine you should be able to set a variable with the css filename/location in your view, and if you pass that variable to the render/response you should be able to access it in your template.

Take a look at https://docs.djangoproject.com/en/dev/ref/templates/api/#variables-and-lookups

example:

def my_view(request):
    stylesheet = {"css": "my_view.css"}
    return render(request, 'myapp/templatename.html', stylesheet,
                  content_type="application/xhtml+xml")

and them templatename.html would be

<link rel="stylesheet" type="text/css" 
href="{{ MEDIA_URL }}{{ stylesheet["css"] }}"  media="screen"/>
๐Ÿ‘คderek

Leave a comment