0

I have an attribute of objects that can be rendered in html template like this :

{{ mymodels.something }}

In my case, i have forms containing an input field that has name similar with "something", so i want to run something like this in my template:

{% for form in my_form %}
{{ mymodels.form.name }} <!-- is same as mymodels.something -->
{% endfor %}

but it can't be rendered.. How can i do something like that?

Faizalprbw
  • 529
  • 1
  • 6
  • 17
  • Can you please explain more about what your trying to do ? So you can get a best possible way to do it. – Lavanya Pant Sep 22 '16 at 13:36
  • In my case, i have forms containing an input field that has name "form.name" with the same value with "something".. So if i write {{ something }}, it will return the same value with {{ form.name }}.. – Faizalprbw Sep 22 '16 at 13:42
  • if i have params = { 'person_name': 'Faizalprbw' } and pass them to template if we use {{ person_name }} it will print your name. – Lavanya Pant Sep 22 '16 at 13:48
  • I Got it, it can be done by refer to this [844746](http://stackoverflow.com/questions/844746/performing-a-getattr-style-lookup-in-a-django-template) – Faizalprbw Sep 23 '16 at 07:24

1 Answers1

0

Forms are something to gather data on client side - send it over to server side which validates them and does something with them (e.g. writing into models, or maybe passing it back into the context of your next rendering).

When your template is rendered, it also renders your form (building the markup, providing initial values) - when the template engine finished it's job, the HTML code is sent to the browser.

If you want to access form data on rendering time, there will always be the same value: the initial value. Which does not make much sense. As you might just pass the same data to your rendering context and use it from there.

Read the docs for further information on forms, especially the part rendering fields manually might interest you - if your question aims into this direction.

dahrens
  • 3,879
  • 1
  • 20
  • 38