Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
Image is not uploaded via django form

My Views

def apost(request):
    if request.method =='POST':
        form = APostForm(request.POST, request.FILES)
        if form.is_valid():
            form = form.save(commit=False)
            form.slug = slugify(form.title)
            form.save()
            return redirect('apost')
    else:
        form = APostForm()

    template_name = 'dadmin/form.html'
    items = Post.objects.all()
    context = {'title':'Add Post','form':form,'items':items}
    return render (request, template_name, context)

My Form

class APostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = {'title','photo','content'}

Models

photo = models.ImageField(upload_to='images')

No Image uploaded is Accepted Photo is selected but when Click save. It shows this field is required error. I had searched through the questions here but request.FILES solves others problems but not mines. Whats wrong?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

you should use in template where you are uploading form:

    <form class="form-horizontal form_middle" enctype='multipart/form-data' method="POST">
#apply logic for media upload
</form>
about 4 years ago · Santiago Trujillo Report

0

in views.y, the form you are saving should also have request.FILES

studentProfileForm = StudentRegisterForm(request.POST, request.FILES)
    if studentProfileForm.is_valid():
        user = studentProfileForm.save()
about 4 years ago · Santiago Trujillo Report

0

File upload is a bit weird in model forms in django. Change your forms.py to -

class APostForm(forms.ModelForm):
    photo=forms.FileField(label='Upload image') # or image field
    class Meta:
        model = Post
        fields = {'title','content'}

form.save() will automatically save the field.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!