dav*_*gri 4 html css python django
如果对此有一个明显的答案,请提前抱歉,我仍在学习 Django 的诀窍。
我正在创建一个网站,其中有 6 个预先确定的主题(未存储在数据库中)英语、公民、文学、语言、历史、圣经
每个主题都将与一种独特的颜色相关联。
我有一个 subject.html 页面的模板和一个从 url appname/subject/subjectname 加载的视图
我需要做的是应用特定的 css 根据访问的主题来设置页面样式。例如,如果用户转到 appname/subject/english,我希望该页面以英语为“主题”。
我希望我已经说清楚了,我也想知道是否有一种方法可以将实际的CSS代码添加到样式表中,而不必从后端一一更改属性。
非常感谢!
在模板中,您可以使用条件来添加 css,如下所示:
<div class="{% if subject=='civics' %}civic-class{% endif %}"></div>
Run Code Online (Sandbox Code Playgroud)
为此,subject价值应该来自view。现在,对于主题页面,您可以使用该extends标签。我们假设:
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
Run Code Online (Sandbox Code Playgroud)
然后在模板中:
{% extends template_base %} # at the top
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助,如果您使用基于类的视图,则逻辑相同。