您如何手动呈现其初始值设置的表单域? [英] How can you manually render a form field with its initial value set?

查看:171
本文介绍了您如何手动呈现其初始值设置的表单域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试手动呈现表单的字段,以便我的设计人员可以操纵HTML中的输入元素,而不是在Python源码中挣扎。



ie。而不是声明这样的表单字段...

  {{form.first_name}} 

..我实际上是...

 < label for =id_first_name>您的姓名:< / label> 
< input type =textvalue ={{form.first_name.somehow_get_initial_value_here}}name =first_nameid =id_first_nameclass =blablamaxlength =30>
{%if form.first_name.errors%}< span> *** {{form.first_name.errors | join:,}}< / span> {%endif%}

问题是在第二种方法中似乎没有办法获取字段的初始值。 {{form.first_name}} 语句使输入元素具有正确的初始值,但是不知何故没有像 {{form.first_name.initial_value}} 字段想要手动呈现表单。

解决方案

这个问题有一个有趣的长期票。有一些示例代码可以在需要执行的注释中实现模板过滤器:



http://code.djangoproject.com/ticket/10427


I'm trying to render a form's fields manually so that my designer colleagues could manipulate the input elements within the HTML instead of struggling in Python source.

ie. Instead of declaring form fields like this...

            {{ form.first_name }}   

.. I actually do ...

            <label for="id_first_name">Your name:</label>
            <input type="text" value="{{  form.first_name.somehow_get_initial_value_here }}" name="first_name" id="id_first_name" class="blabla" maxlength="30" >
            {% if form.first_name.errors %}<span>*** {{ form.first_name.errors|join:", " }}</span>{% endif %}

Problem is that there seems to be no way to get the initial value of the field in the second method. {{ form.first_name }} statement does render the input element with the proper initial value but somehow there is nothing like {{ form.first_name.initial_value }} field when you want to render the form manually.

解决方案

There is an interesting long standing ticket about this very issue. There is sample code to implement a template filter in the comments that should do what you need:

http://code.djangoproject.com/ticket/10427

这篇关于您如何手动呈现其初始值设置的表单域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