django - Django - 如何动态选择基本模板
问题描述
我想从三个可能的选项中选择要在我的 Django 项目中使用的基本模板。我从网上采购,从这个链接,我看到:
使用变量。
{% extends base_template %}
并在您的视图中,将其设置为您的视图中的“base.html”,或者一个新的“ajax.html”文件,它只提供块而没有别的。
但对该答案的评论如下:
这打破了 django-compressor 离线压缩。
- 我不知道那个是什么意思
另一个回答说:
{% extends request.is_ajax|yesno:"app/base_ajax.html,app/base.html" %}
但我没有提出AJAX
要求。从这个答案中,我发现了yesno
Django 提供的。这仅适用于True
or False
; 更像是if-else
声明。我怎样才能实现和if
报表?因为这似乎是我可以实现我想要实现的目标的一种方式。但如果你有更好的选择,请把它倒出来。else
else-if
这是我尝试过的
{% if user_type == 1 %}
{% extends 'hod_template/base.html' %}
{% load static %}
{% elif user_type == 2 %}
{% extends 'staff_template/base.html' %}
{% load static %}
{% else %}
{% extends 'student_template/base.html' %}
{% load static %}
{% endif %}
但我得到了Invalid block tag on line 4: 'elif'. Did you forget to register or load this tag?
错误。
解决方案
推荐阅读
- javascript - 服务器无法验证文件数组(php)
- oracle - sqlplus ORA-12504 在 oracle 服务器主机上本地运行 sqlplus - 但只能作为非 oracle 所有者帐户
- android - Branch.io 返回“会话初始化已经发生”错误消息
- regex - 如何使用正则表达式找到空的降价标题?
- configuration - 我可以在域模式下通过 cli 将模块安装到我的 wildfly 吗?
- flutter - 在 Flutter 文档中,Provider.of 段落的解释
- amazon-web-services - AWS API Gateway 通过查询参数缓存不起作用
- javascript - 将 JSON 问题库中的问题子集随机添加到调查 JS 测验
- c# - AWS Cognito 与 ASP.NET Core REST API
- vegan - 清楚地解释 NMDS 排序标准偏差椭圆和置信区间