10

I have a simple Django form:

class CommentForm(forms.Form):
    comment = forms.CharField(max_length=2000, required=True)
    post_id = forms.CharField(max_length=2000, widget=forms.HiddenInput, required=True)
    parent_id = forms.CharField(max_length=2000, widget=forms.HiddenInput, required=True)

Now I want to print this form several times on my page - I am doing it through a template tag, so the new form is created each time. The problem is, that I get the same ID's for all fields.

I know about the prefix, but I do not want to change field names, because there is one handler for all forms, only to set unique IDs.

So my question:

  • Is there a way to make Django set unique IDs if I want to output a form several times, without changing the names of fields?
  • If not, is there a way to make Django not to output IDs at all?
AakashM
  • 62,551
  • 17
  • 151
  • 186
Silver Light
  • 44,202
  • 36
  • 123
  • 164
  • 2
    Have you looked at Django formsets? They allow you to manage multiple instances of the same form on a page: http://docs.djangoproject.com/en/dev/topics/forms/formsets/ – Timmy O'Mahony May 16 '11 at 11:27
  • @pastylegs, thank you for the link. I cannot use the formsets, because each form has different initial parameters. – Silver Light May 16 '11 at 12:05
  • You should be able to populate the forms in the formset with separate initial data by looping over the forms. Have a look at some of the examples. Formsets don't seem to be as well documented as the rest but they are quite powerful – Timmy O'Mahony May 16 '11 at 12:24

1 Answers1

18

You can control how the automatic IDs are generated with the auto_id parameter when you create a new instance of that form

Have a look here (search for auto_id):

http://docs.djangoproject.com/en/dev/ref/forms/api/#configuring-html-label-tags

Davo
  • 196
  • 1
  • 2