Python 我想访问其模板中的结构块默认ID
我想将流字段ID保存到它的模板中 简而言之,在text_question.html中,我给出了id={{self.id}},但它不返回任何内容 我想要这个,因为在question.html文件中,我希望它与{{ 返回流字段id的field.id}} 换句话说,我想在text_question.html的id字段中存储{{field.id}}的值 models.pyPython 我想访问其模板中的结构块默认ID,python,django,wagtail,wagtail-streamfield,Python,Django,Wagtail,Wagtail Streamfield,我想将流字段ID保存到它的模板中 简而言之,在text_question.html中,我给出了id={{self.id}},但它不返回任何内容 我想要这个,因为在question.html文件中,我希望它与{{ 返回流字段id的field.id}} 换句话说,我想在text_question.html的id字段中存储{{field.id}}的值 models.py class TextQuestionBlock(blocks.StructBlock): """Text Question"
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 }}"
<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>
{%load tag_library%}
ID不是块值的内置属性,而是StreamField容器用来跟踪其内容的机制。块值具有ID属性并不总是有意义的:例如,CharBlock的值是一个字符串,并且不能在字符串上真正具有.ID
属性。类似地,StructBlock的子块也不会被赋予一个
因此,id在块的模板上不会自动可用-如果需要,需要通过{%include_block%}
模板标记从调用模板显式传递id。例如:
{% for field in question.body %}
{% if field.block_type == 'text_question' %}
{% include_block field with block_id=field.id %}
{% endif %}
{% endfor %}
这将使ID作为变量block\u ID
在text\u question.html
上可用