[Answer]-Django Image Upload form not displaying error messages

1👍

You are you just redirecting when the form is not valid. So any errors will be lost, and nothing will happen. I suggest you combine the two views. (no reason to keep them separate.) Then you can return to the same view when the form is not valid. Here is my rough implementation: (not tested)

def upload_image_view(request):
    if request.method=='GET':
        form=ImageForm()
        # TODO: Limits to images uploaded by current user
        my_images = UploadedImage.objects.filter(owner=User.objects.get(username=request.user))
        return render_to_response(
            'uploader.html',
            {'my_images': my_images, 'form': form,},
            context_instance=RequestContext(request)
        )
    elif request.method=='POST':
        form = ImageForm(request.POST, request.FILES)
        if form.is_valid():
            u= User.objects.get(username=request.user)

            public_form = PublicImageForm(request.POST, request.FILES)
            if public_form.is_valid():
                newdoc = UploadedImage(imagefile = request.FILES['imagefile'], owner=u, isPublic=True)
            else:
                newdoc = UploadedImage(imagefile = request.FILES['imagefile'], owner=u, )

            newdoc.save()

            return HttpResponseRedirect('/workspace/upload_image/')
        else:
            my_images = UploadedImage.objects.filter(owner=User.objects.get(username=request.user))
            return render_to_response(
                'uploader.html',
                {'my_images': my_images, 'form': form,},
                context_instance=RequestContext(request)
            )
    else:
        return HttpResponseNotAllowed(['GET', 'POST'])

Leave a comment