0

I'm trying to add a simple file upload page on my site. Whenever I submit the form, it says "This field is required" above the file selector, as if it's not passing it. Here is my form, model, and view.

forms.py

from django import forms
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response

class UploadFileForm(forms.Form):

    MURRMACMOD = "MMM"
    CUBE67 = "C67"
    CUBE20 = "C20"
    VDINCONN = "VDC"
    IMPACT20 = "I20"
    IMPACT67 = "I67"
    CAT_CHOICES = (
        (MURRMACMOD, 'Murrelektronik Mac Module'),
        (CUBE67, 'Cube67/Cube67'),
        (CUBE20, 'Cube20'),
        (VDINCONN, 'Valve DIN Connectors'),
        (IMPACT20, 'Impact 20'),
        (IMPACT67, 'Impact 67')
    )

    article = forms.CharField(max_length=100)
    title = forms.CharField(max_length=100)
    file = forms.FileField()
    cat = forms.ChoiceField(choices=CAT_CHOICES)

models.py

from django.db import models

class File(models.Model):

    article = models.CharField(max_length=100)
    title = models.CharField(max_length=100)
    file = models.FileField()
    cat = models.CharField()

views.py

from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext

from files.forms import UploadFileForm

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/success/url/')
    else:
        form = UploadFileForm()

    ctxt_dict = {
        'form': form,
    }
    context = RequestContext(request, ctxt_dict)
    return render_to_response('upload.html', context_instance=context)

I would love some help guys, thanks.

EDIT: Here is the template.

{% if user.is_staff %}
<form action="." method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
{% else %}
{% endif %}
</form>
  • May you kindly add also your template (at least the form part and all potentially form-related javascipt code)? Thanks :) – furins Mar 26 '13 at 22:37
  • @furins I didn't think it mattered, but I added it. – Konstantin Keller Mar 26 '13 at 22:54
  • Now that I see it I agree, it does not matter :) thank you - PS you should close the form tag inside the if..else..endif statement or you'll have an orphan close tag – furins Mar 26 '13 at 22:57
  • :P Sure thing. I just don't see why it's not working. – Konstantin Keller Mar 26 '13 at 22:58
  • 1
    form.save is not enough. You have to instantiate your class with request.FILES content as shown in point 4 of this answer http://stackoverflow.com/questions/5871730/need-a-minimal-django-file-upload-example – furins Mar 26 '13 at 23:07

1 Answers1

1

In Django Template , if you are trying to render a form that has a filefield . You must pass replace

form method="post" action="">

with

form method="POST" enctype="multipart/form-data">

loop over forms in formset and a list at same time + FileField required error :django

Community
  • 1
  • 1
donkeyboy72
  • 1,883
  • 10
  • 37
  • 55