简体   繁体   English

如何为模板中的 django 表单字段赋值?

[英]How to assign a value to a django form field in the template?

I was wondering how you can assign a value to a django form field in the template.我想知道如何为模板中的 django 表单字段赋值。

I know that there is other ways to assign an initial value in django, but I need to assign the value in the template because the variable is only present in the template.我知道在 django 中还有其他方法可以分配初始值,但是我需要在模板中分配值,因为该变量仅存在于模板中。

The way to do this with a normal html form would be this:使用普通 html 表单执行此操作的方法是:

{% for thing in things %}
  <p> {{ thing.content }} </p>
  <!-- Reply form -->
  <form>
    <input type="hidden" name="replyingto" value="{{ thing.number }}">
    <input type="text" label="Reply"></input>
  </form>
{% endfor %}

However, I need to use a django form.但是,我需要使用 django 表单。

I also know there is a way to assign a label to a field in the template, like this:我也知道有一种方法可以为模板中的字段分配标签,如下所示:

{{ form.non_field_errors }}
{{ form.field.errors }}
   <label for="{{ form.field.id_for_label }}"> field </label>
{{ form.field }}

So my question is basically how you would go about doing the example above but instead of assigning a label, assign a value.所以我的问题基本上是你将如何做上面的例子,而不是分配标签,而是分配一个值。


I've found a solution!我找到了解决办法!

What I did was type the html manually as Daniel suggested and assigned the value that way.我所做的是按照丹尼尔的建议手动输入 html 并以这种方式分配值。

For anyone who is wondering how I did it here is an example .对于想知道我是如何做到的人来说,这里是一个例子

you do it in Python, which can then be available to the HTML form or generator你在 Python 中完成它,然后它可以被 HTML 表单或生成器使用

in forms.py you can set the initial property to define the default value of the field.forms.py您可以设置initial属性来定义字段的默认值。

ex: name = forms.CharField(initial='class')例如: name = forms.CharField(initial='class')

or dynamically in views.py you can use a dict .或者在views.py动态地使用dict

ex: f = CommentForm(initial={'name': 'instance'})例如: f = CommentForm(initial={'name': 'instance'})

Once available within the form instance you can use {{ form.field.value }} in your HTML or automatically with a generator一旦在form实例中可用,您就可以在 HTML 中使用{{ form.field.value }}或使用生成器自动使用

Extended reference: https://docs.djangoproject.com/en/1.10/ref/forms/api/#s-dynamic-initial-values扩展参考: https : //docs.djangoproject.com/en/1.10/ref/forms/api/#s-dynamic-initial-values

You could render the form out manually field by field but then render all other fields using Django template interpolation, but your hidden field you can render manually.您可以逐个字段手动渲染表单,然后使用 Django 模板插值渲染所有其他字段,但您可以手动渲染隐藏字段。 eg例如

{% for thing in things %}
  <p> {{ thing.content }} </p>
  <!-- Reply form -->
  <form FORM_PARAMS_HERE >
    <div class="form-row">
        {{ form.other_form_field.errors }}
        {{ form.other_form_field.label_tag }} {{ form.other_form_field }}
    </div>
    ... (more form rows)
    <div class="form-row">
        <input type="hidden" name="replyingto" id="replyingto_id" value="{{ thing.number }}">
        <input type="text" label="Reply"></input>
    </div>
  </form>
{% endfor %}

I faced this exact problem and was finally able to resolve it as I think you want after finding this excellent and hard to find reference on it.我遇到了这个确切的问题,并且在找到这个优秀且难以找到的参考资料后,终于能够按照我想的那样解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM