views:

257

answers:

1

I want to add same django form instance on same template. i already add one before and other add dynamically using javascript.

for example 'form" is a django form: newcell.innerHTML = {{ form.firstname }}; The problem is that when i submit the form, in view the request object has only one value (that is not add using javascript). how can i get the values of other form elements values that is added dynamically runtime.

It is something like the "Attach Another File" feature in gmail, where the user is presented with a file upload field and new fields are added to the DOM on the fly as the user clicks to "Attach Another File" plus button

A: 

You could always try separating out your FileField into a FileModel.

Take a look at the following pseudo code (as in python based on memory--i've moved over to clojure for now).

models.py

class FileModel(models.Model):
  file = models.FileField()
  ...

class ThingToWhichYoureAttaching(models.Model):
  name = models.CharField()
  attachments = models.ManyToManyField(FileModel)
  ...

forms.py

class FileForm(forms.ModelForm):
  class Meta:
    model=FileModel

class ThingForm(forms.ModelForm):
  attachments = forms.MultipleChoiceField()#override the manytomany form field with style field of your choice.
  class Meta:
    model=ThingToWhichYoureAttaching

When they pop up the window with the PLUS button, show the FileForm but on the main page leave the ThingForm untouched. You can also have an initial FileField on the main page with the ThingForm for people who don't have javascript. Just make sure to process the FileForm BEFORE the ThingForm so that the File is available for the Thing.

When processing the popup form you can use AJAX (i recommend jquery) to submit the FileForm to the server, and return the markup to insert the File in the Attachments field.

Brandon H