Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:51:11+00:00 2026-06-12T22:51:11+00:00

I am unable to upload file if there are multiple fields in my model

  • 0

I am unable to upload file if there are multiple fields in my model other than file field. I didn’t get any error on the page but the page simply displays same page without uploading of my image.

Here is my model

class ImageUpload(models.Model):
   username = models.ForeignKey(User)
   imagefile = models.FileField(upload_to='pictures')
   isapproved = models.BooleanField()
   class Meta:
       db_table = u'imageupload'

   def __unicode__(self):
       return u'%s' % (self.imagefile)

Here is my model form

class UploadImageForm(forms.ModelForm):
   def __init__(self,user, *args, **kwargs):
       super(UploadImageForm, self).__init__(*args, **kwargs)
       self.fields['username'] = forms.CharField(initial = user,
                                       widget = forms.TextInput(
                                     attrs = {'readonly':'readonly'}))
   class Meta:
       model = ImageUpload
       exclude = ('id', 'isapproved')

Here is my view where I am handling the data.

def imageupload(request):
# Handle file upload
   if request.method == 'POST':
      list = ["jpeg", "jpg","JPG","JPEG","png","PNG"]
      format = imghdr.what(request.FILES['imagefile'])
      if format not in list:
          error = "Please upload a valid file format"
          form = UploadImageForm(request.user)
          return HttpResponseRedirect(reverse('myview.views.imageupload'))
      else:
          form = UploadImageForm(request.POST, request.FILES)
          if form.is_valid():
              newdoc = ImageUpload(imagefile = request.FILES['imagefile'])
              newdoc.save()
          return HttpResponseRedirect(reverse('myview.views.imageupload'))
  else:
      form = UploadImageForm(request.user) # A empty, unbound form
  documents = ImageUpload.objects.all()
  return render_to_response('imageupload.html',
       {'documents': documents, 'form': form,'media':settings.MEDIA_URL},
        context_instance=RequestContext(request)
   )

It used to work properly when I have used only file field in the Model Form. After adding more fields to the model, its not uploading the file. Any suggestion on how to fix this issue?

Thanks in advance
Vikram

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:51:12+00:00Added an answer on June 12, 2026 at 10:51 pm

    Just add an else block to your view for form validation. I think that your form is not valid and you do not just print any errors in your template.

    if form.is_valid():
        newdoc = ImageUpload(imagefile = request.FILES['imagefile'])
        newdoc.save()
    else:
        # do something visible here
    

    Also if your form is valid you could just use form.save() to save the image and the related data instead of creating new ImageUpload object.

    Also, your model requires the User object to be saved.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting a Unable to load the requested file: default.php error in codeigniter
For some reason, I'm unable to upload a file with the <asp:FileUpload /> control.
Suppose I upload a file in a bucket and there are already some 1000
Error: Unable to read data from the transport connection: A blocking operation was interrupted
I'm trying to upload a file to my server using the CakePHP file upload
Literally, I am unable to upload anything to my server via the normal HTML
I've been trying to do simple file uploads but I am unable to find
Recently I am developing website baker's module. There is one option to upload image.
I understand IE9 and below don't support file uploads for inputs with multiple files.
I just tested this simple php file upload script. Works in all browsers except

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.