You can specify it upon form creation. Here is an example of how you
could use prefixes for multiple model forms:

In your views:

from django import newforms as forms
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from yourproject.yourapp.models import ModelClass1, ModelClass2

def some_form_view(request):
  form_classes = {ModelClass1: None, ModelClass2: None}
  for model in form_classes:
    form_classes[model] = forms.form_for_model(model)
  form_objects = {}
  if request.method == 'POST':
    prefix = 0
    forms_are_valid = True
    for model, form_class in form_classes.iteritems():
      form_objects[model] = form_class(request.POST, prefix='f' +
str(prefix))
      forms_are_valid = forms_are_valid and
form_objects[model].is_valid()
      prefix += 1
    if forms_are_valid:
      for model, form in form_objects.iteritems():
        form.save()
      return HttpResponseRedirect('/some/url')
  else:
    prefix = 0
    for model, form_class in form_classes.iteritems():
      form_objects[model] = form_class(prefix='f' + str(prefix))
  return render_to_response('blah.html', {'forms':
form_objects.values()})

In your template:

<form action="." method="post">
{% for form in forms %}
  {{ form.as_p }}
{% endfor %}
<div class="submit"><input type="submit" value="Submit"></div>
</form>


On Jul 24, 11:05 am, "Chris Brand" <[EMAIL PROTECTED]> wrote:
> >  - When you instantiate your form, pass in prefix='form1' as an
> > argument - all the fields on the form will get that prefix, which will
> > keep the two forms distinct in the POST dictionary. This will only be
> > an issue if there is an overlap in the field names on the two forms,
> > but it's better to be safe.
>
> I've seen this "prefix" mentioned before in this context. Is it in the
> documention yet (I couldn't find it) ? Do all forms accept it as an
> instantiation parameter, or is it specific to form_for_instance and
> form_for_model ? Is it in 0.96 or only in trunk ?
>
> A quick grep through the 0.96 source seems to indicate that it is a
> parameter to BaseForm.__init__(), but that's as far as I can get by myself
> at the moment...
>
> Thanks,
>
> Chris


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to