0

i'm new in django and i have a problem with Uploading File Please Help me!! :X

here is my view.py

def uploadimg(request):

try:

   user = request.session['user']        

   if request.method == 'POST':

       form = User_image_form(request.POST, request.FILES)
       #if form.is_valid():
       usr_img = User_image(imgfile = request.FILES['imgfile'])
       return HttpResponse("yes")
       #usr_img.user = user
       usr_img.save()
       return HttpResponse("yees the first upload is right !! :X")
   else:
       return HttpResponse("Noooooo!!!")

except:

   pass

this is my form.py

class User_image_form(forms.Form):

   imgfile = forms.FileField()

and this is my models.py

class User_image(models.Model):

   imgfile = models.ImageField(upload_to = 'User-Image')
   #user = models.ForeignKey(User_account)

and i have problem in view.py at line which

usr_img = User_image(imgfile = request.FILES['imgfile'])

and it's never get to the

return HttpResponse("Yes")

error:

Exception Value: The view User.views.uploadimg didn't return an HttpResponse object.

Plz Help

1 Answer 1

1

If there is an exception, you are not returning an HttpResponse object. Hence the error.

use form.is_valid() to see if the form is valid. Something like this:

if request.method == 'POST':
    form = User_image_form(request.POST, request.FILES)
    if form.is_valid():
        usr_img = User_image(imgfile = form.cleaned_data['imgfile'])
        usr_img.user = user
        usr_img.save()
        return HttpResponse("yees the first upload is right !! :X")
    else:
        print form.errors #for debugging purposes only. 

    return HttpResponse("Noooooo!!!")
Sign up to request clarification or add additional context in comments.

8 Comments

i put the HttpResponse("yes") befor the error line and it was ok !
rather than enclosing the whole block in try..except, makeit as localized as possible. example put the try..except around exactly where the erro might occur.
is there a problem in usr_img = User_image(imgfile = request.FILES['imgfile']) ???
just edited the answer. . Looks fine, but you might need to check validity
@karthikr if the form is not valid it will still not return HttpResponse You might want to decrease indent for inner HttpResponse
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.