python - 为什么 user.is_superuser 在 Django 模板中返回字符串?

标签 python django django-templates python-social-auth

您好,我正在使用一个模板,我想在其中显示或隐藏 html block ,具体取决于用户是否是 super 用户。代码如下:

<h1> What user.is_superuser returns: {{ user.is_superuser }}</h1>
{% if user.is_superuser %}
        <h1>Superuser</h1>
{% else %}
        <h1>Mindundi</h1>
{% endif %}

这个想法是向 super 用户显示“Superuser”,否则显示“mMindundi”。

但这是我得到的输出:

enter image description here

我一直在抓狂,找不到解决办法。我知道问题来自于 ``"0"```` 是一个字符串而不是一个 int,所以所有的东西都被解析为 True...为什么会发生这种情况???我知道修复方法是将其解析为 int...但如果可能的话我想以其他方式修复它。

作为引用,我正在使用这些软件包:

asgiref==3.2.3
certifi==2019.6.16
chardet==3.0.4
defusedxml==0.6.0
Django==2.2.3
django-markdownx==2.0.28
django-model-utils==3.2.0
docxtpl==0.6.3
idna==2.8
Jinja2==2.10.1
lxml==4.3.4
Markdown==3.1.1
MarkupSafe==1.1.1
oauthlib==3.0.2
Pillow==6.1.0
PyJWT==1.7.1
python-docx==0.8.7
python-social-auth==0.3.6
python3-openid==3.1.0
pytz==2019.1
requests==2.22.0
requests-oauthlib==1.2.0
six==1.12.0
social-auth-app-django==3.1.0
social-auth-core==3.2.0
sqlparse==0.3.0
urllib3==1.25.3
xlrd==1.2.0

最佳答案

我发现问题所在了。今年夏天我进行了一次手动迁移,修复了一些无法自动迁移的表。在这个过程中的某个地方,我修改了表auth_user,将一些列从整数更改为文本,一旦我替换,一切就开始按预期工作。

关于python - 为什么 user.is_superuser 在 Django 模板中返回字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59618993/

相关文章:

python - Django:当管理模板已经被覆盖时不能覆盖它们吗?

python - sqlalchemy session.close() 不关闭连接

python - 一个字符串收缩的 Pythonic 实现,它列出了出现的字符及其计数?

python - 如何使用子字符串格式化 JSON 项目 OrderedDic 转储 - PYTHON 3

python - 在 Django 中安装用户管理系统时如何修复 "no fixtures found"?

python - 自定义 django 管理模板的单元测试

python - 如何将参数传递给 Python 3 中的自定义静态类型提示?

python - 将一系列外键串在一起以返回 Django 中的表值

python - 我的路由中需要什么 base_name 参数才能使这个 Django API 工作?

django - 等效于使用 if .. else 作为 Django 模板语言中的表达式