简体   繁体   English

Django - 通用 UpdateView 以一种形式工作,但不能以其他形式工作

[英]Django - generic UpdateView works in one form but not in other

I am working on a web application for my school project and I chose Django and all the tutorials found online were pretty comprehensive and I manage to get to some point to where I am stuck.我正在为我的学校项目开发一个 Web 应用程序,我选择了Django ,网上找到的所有教程都非常全面,我设法解决了我遇到的问题。

I have the following class based view trying to update an object:我有以下class based view试图更新一个对象:

views.py

class MyPostUpdateView(UpdateView):
    model = Post
    fields = ('VideoURL', 'MainDescription')
    template_name = 'myapp/editpost.html'

Then, I imported in my urls.py :然后,我在我的urls.py导入:

path('profile/account/my-posts/<int:pk>/edit/', MyPostUpdateView.as_view(),
     name='myapp-editpost')

Then, in my HTML template I have a several <form> elements but all belongs to the same <div> element:然后,在我的HTML模板中,我有几个<form>元素,但都属于同一个<div>元素:

<div class="row">
  <div class="col-12 col-lg-7">
    <form method='post' class="card shadow-soft border p-4 mb-4">
      {% csrf_token %}
      <h5 class="mb-4">Form1</h5>
      <div class="form-group">
        <label for="MainDescription">Titlu</label> {{form.MainDescription|add_class:"form-control shadow-soft"}}
      </div>
      <div class="row">
        <div class="col">
          <button class="btn btn-primary btn-dark mt-2 animate-up-2" type="submit">Update form
                                1</button>
        </div>
      </div>
    </form>
  </div>
  <div class="col-12 col-lg-5">
    <form method='post' class="card shadow-soft border p-4 mb-4">
      {% csrf_token %}
      <h5 class="mb-4">Form2</h5>
      <div class="form-group">
        <label for="VideoURL">Video URL:</label> {{form.VideoURL|add_class:"form-control shadow-soft"}}
      </div>
      <div class="row">
        <div class="col">
          <button class="btn btn-primary btn-dark mt-2 animate-up-2 text-right" type="submit">Update form 2</button>
        </div>
      </div>
    </form>
  </div>
</div>

The behavior is the following: I cannot update any of the two fields if I leave the HTML code as above.行为如下:如果我保留上述HTML代码,我将无法更新这两个字段中的任何一个。 However, if I bring together the two fields, in the same <form> element, it works:但是,如果我将这两个字段放在同一个<form>元素中,它会起作用:

<div class="row">
  <div class="col-12 col-lg-7">
    <form method='post' class="card shadow-soft border p-4 mb-4">
      {% csrf_token %}
      <h5 class="mb-4">Form1</h5>
      <div class="form-group">
        <label for="VideoURL">Video URL:</label> {{form.VideoURL|add_class:"form-control shadow-soft"}}
      </div>
      <div class="form-group">
        <label for="MainDescription">Titlu</label> {{form.MainDescription|add_class:"form-control shadow-soft"}}
      </div>
      <div class="row">
        <div class="col">
          <button class="btn btn-primary btn-dark mt-2 animate-up-2" type="submit">Update form
                                1</button>
        </div>
      </div>
    </form>
  </div>
  <div class="col-12 col-lg-5">
    <form method='post' class="card shadow-soft border p-4 mb-4">
      {% csrf_token %}
      <h5 class="mb-4">Form2</h5>
      <div class="form-group">
        <label for="VideoURL">Video URL:</label> {{form.VideoURL|add_class:"form-control shadow-soft"}}
      </div>
      <div class="form-group">
        <label for="MainDescription">Titlu</label> {{form.MainDescription|add_class:"form-control shadow-soft"}}
      </div>
      <div class="row">
        <div class="col">
          <button class="btn btn-primary btn-dark mt-2 animate-up-2 text-right" type="submit">Update form 2</button>
        </div>
      </div>
    </form>
  </div>
</div>

Could anyone explain it to me?谁能给我解释一下? I am asking because I want to extend the UpdateView functionality to other fields belonging to the same model.我问是因为我想将UpdateView功能扩展到属于同一模型的其他字段。 Thanks!谢谢!

In your first block of code, you have put the two fields into two different forms.在您的第一个代码块中,您将两个字段放入了两种不同的形式。 When you are submitting the form, only the one which was submitted is taken.当您提交表单时,只会使用已提交的表单。

So it is like placing 2 apples in the first bucket and 2 oranges in the second bucket.所以这就像在第一个桶里放 2 个苹果,在第二个桶里放 2 个橙子。

Imagine you have changed the number of apples and oranges to 1 and 1. Am asking you to bring only one bucket(you can only bring one bucket).想象一下,你把苹果和橙子的数量分别改为 1 和 1。我要求你只带一桶(你只能带一桶)。 What happens?发生什么了? Number of apples is 1, but I have no idea whether you have changed the number of oranges in the second bucket.苹果的数量是 1,但我不知道您是否更改了第二个桶中的橙子数量。

Place the apples and oranges in one bucket so that when you bring me the bucket, I can clearly know how many apples and oranges were changed.把苹果和橙子放在一个桶里,这样当你把桶拿来的时候,我就能清楚地知道换了多少苹果和橙子。

apples = content of videourl苹果 = videourl 的内容

oranges = maindescription橙子 = 主要描述

bucket = form桶 = 形式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM