简体   繁体   English

django 显示错误,例如“第 3 行的块标记无效:“其他”。 您是否忘记注册或加载此标签?

[英]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.

相关问题 Django:第14行上的无效块标记:“ else”。 您是否忘记注册或加载此标签? - Django: Invalid block tag on line 14: 'else'. Did you forget to register or load this tag? 无效的块标记“其他”。 您是否忘记注册或加载此标签? - Invalid block tag 'else'. Did you forget to register or load this tag? Django - 无效的块标记..'else',预期为 'empty' 或 'endfor'。 您是否忘记注册或加载此标签? - Django - Invalid block tag..'else', expected 'empty' or 'endfor'. Did you forget to register or load this tag? Django 问题:第 17 行的块标记无效:“静态”。 您是否忘记注册或加载此标签? - a Django problem: Invalid block tag on line 17: 'static'. Did you forget to register or load this tag? Django:第 14 行的块标记无效:“endblock”,预期为“endfor”。 您是否忘记注册或加载此标签? - Django: Invalid block tag on line 14: 'endblock', expected 'endfor'. Did you forget to register or load this tag? Django 错误第 21 行的块标记无效:'endblock'。 您是否忘记注册或加载此标签? 我如何解决它 - Django error Invalid block tag on line 21: 'endblock'. Did you forget to register or load this tag? How do i fix it 无效的块标记。 您是否忘记注册或加载此标记? - Invalid block tag. Did you forget to register or load this tag? 无效的块标记:'endblock'。 您是否忘记注册或加载此标签? - Invalid block tag: 'endblock'. Did you forget to register or load this tag? 无效的块标记:'endblock'。 您是否忘记注册或加载此标签? - Invalid block tag : 'endblock'. Did you forget to register or load this tag? django模板代码未加载:“ TemplateSyntaxError:第19行“ applink”上的无效块标记。 您是否忘记注册或加载此标签?” - django template code not loading: “TemplateSyntaxError : Invalid block tag on line 19: 'applink'. Did you forget to register or load this tag?”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM