insert html into a form from Django code (not template)

999 views
Skip to first unread message

angelika

unread,
Jul 5, 2012, 9:57:57 AM7/5/12
to django...@googlegroups.com
Is there a way to insert arbitrary html into a form from the Django code, and not in the template? The equivalent of #markup in a Drupal form.

/Angelika

Jon Black

unread,
Jul 5, 2012, 10:18:03 AM7/5/12
to django...@googlegroups.com
This (https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#customizing-the-form-template) and more generally the entire page explains how to work with forms.
--
Jon Black
 
 
On Thu, Jul 5, 2012, at 02:57, angelika wrote:
Is there a way to insert arbitrary html into a form from the Django code, and not in the template? The equivalent of #markup in a Drupal form.
 
/Angelika

 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/ErDblgugM7wJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
 

angelika

unread,
Jul 5, 2012, 10:20:47 AM7/5/12
to django...@googlegroups.com
Thanks, but I am asking if there is a way to insert html into a form from the backend code, and not in the template?

Jon Black

unread,
Jul 5, 2012, 10:40:57 AM7/5/12
to django...@googlegroups.com
I've never done this, so I'm just throwing out ideas to try and be helpful. I've found your stackoverflow post as well, which has more information. (http://stackoverflow.com/questions/11341118/printing-repeated-django-form-fields-individually)
 
Have you tried looping over the fields in the template? I know this is in the template, but you can add the text you want still:
 
{% for field in form %}
  <p>Some stuff I want here that form.as_p won't do for me</p>
  <div class="fieldWrapper">
    {{ field.errors }}
    {{ field.label_tag }}: {{ field }}
  </div>
{% endfor %}
--
Jon Black
--

You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/y7Inar5KaoEJ.

angelika

unread,
Jul 5, 2012, 11:04:42 AM7/5/12
to django...@googlegroups.com
Thanks for answering! I would really like to do it that way, if I could. But I only want the html for some of the fields, not all of them. It would be great if I could iterate over just a few fields or be able to generate the name of the field in a loop, something like this:

[some fields]

{% for index in count|get_range %}
  <p>Some stuff I want here that form.as_p won't do for me</p>
  <div class="fieldWrapper">
    {{ field_name_[somehow get index in here].errors }}
    {{ field_name_[somehow get index in here] }}
</div>
  <div class="fieldWrapper">
    {{ field_email_[somehow get index in here].errors }}
    {{ field_email_[somehow get index in here] }}
</div>
{% endfor %}

[some more fields]

but I can't figure out a way to add index as a part of the field name.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.

Σταύρος Κρουστούρης

unread,
Jul 5, 2012, 10:32:06 AM7/5/12
to django...@googlegroups.com
On 07/05/2012 01:20 PM, angelika wrote:
Thanks, but I am asking if there is a way to insert html into a form from the backend code, and not in the template?
You can pass the html as a string from a context variable (i believe this should work),
but this is not a very good way to do things.
You are supposed to use django as suggested.The philosophy is
that html code and in general the frontend operations are being held
only in the templates and vice versa, backend operations only in the backend,
in order to ease and separate these tasks.

E.g. Better solution: you can pass a boolean or sth (foo)  to the template and with a template tag like:
{% if foo %}display html here {% endif %}

Please explain what your problem is or what do you want to do.
I am pretty sure that there is a better solution than loading html code from the backend.

Σταύρος Κρουστούρης

unread,
Jul 5, 2012, 10:53:32 AM7/5/12
to django...@googlegroups.com
On 07/05/2012 01:40 PM, Jon Black wrote:
/*SC*/DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"/*EC*/
I've never done this, so I'm just throwing out ideas to try and be helpful. I've found your stackoverflow post as well, which has more information. (http://stackoverflow.com/questions/11341118/printing-repeated-django-form-fields-individually)
 
Have you tried looping over the fields in the template? I know this is in the template, but you can add the text you want still:
 
{% for field in form %}
  <p>Some stuff I want here that form.as_p won't do for me</p>
  <div class="fieldWrapper">
    {{ field.errors }}
    {{ field.label_tag }}: {{ field }}
  </div>
{% endfor %}
--
Jon Black
May i suggest that the best way to do this is to create a template tag.
But i am not so sure about what she is trying to achieve.

angelika

unread,
Jul 5, 2012, 12:15:47 PM7/5/12
to django...@googlegroups.com
I've written a longer post here: http://stackoverflow.com/questions/11341118/printing-repeated-django-form-fields-individually explaining what I need. Either a way to individually print out the fields in a loop or a way to insert html from the backend. Maybe it's just not possible to do this in Django and then I will think of another solution, but I would like to make sure before I solve it another way.

/Angelika

Tom Evans

unread,
Jul 5, 2012, 2:26:06 PM7/5/12
to django...@googlegroups.com
On Thu, Jul 5, 2012 at 1:15 PM, angelika <angelik...@gmail.com> wrote:
> I've written a longer post here:
> http://stackoverflow.com/questions/11341118/printing-repeated-django-form-fields-individually
> explaining what I need. Either a way to individually print out the fields in
> a loop or a way to insert html from the backend. Maybe it's just not
> possible to do this in Django and then I will think of another solution, but
> I would like to make sure before I solve it another way.
>
> /Angelika
>

Of course it is possible. When you create the fields in the init
method, make sure they are given distinct names. Store the names of
the generated fields in a list on the form object, and then provide an
iterator method that yields the variable fields in the order you want.
Eg:

class MyForm:
def __init__(self, *args, **kwargs):
self._variable_fields = [ ]
for ....:
field_name_id = 'field_name_%d' % val_id
field_email_id = 'field_email_%d' % val_id
self.fields[field_name_id] = forms.FooField(...)
self.fields[field_email_id] = forms.FooField(...)
self._variable_fields.append((field_name_id, field_email_id))

def variable_fields(self):
for field_name_id, field_email_id in self._variable_fields:
yield self[field_name_id], self[field_email_id]

In your template:

{{ form.static_named_field1 }}
{{ form.static_named_field2 }}

{% for field_name, field_email in form.variable_fields %}
{{ field_name }}
{{ field_email }}
{% endfor %}

Hope that helps

Cheers

Tom

Melvyn Sopacua

unread,
Jul 5, 2012, 2:35:18 PM7/5/12
to django...@googlegroups.com
On 5-7-2012 16:26, Tom Evans wrote:
> On Thu, Jul 5, 2012 at 1:15 PM, angelika <angelik...@gmail.com> wrote:
>> I've written a longer post here:
>> http://stackoverflow.com/questions/11341118/printing-repeated-django-form-fields-individually
>> explaining what I need. Either a way to individually print out the fields in
>> a loop or a way to insert html from the backend. Maybe it's just not
>> possible to do this in Django and then I will think of another solution, but
>> I would like to make sure before I solve it another way.
>>
>> /Angelika
>>
>
> Of course it is possible. When you create the fields in the init
> method, make sure they are given distinct names. Store the names of
> the generated fields in a list on the form object, and then provide an
> iterator method that yields the variable fields in the order you want.

[snip]

Nice solution, but weren't formsets made for this type of thing? I'm
trying to figure out why formsets couldn't be used here and coming up
blank, unless the naming convention is somehow unchangeable.
--
Melvyn Sopacua


angelika

unread,
Jul 5, 2012, 2:47:00 PM7/5/12
to django...@googlegroups.com
Well thank you, Tom, you're a star! Will try this out as soon as possible,
Cheers

angelika

unread,
Jul 5, 2012, 2:48:07 PM7/5/12
to django...@googlegroups.com
Only a few of the fields need to be repeated in the form, not the entire form.
/Angelika

Tom Evans

unread,
Jul 5, 2012, 2:52:10 PM7/5/12
to django...@googlegroups.com
On Thu, Jul 5, 2012 at 3:35 PM, Melvyn Sopacua <m.r.s...@gmail.com> wrote:
> Nice solution, but weren't formsets made for this type of thing? I'm
> trying to figure out why formsets couldn't be used here and coming up
> blank, unless the naming convention is somehow unchangeable.

Possibly. Formsets are great if you want to repeat the same form over
and over again, but sometimes you don't want to do that.

I've made forms like this for things like personal preferences.
Certain fields are always required (name, etc), some fields are only
collected for users if their organization permits, and some
organizations have a list of custom properties that must be collected.
In this scenario, formsets are useless, but dynamically created form
fields fit perfectly, providing you can generate the form in the
template in a sane manner.

Cheers

Tom

Melvyn Sopacua

unread,
Jul 5, 2012, 2:56:45 PM7/5/12
to django...@googlegroups.com
On 5-7-2012 16:48, angelika wrote:
> Only a few of the fields need to be repeated in the form, not the entire
> form.

Right, so split out the repeated fields in a separate form. Remember
that the form tag and submit button is not part of the form object. If
these repeated fields are many-to-many relationships of a model, then
look at inline formsets:
<https://docs.djangoproject.com/en/1.4/topics/forms/modelforms/#inline-formsets>

--
Melvyn Sopacua


Reply all
Reply to author
Forward
0 new messages