问题
i am using two models named Posts and PostPicture and posts model have multiple text fields whereas PostPicture model have one image field and one posts foreignkey field and i am using formset_factory on image field in views to let the user upload multiple images with the post, the problem is that whenever i try to update the post it updates the text fields of Posts model but it doesn't update the images i upload with it...there's something wrong with my update view and i can't figure out
Here's my views.py for update_post
def update_post(request, post_id):
ImageFormSet = modelformset_factory(PostPicture,
form=AddPictures, extra=5, min_num=1,
can_delete=True)
post_form = get_object_or_404(Posts, pk=post_id)
pics_form = PostPicture.objects.filter(post=post_id)
if request.method == "POST":
form = AddFile(request.POST, instance=post_form)
formset = ImageFormSet(request.POST or None, queryset=pics_form)
if form.is_valid() and formset.is_valid():
posting = form.save(commit=False)
posting.user = request.user
posting.save()
for form in formset.cleaned_data:
image = form['image']
photo = PostPicture(post=posting, image=image)
photo.save()
messages.success(request, 'Post updated Successfully!')
return render(request, 'vehicles_app/addfile.html', {
'form': form,
'pics_form': pics_form,
'formset': formset,
'post_form': post_form,
'post_id': post_id,
})
else:
messages.success(request, 'Post not updated Successfully!')
print(form.errors, formset.errors)
else:
form = AddFile(instance=post_form)
formset =
ImageFormSet(queryset=PostPicture.objects.filter(post=post_id))
return render(request, 'vehicles_app/update_post.html', {'form':
form, 'formset': formset, 'pics_form': pics_form,
'post_form':post_form,'post_id': post_id})
here's my update_post.html
<form id="post_form" method="post" action="{% url
'vehicles_app:update_post' post_id %}"
enctype="multipart/form-data">
{% csrf_token %}
<div class="row">
<div class="form-group">
<div style="float: left; width: 50%">
<label for="title">Ad Title</label>
</div>
<div class="title" style="float: right; width: 45%">
<input type="text" name="title" placeholder="Type Here"
value='{{ form.initial.title }}' required>
</div>
</div>
<br>
<div class="form-group">
<div style="float: left; width: 50%">
<label for="city">City</label>
</div>
<div class="form-group">
<div style="float: left; width: 50%">
<label for="mileage">Mileage</label>
</div>
<div class=mileage style="float: right; width: 45%">
<input type="text" name="mileage" placeholder="ie:50000"
value='{{ form.initial.mileage }}' required>
</div>
</div>
</div>
<br>
<div class="form-group">
<div style="float: left; width: 50%">
<label for="details">Type details here</label>
</div>
<div class="details" style="float: right; width: 45%">
<textarea name="details" rows="9" cols="45" value='{{
form.initial.details }}' required></textarea>
</div>
</div>
<br>
{{ formset.management_form }}
{% if formset %}
{% for form in formset %}
{% if form %}
<table class="display" cellspacing="1" width="100%">
<tr>
<td>{{ form }}</td>
</tr>
</table>
{% endif %}
{% endfor %}
{% endif %}
<input type="submit" name="submit" value="Update"
class="btn btn-info" id="post_btn">
<input type=button class="btn btn-default" value="Cancel"
onClick="javascript:history.go(-1);">
</form>
来源:https://stackoverflow.com/questions/49604076/i-cant-update-my-post-using-modelformset-factory