0

I have a form with about 10 fields 3 of which are filefields.

When I create a new record, No problem. When I update all fields, No problem. When I update only the 3 file fields and none of the other fields, no problem. But If I leave one filefield empty in the template I get MultiValueDictKeyError. Which is not cool because I want the users to be able to upload or update whatever fields they like whether its 1 or 3 or all fields.

I have searched everywhere and I cannot see this exact question coming up anywhere. Any help would be really appreciated.

Please excuse my very long winded code. I am a beginner. Form is something like this...

class DocumentForm(forms.Form):
price= forms.IntegerField(max_value=1000, min_value=1,
                          label='1. Set Price in $',
                          help_text='*')

docfile = forms.FileField(allow_empty_file= True,
    label='3 Upload file 1',
    help_text='*')

docfile1 = forms.FileField(allow_empty_file= True,
    label='2. Upload file2',
    help_text='*')

docfile2=forms.FileField(allow_empty_file= True,
    label='4. Upload a file ',
    help_text='*')

name= forms.CharField(widget=forms.TextInput,help_text='*')

description=forms.CharField(widget=forms.Textarea,help_text='*')

RADIO_CHOICES = (
    ('none', "No Category"),
    ('one', "Music"),
    ('two', "Movie"),
    ('three', "Tutorial "),
)
category=forms.ChoiceField(
    choices=RADIO_CHOICES,
    widget=forms.RadioSelect(),
    label ='7. Select a Category for your upload',
    required=False,
    initial=False,
    )

isover18s =  forms.BooleanField(
    label='8. Is over 18 content advised?', 
    required=False,
    initial=False
 )

Model is something like this...

class Document(models.Model):

docfile1 =models.FileField(blank=True,null=True,upload_to= 'MEDIA_ROOT/documents/%Y/%m/%d',)
docfile = models.FileField(blank=True,null=True,upload_to= 'MEDIA_ROOT/documents/%Y/%m/%d',)
docfile2 =models.FileField(blank=True, null=True,upload_to= 'MEDIA_ROOT/documents/%Y/%m/%d',)

category= models.CharField(max_length=100, null=False, blank=False)
usersname =models.CharField(max_length=100, null=False, blank=False)
name= models.CharField(max_length=100, null=False, blank=False)
description=models.CharField(max_length=300, null=False, blank=False)
isover18s =models.BooleanField(default=False, verbose_name = "Over 18 Content advised?")

price=models.IntegerField(verbose_name="Price is in Dollars MAX is $999")
dateUploaded=models.DateTimeField(auto_now_add=True, auto_now=False)
preview_views=models.IntegerField(verbose_name="Pre Views")
views=models.IntegerField(verbose_name="Views")

View is very long winded because another view uses the same form for different purpose and I'm no good. Anyway it is something like this...

def edit_preview(request, document_id):

    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        form.fields['docfile'].required = False
        form.fields['docfile1'].required = False
        form.fields['docfile2'].required = False
        form.fields['category'].required = False
        form.fields['price'].required = False
        form.fields['description'].required = False
        form.fields['name'].required = False

        if form.is_valid():

            newdoc = Document.objects.get(id=document_id)
            newdoc.usersname = request.user.username                

            newdoc = Document(docfile = request.FILES['docfile'])
            if newdoc.docfile:
                myfile=newdoc.docfile

            newdoc = Document(docfile1 = request.FILES['docfile1'])
            if newdoc.docfile1:
                myfile1=newdoc.docfile1

            newdoc = Document(docfile2 = request.FILES['docfile2'])
            if newdoc.docfile2:
                myfile2= newdoc.docfile2

            newdoc = Document.objects.get(id=document_id)
            newdoc.usersname = request.user.username

            if myfile:
               newdoc.docfile = myfile
            if myfile:
               newdoc.docfile1 = myfile1
            if myfile:
               newdoc.docfile2 = myfile2

            newdoc.price=request.POST.get('price','')
            newdoc.category= request.POST.get('category','')
            newdoc.isover18s =request.POST.get('isover18s','')
            newdoc.name =request.POST.get('name','')
            newdoc.description =request.POST.get('description','')

            form.clean_content()
            if myfile:
                newdoc.save(update_fields=['docfile'])
            if myfile1:   
                newdoc.save(update_fields=['docfile1'])
            if myfile2: 
                newdoc.save(update_fields=['docfile2'])
            if request.POST.get('price'):
                newdoc.save(update_fields=['price'])
            if request.POST.get('category'):
                newdoc.save(update_fields=['category'])
            if request.POST.get('isover18s'):
                newdoc.save(update_fields=['isover18s'])
            if request.POST.get('name'):
                newdoc.save(update_fields=['name'])
            if request.POST.get('description'):   
                newdoc.save(update_fields=['description'])

    document = Document.objects.get(id=document_id)
    form = DocumentForm() 
    return render_to_response(
        'edit_preview.html',
        {'document': document, 'form': form},
        context_instance=RequestContext(request)
    )

