我无法使用modelformset工厂更新我的帖子

时间:2018-04-02 00:55:33

标签: python django python-3.x django-views

我正在使用两个名为Posts和PostPicture的模型,帖子模型有多个文本字段,而PostPicture模型有一个图像字段和一个发布外键字段,我在视图中的图像字段上使用formset_factory让用户上传多个图像帖子,问题是,每当我尝试更新帖子时,它都会更新帖子模型的文本字段,但它不会更新我上传的图片...我的更新视图出现了问题我无法弄清楚

这是我的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})

这是我的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>

0 个答案:

没有答案