[英]django shows error like 'Invalid block tag on line 3: 'else'. Did you forget to register or load this tag?'
following code is from views.py it fetch the profile which is a extension of user module in django also it passes that profile to html as a dictionary.以下代码来自views.py,它获取配置文件,该配置文件是django中用户模块的扩展,还将该配置文件作为字典传递给html。
views.py视图.py
def render_add_req(request):
profile = Profile.objects.get(user=request.user)
return render(request, 'add_item_request.html', {'profile': profile})
This is the code from html to be rendered and in this I am using is_admin attribute of profile object to determine whether the given user is admin or staff and after that I am determining which bases template to be extend with that html depending on user.这是 html 中要呈现的代码,在此我使用配置文件 object 的 is_admin 属性来确定给定用户是管理员还是员工,然后我确定要使用 ZFC35FDC70D2FC69D23EZA 扩展哪些基础模板,具体取决于用户。
add_item_request.html add_item_request.html
{% if profile.is_admin == 'a' %}
{% extends 'admin_base.html' %}
{% else %}
{% extends 'staff_base.html' %}
{% endif %}
{% block title %}
Item Req : Add
{% endblock %}
{% block content %}
Item Req : Add
{% endblock %}
Included answers [from comments] of: Abdul Aziz Barkat包含的答案 [来自评论]:Abdul Aziz Barkat
You cannot put extends in an if-else.您不能将扩展放在 if-else 中。
Check, python / django - if statement in template around extends检查, python / django - 如果模板中的语句围绕扩展
Django {% with %} tags within {% if %} {% else %} tags? Django {% with %} 标签内 {% if %} {% else %} 标签?
Q how can I extend different base template depending on different user type in single html page? Q 如何在单个 html 页面中根据不同的用户类型扩展不同的基本模板?
1 a simple way would be to pass the template name in the context as extends can use a variable name so passing something like {'template_name': 'admin_base.html', ...} and in the template {% extends template_name %} 1 一种简单的方法是在上下文中传递模板名称,因为扩展可以使用变量名称,因此传递类似 {'template_name': 'admin_base.html', ...} 和模板 {% extends template_name %}
2 Check an user's category from views and render seperate template for admin and normal users 2 从视图中检查用户的类别并为管理员和普通用户呈现单独的模板
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.