django形式+数组奇怪的行为

时间:2022-01-21 17:26:20

I have a clean function for my form, and in it I have the below:

我的表单有一个干净的功能,在其中我有以下内容:

tags = cleaned_data.get("tags")
logger.info('got to clean function ' + str(tags))

When I output the tags value, I see: [ '1', '2', '3' ]. In my template, when I want to recreate the html for it, I have a template_tag:

当我输出标签值时,我看到:['1','2','3']。在我的模板中,当我想为它重新创建html时,我有一个template_tag:

@register.simple_tag
def generate_tags(tags):
    print str(tags)
    # Code to produce the html

and I'm calling it via: {% generate_tags form.tags %}.

我通过以下方式调用它:{%generate_tags form.tags%}。

However, when I finish the print command in the template function im getting:

但是,当我在模板函数中完成打印命令时,我得到:

<input type="hidden" name="event_tags" value="3" id="id_event_tags_0" />

Why isn't it giving me the array as I was seeing in the clean function?? Is there a way for me to get the array before the clean function is called?

为什么它不像我在clean函数中看到的那样给我数组?在调用clean函数之前,有没有办法让我获取数组?

1 个解决方案

#1


1  

Clearly this is not documented anywhere. For anybody else stuck on the same problem, if you have a form object, you can do:

显然,这在任何地方都没有记录。对于遇到同样问题的其他人,如果你有一个表单对象,你可以这样做:

form['field_name'].value()

This gives the value before it is cleaned or validated.

这将在清除或验证之前给出值。

#1


1  

Clearly this is not documented anywhere. For anybody else stuck on the same problem, if you have a form object, you can do:

显然,这在任何地方都没有记录。对于遇到同样问题的其他人,如果你有一个表单对象,你可以这样做:

form['field_name'].value()

This gives the value before it is cleaned or validated.

这将在清除或验证之前给出值。