英文:
How to make if statements on postgresql json field elements inside a django template
问题
在 Django 模板语言中,你的拼写可能有点问题。请检查你的条件语句中的单词 "setttings",看起来应该是 "settings"。
英文:
I am having a hard time checking if a json field contains certain variable value in django template language.
This is my model:
from django.db.models import JSONField
class Widget(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
endpoint = models.CharField(max_length=512) # "/data/id"
type = models.CharField(max_length=64, default="line-graph")
settings = JSONField(default=dict)
This is how I insert data to this model:
widget, created = Widget.objects.get_or_create(
user=request.user,
endpoint=endpoint,
)
widget.type = type_
widget.settings = {
"code": "",
"dashboard": dashboard.id,
"position": "top",
"params": "per=m&len=1"
}
However, when I do this in the template it fails:
{% if widget.setttings.position == "top" %}
This does not fail, on the other hand:
{% with widget.settings.position as pos %}
{% if pos == "top" %}
and by "fail" I mean it doesn't pass the condition.
Am I doing something wrong?
- Django version: 3.2
- Python version: 3.8
- PostgreSQL version: 12.10
答案1
得分: 2
你这里有3个't':
{% if widget.setttings.position == "top" %}
英文:
You have 3x 't' here:
{% if widget.setttings.position == "top" %}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论