How to get BoundField value in django template?

The built-in methods as_html, as_ul, as_p in Django formats do not work for me, as well as the built-in rendering {{field}}, so I am trying to write personalized rendering of the form.

Here is what I still have:

    <input id="id_{{field.html_name}}" 
                   type="text"
                   name="{{field.html_name}}"
                   placeholder="{{field.label}}"  <!-- "placeholder" is really the only reason I need to do a custom implementation -->
                   value="{{ XXX }}"      <!-- what goes here? -->
                   maxlength="30"  />

The question is what should go in the value attribute (marked XXXabove)?

I looked around a bit and did not find that BoundFieldthe valueor attribute supports data. I use ModelForms if that matters

+3
source share
3 answers

value trunk 2010 . , / ( , ). .

+3

. .

: http://djangosnippets.org/snippets/2264/

:/yourproject/yourapp/templatetags/

/yourproject/yourapp/templatetags/ 2 :

__ init__.py -

field_value.py - :

from django import template
register = template.Library()

@register.simple_tag
def field_value(field):
    """ returns field value """
    return field.form.initial.get(field.name, '')

:

{% load field_value %}

, :

{% field_value form.field %}

"field", :

{% field_value field %}

"" ,

{% field_value inline_admin_form.form.text %}
+1

Assuming the field name is "username" and the form name is "user_form", there are two values:

1) Initial:

{{ user_form.initial.username }}

2) Associated:

{{ user_form.username.data }}
0
source

Source: https://habr.com/ru/post/1777582/


All Articles