python - Changing css styles from view in Django -


sorry in advance if there obvious answer this, i'm still learning ropes django.

i'm creating website has 6 pre determined subjects (not stored in db) english, civics, literature, language, history, bible

each subject going associated unique color.

i've got template subject.html page , view loads url appname/subject/subjectname

what need apply particular css style page according subject accessed. example if user goes appname/subject/english want page "themed" english.

i hope i've made myself clear, know if there way can add actual css code stylesheet , not have change attributes 1 one back-end.

thanks much!

django's views not responsible presentation, it's template (and css etc of course)'s reponsability. assuming have same view serving different subjects, view need know current subject (i assume captured part of url passed argument view), can pass information template, in turn can use add subject-specific class body tag. have write css accordingly.

as example:

# urls.py  patterns = urlpatterns('',     #...     url(r'whatever/(p?<subject>[a-z-]+>)/$', 'myviews.index', ...),     )  # myviews.py def index(request, subject):     # 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> 

Comments