我想将流字段ID保存到模板中。
简而言之,在text_question.html中,我给出id = {{self.id}},但返回Nothing。
我想要这个,因为在question.html文件中,我希望它与{{ field.id}},返回流字段ID
换句话说,我想将{{field.id}}的值存储在text_question.html的ID字段中
models.py
class TextQuestionBlock(blocks.StructBlock):
"""Text Question"""
question = blocks.CharBlock(required=True, help_text="Add your Question")
is_save = blocks.BooleanBlock(label="Want to save this field ?", required=False)
is_email = blocks.BooleanBlock(label="Want to get this field as an email ?", required=False)
class Meta: # noqa
template = "question/question_field/text_question.html"
icon = "edit"
label = "Text Question"
@register_setting(icon='fa-commenting')
class QuestionSettings(BaseSetting):
body = StreamField([
("text_question", TextQuestionBlock()),
], verbose_name='Question', blank=True)
panels = [
StreamFieldPanel('body')
]
class Meta:
verbose_name_plural = 'Question'
verbose_name = 'Questions'
text_question.html
{% load tag_library %}
<input issave="{{self.is_save}}" isemail="{{ self.is_email }}" class="text_question" type="text" name="{{ self.question|to_name }}" id="{{ self.id }}" data-conv-question="{{ self.question }}"
question.html
<form name="question_form" action="" method="post" class="hidden">
<div id="unique_id"></div>
{% for field in question.body %}
{{ field.id }}
{% endfor %}
<input type="text" data-conv-question="test">
</form>
谢谢!
答案 0 :(得分:1)
ID不是块值的内置属性-而是StreamField容器用来跟踪其内容的一种机制。块值具有ID属性并不总是有意义的:例如,CharBlock的值是字符串,并且字符串上实际上不能具有.id
属性。同样,StructBlock的子块也不会被赋予一个。
因此,该ID不会自动在块的模板上提供-如果需要,您需要通过{% include_block %}
template标签从调用模板中显式传递它。例如:
{% for field in question.body %}
{% if field.block_type == 'text_question' %}
{% include_block field with block_id=field.id %}
{% endif %}
{% endfor %}
这将使ID在text_question.html
上作为变量block_id
可用。