11

Below is a simplified version of the django code in my project; it allows users to upload a file and give it a title. This feature works perfectly. However, when user goes re-edit the form later, the file and title are redisplayed, but when the user goes to submit the file filed empties. The file field of the form reopened for editing looks like this:

Currently: media_location/uploadedfile.mp3

Change: [Choose File] No file Chosen

And after I submit it, it's:

  • This Filed is required

[Choose File] No file Chosen

How do I get it so that the user does not have to reupload the file? It does not matter to me whether the field is made readonly once submission is done, or whether it remains editable. The finished project is not for a client and will only be available to a small group of trusted users, but I would still like to follow best practices if possible. Thanks for any help.

Django Code:

models.py

class Recording(models.Model):
    rec_title=models.CharField(max_length=200,blank=True)
    rec_file = models.FileField(upload_to='recordings')

forms.py

from django import forms 
from songstorage.models import Recording
class RecordingForm(forms.ModelForm):
    rec_file=forms.FileField(label='Upload File')
    rec_title=forms.CharField(label='Recording Title',required=False)       
    class Meta:
        model=Recording

views.py

def addrecordings(request,recordingfile):
    #if there is a recordingfile in the url, the user is editing...
    if recordingfile:
        recording=Recording.objects.get(rec_title=recordingfile)
        recording_form = RecordingForm(instance=recording)
    #...Otherwise a new form is createing a new one
    else:
        recording_form = RecordingForm(request.POST, request.FILES)

    #When the form is submitted, do the following:
    if request.method == 'POST': 
        #check if its valid
        if recording_form.is_valid():
            recording_form.save() 
            #if sucessfully submitted, redirect
            return redirect('/recordings/')
    return render_to_response('addrecordings.html',{'recording_form':recording_form},context_instance=RequestContext(request))
koleror
  • 338
  • 1
  • 6
  • 22
sean
  • 520
  • 7
  • 17

2 Answers2

1

I have had the same problem and couldn't figure out how nor able to search anything useful, my current solution is to use another form, in your scenario:

class RecordingUpdateForm(RecordingForm):
    rec_file=forms.FileField(label='Upload File', required=False)

The only difference is I am using UpdateView class based view, so you have to modify your view function to use the RecordingUpdateForm for updating.

David Schumann
  • 13,380
  • 9
  • 75
  • 96
James Lin
  • 25,028
  • 36
  • 133
  • 233
1

I had the same problem.

You can override a model's default clean function. This validates all forms, the user can change the image file on edit, and the file is guaranteed to be nonempty.

class MyModel(models.Model):
  image = models.ImageField(blank=True)
  def clean(self, *args, **kwargs):
    super(MyModel, self).clean(*args, **kwargs)
    if not self.image:
      raise ValidationError('Please provide an image')
David Schumann
  • 13,380
  • 9
  • 75
  • 96
Liu
  • 87
  • 1
  • 8