The Template form is way too big and messy that I can't seem to get it pasted in properly. Hope someone can help.

Thanks a million @knbk This is what I changed the view to and it works 100% now ...

def edit_preview(request, document_id):

    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        form.fields['docfile'].required = False
        form.fields['docfile1'].required = False
        form.fields['docfile2'].required = False
        form.fields['category'].required = False
        form.fields['price'].required = False
        form.fields['description'].required = False
        form.fields['name'].required = False

        if form.is_valid():

            newdoc = Document.objects.get(id=document_id)
            newdoc.usersname = request.user.username                


            if request.FILES.get('docfile'):
                myfile=request.FILES.get('docfile')
            else:
                myfile=None

            if request.FILES.get('docfile1'):
                myfile1=request.FILES.get('docfile1')
            else:
                myfile1=None

            if request.FILES.get('docfile2'):
                myfile2=request.FILES.get('docfile2')
            else:
                myfile2=None

            if myfile:
               newdoc.docfile = myfile
            if myfile1:
               newdoc.docfile1 = myfile1
            if myfile2:
               newdoc.docfile2 = myfile2


            newdoc.price=request.POST.get('price','')
            newdoc.category= request.POST.get('category','')
            newdoc.isover18s =request.POST.get('isover18s','')
            newdoc.name =request.POST.get('name','')
            newdoc.description =request.POST.get('description','')

            form.clean_content()
            if myfile:
                newdoc.save(update_fields=['docfile'])
            if myfile1:   
                newdoc.save(update_fields=['docfile1'])
            if myfile2: 
                newdoc.save(update_fields=['docfile2'])
            if request.POST.get('price'):
                newdoc.save(update_fields=['price'])
            if request.POST.get('category'):
                newdoc.save(update_fields=['category'])
            if request.POST.get('isover18s'):
                newdoc.save(update_fields=['isover18s'])
            if request.POST.get('name'):
                newdoc.save(update_fields=['name'])

            if request.POST.get('description'):   
                newdoc.save(update_fields=['description'])

            p =request.POST.get('docfile')


    document = Document.objects.get(id=document_id)
    form = DocumentForm() 
    return render_to_response(
        'edit_preview.html',
        {'document': document, 'form': form},
        context_instance=RequestContext(request)
    )
Dunski
  • 653
  • 5
  • 14

1 Answers1

1
newdoc = Document(docfile = request.FILES['docfile'])
...

newdoc = Document(docfile1 = request.FILES['docfile1'])
...

newdoc = Document(docfile2 = request.FILES['docfile2'])

This will raise a MultiValueDictKeyError if the key doesn't exist. Use request.FILES.get() with a default value.

knbk
  • 52,111
  • 9
  • 124
  • 122
  • Do you mean like this...newdoc =request.FILES.get('docfile') – Dunski Mar 31 '15 at 00:21
  • Yes, exactly. `MultiValueDict` (which `request.FILES` is) implements all the methods that `dict` has. – knbk Mar 31 '15 at 00:26
  • Great. That worked but now Im getting nMemoryUploadedFile' object has no attribute 'docfile' for the line 'if newdoc.docfile:' So how can I check if there is a file uploaded or not? Thanks so much for your help by the way – Dunski Mar 31 '15 at 00:35
  • Thanks a million @knbk. If I was able I would upvote you:) This is what I put in the end and all is working now if request.FILES.get('docfile'): myfile=request.FILES.get('docfile') else: myfile=None – Dunski Mar 31 '15 at 02:02
  • You can shorten that to `myfile = request.FILES.get('docfile', None)`, and the value will be `None` if the key doesn't exist. – knbk Mar 31 '15 at 09:47