Reputation: 1123
I can't fins any information on how to define css or javascript files in a view like:
class MyView(View): ....
class Media: css = { 'all' : 'mystyle.css' }
If you have a form you can do like: class MyForm(ModelForm): ....
class Media: css = { 'all' : 'mystyle.css' }
And then in the template you can print the files like; {{ form.media.css }}
I like that Syntax very much and I like to keep the View-specific css files in the app-directory.
Does anyone know if it's possible?
Thanks!
Upvotes: 1
Views: 175
Reputation: 306
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"/>
Upvotes: 2