davegri
davegri

Reputation: 2286

Changing css styles from view in Django

Sorry in advance if there is an obvious answer to this, I'm still learning the ropes with Django.

I'm creating a website which has 6 pre determined subjects (not stored in DB) english, civics, literature, language, history, bible

each subject is going to be associated with a unique color.

I've got a template for a subject.html page and a view that loads from the url appname/subject/subjectname

what I need to do is apply particular css to style the page according to the subject accessed. for example if the user goes to appname/subject/english I want the page to be "themed" to english.

I hope I've made myself clear, also I would like to know if there is a way I can add actual css code to the stylesheet and not have to change attributes one by one from the back-end.

thanks very much!

Upvotes: 4

Views: 10044

Answers (3)

bruno desthuilliers
bruno desthuilliers

Reputation: 77902

Django's views are not responsible for the presentation, it's the template (and css etc of course)'s reponsability. Now assuming you have the same view serving different subjects, the view obviously need to know which is the current subject (I assume from a captured part of the url passed as argument to the view), so it can easily pass this information to the template, which in turn can use it to add a subject-specific class to the body tag. Then you only have to write your css accordingly.

As an example:

# urls.py

patterns = urlpatterns('',
    #...
    url(r'whatever/(P?<subject>[a-z-]+>)/$', 'myviews.index', ...),
    )

# myviews.py
def index(request, subject):
    # do whatever
    context = {
       # whatever else
       'subject':subject
       }
   return render(request, "whatever/index.html", context)

# whatever/index.html
<html>
   # headers etc
   <body class="something {{ subject }} etc">
     # whatever here
   </body>
</html>

Upvotes: 3

Gocht
Gocht

Reputation: 10256

In templates you can use conditionals for add css, like this:

<div class="{% if subject=='civics' %}civic-class{% endif %}"></div>

For this, subject value should come from view. Now, for themed page, you could use the extends tag. Let's supose:

def your_view(request):
    subject  # Here you get the url subject, 'how' is up to you
    if subject == 'english'
        template_base = '/some/html/tenplate.html'
    elif subject == 'civis':
        template_base = '/some/other/template.html'
    ... # then you return 'template_base' variable to template

Then in template:

{% extends template_base %}  # at the top

Hope this helps, is the same logic if you use Class-Based views.

Upvotes: 5

pythad
pythad

Reputation: 4267

You can do this is many ways. In general you need to return some variable from your view to the html and depending on this variable select a style sheet, if your variable name will match you style sheet's name you can do "{{variable}}.css", if not you can use JQuery.

Upvotes: 1

Related Questions