Answer the question
In order to leave comments, you need to log in
How to fix adding images via form?
Hello, I have two forms - "Create" and "Edit". In the create form, the image is attached, but not in the edit form, while the framework does not give any errors.
Please tell me where to dig? Maybe there are some subtleties in working with images that I don't know about?
Create form listing
def post_new(request):
if request.method == "POST":
form = PostForm(request.POST)
if form.is_valid():
farm = form.save(commit=False)
farm.owner = request.user
farm.published_date = timezone.now()
farm.save()
return redirect('blog.views.post_detail', id=post.id)
else:
post = PostForm()
return render(request, 'posts/post_edit.html', {'form': form})
def post_edit_main(request, id):
post = get_object_or_404(Post, id=id)
if request.method == "POST":
form = PostEditMain(request.POST, instance=post)
if form.is_valid():
post = form.save(commit=False)
post.owner = request.user
post.published_date = timezone.now()
post.save()
return redirect('post.views.own_farm_detail', id=farm.id)
else:
form = PostEditMain(instance=farm)
return render(request, 'posts/post_edit_main.html', {'form': form})
class Post(models.Model):
owner = models.ForeignKey('auth.User')
title = models.CharField(max_length=100,verbose_name=u'Название')
image = models.ImageField(upload_to="{{ MEDIA_ROOT }}",
verbose_name=u'Заглавное фото', help_text='Большое', blank=True, null=True)
<form method="POST" class="post-form">{% csrf_token %}
{{ form|bootstrap}}
<button type="submit" class="save send">Save</button>
</form>
Answer the question
In order to leave comments, you need to log in
PostEditMain(request.POST, request.FILES, instance=farm)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question