Alex_Silva
Alex_Silva

Reputation: 11

FileField not processed on form POST

When trying to upload a file using a FileField, the form doesn't post the file data to the server. It works perfectly if you use a text field, but for some reason it doesn't recognize the file, as it doesn't show up on request.FILES, or request.POSTS.

MEDIA_ROOT and MEDIA_URL configuration:

MEDIA_ROOT = '/home/grove/pootleImages/'
MEDIA_URL = '/pootleImages/'

get_unit_context decorator in decorators.py:

def get_unit_context(permission_codes):

def wrap_f(f):

    @wraps(f)
    def decorated_f(request, uid, *args, **kwargs):
        unit = get_object_or_404(
                Unit.objects.select_related("store__translation_project",
                                            "store__parent"),
                id=uid,
        )
        _common_context(request, unit.store.translation_project,
                        permission_codes)
        request.unit = unit
        request.store = unit.store
        request.directory = unit.store.parent

        return f(request, unit, *args, **kwargs)

    return decorated_f

return wrap_f

My forms.py method:

def unit_image_form_factory(language):

    image_attrs = {
        'lang': language.code,
        'dir': language.direction,
        'class': 'images expanding focusthis',
        'rows': 2,
        'tabindex': 15,
    }


    class UnitImageForm(forms.ModelForm):

        class Meta:
            fields = ('image',)
            model = Unit

        # It works if using a CharField!
        #image = forms.CharField(required=True,
          #                                   label=_("Image"),
           #                                  widget=forms.Textarea(
            #                                 attrs=image_attrs))
        image= forms.FileField(required=True, label=_('Image'),
                                             widget=forms.FileInput(
                                             attrs=image_attrs))
        def __init__(self, *args, **kwargs):
            self.request = kwargs.pop('request', None)
            super(UnitImageForm, self).__init__(*args, **kwargs)


        def save(self):
            super(UnitImageForm, self).save()


    return UnitImageForm

My models.py snippet:

class Unit(models.Model, base.TranslationUnit):
    # ...
    # ...
    # It works if using a TextField!
    #image = models.TextField(null=True, blank=True)
    image = models.FileField(upload_to=".", blank=True, null=True)
    # ...
    # ...

My urls.py snippet:

url(r'^xhr/units/(?P<uid>[0-9]+)/image/?$',
    'image',
    name='pootle-xhr-units-image'),

My views.py method:

@require_POST
@ajax_required
@get_unit_context('translate')
def image(request, unit):
    """Stores a new image for the given ``unit``.

    :return: If the form validates, the cleaned image is returned.
             An error message is returned otherwise.
    """
    # Update current unit instance's attributes
    unit.uploaded_by = request.profile
    unit.uploaded_on = timezone.now()

    language = request.translation_project.language
    form = unit_image_form_factory(language)(request.POST, request.FILES, instance=unit,
                                               request=request)

    if form.is_valid():
        form.save()
        context = {
            'unit': unit,
            'language': language,
        }
        t = loader.get_template('unit/image.html')
        c = RequestContext(request, context)

        json = {'image': t.render(c)}
        rcode = 200
    else:
        json = {'msg': _("Image submission failed.")}
        rcode = 400

    response = simplejson.dumps(json)

    return HttpResponse(response, status=rcode, mimetype="application/json")

My HTML template for the image upload:

<div id="upload-image">
    <form enctype="multipart/form-data" method="post" action="{% url 'pootle-xhr-units-image' unit.id %}" id="image-form">
    {% csrf_token %}
    <input type="file" name="image" id="id_image" />
    <p><input type="submit" value="{% trans 'Upload' %}" /></p>
    </form>
</div>

When the form is instantiated, request.POST does not return the file browsed by the user, neither request.FILES. form.errors just returns "This field is required" The form object returns the following:

<tr><th><label for="id_image">Image:</label></th><td><ul class="errorlist"><li>This field is required.</li>
</ul><input lang="pl" rows="2" name="image" id="id_image" type="file" class="images expanding focusthis" dir="ltr" tabindex="15" /></td></tr>

And when the user clicks the submit button, the following POST error occurs:

"POST /xhr/units/74923/image HTTP/1.1" 400 35

I could bypass it by including required=False to the image property, but the file is not posted anyway.

More output debug information:

POST when fileField is required=True:

Status Code: 400 BAD REQUEST Form Data: csrfmiddlewaretoken: yoTqPAAjy74GH

form.errors:

"msg": "

  • image
  • This field is required.
  • "}

    If change required=True to required=False:

    Status Code: 200 OK Form Data: csrfmiddlewaretoken: yoTqPAAjy74GH

    But the imagefield still doesn't show up in the form data.

    Thank you,

    Alex


    I added a gist hub containing all files related to this problem, to ease visualization:

    https://gist.github.com/alex-silva/40313734b9f1cd37f204

    Upvotes: 1

    Views: 431

    Answers (2)

    Alex_Silva
    Alex_Silva

    Reputation: 11

    Finally managed to get it to work in some way. The form wasn't working at all when using it in the main page, so I created a link to a blank page containing just the upload image form. When separate from the main page, the upload works fine. Then I just redirect to the main page after uploading. Why the form doesn't work in the main page, that's a mystery.

    Upvotes: 0

    anonymous
    anonymous

    Reputation: 1

    It looks like you've forgotten to add the {% csrf_token %} in your form. Add that between the tag.

    OR...

    You can add the csrf_exempt decorator to your processing view:

    from django.views.decorators.csrf import csrf_exempt
    from django.http import HttpResponse
    
    @csrf_exempt
    def my_view(request):
        return HttpResponse('Hello world')
    

    More info: https://docs.djangoproject.com/en/dev/ref/contrib/csrf/

    Upvotes: 0

    Related Questions