Django如何用多个对象保存窗体?

2024-09-28 22:22:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我用的是Python3.7,Django 2.2。你知道吗

下面你可以找到我的代码,更好的理解我的问题。你知道吗

正如您可以在下面看到我的代码一样,我创建了一个包含一些记录的表。跳过中值的名称型号.py. 我们只需要关注经理的接受。此值有2个参数(检查文件选择.py). 你知道吗

当我用def manager\u day\u free\u accept创建视图时,我得到了多个对象。最后一列包含choice,我的用户必须选择并保存它。我在下面的屏幕上放了一个例子。 我有什么问题。 我有两种方法,因为用户会选择哪种解决方案对他们更友好:) 我的按钮有问题,我应该把它们放在哪里?因为现在,submition改变了整个对象,但是使用了错误的值。你知道吗

当我把按钮放在“for loop”外面时,对象什么也没有发生。你能帮我吗,我得找到两个解决办法:

第一名: 每一行都应该包含带有提交和拒绝的绿色按钮(白色按钮)。当用户点击绿色按钮时,只有这个raw中的值才会更新db中的对象。。你知道吗

第二溶胶: 用户在每一行中选择“接受”和“拒绝”。之后,用户应该按submit按钮,所有更改都应该更新db中的对象。你知道吗

你知道吗型号.py你知道吗

class Wniosek_urlop(models.Model):
    data_zloz_wniosku = models.DateTimeField(auto_now_add=True, null=True)
    data_start_urlop = models.DateField('Urlop od', blank=False, null=True)
    data_end_urlop = models.DateField('Urlop do', blank=False,null=True)
    #Representant - osoba zastepująca w tym czasie nieobecnego pracownika.
    representant = models.CharField('Zastępca', max_length=50, null=False,blank=False,
                                    help_text="Podaj imie i nazwisko osoby zastępującej Cię w czasie nieobecności. To pole będzie widoczne przez kierownika")
    reason = models.TextField('Uzasadnienie', max_length=255, blank=False, null=False, help_text="Powód złożenia wniosku", error_messages={'blank': "Wpisz tutaj coś"})
    note_optional = models.TextField('Uwagi', max_length=255, null=True, blank=True)
    urlop = models.CharField('Urlop', choices=urlop_ch, default=1, max_length=50)
    manager = models.CharField('Przełożony', choices=manager_ch, default=0, max_length=50)
    manager_accpt = models.CharField('Akceptacja', choices=accpt_ch, null=True, blank=True, max_length=25)
    worker = models.ForeignKey(Lista, on_delete=models.CASCADE, null=True, blank=True)

你知道吗视图.py你知道吗

def manager_day_free_accpt(request):
    db_all_free_days = Wniosek_urlop.objects.all().order_by('id')

    #Pole wyboru y_n
    form_accpt = Manager_accpt_y_n(request.POST or None)
    if form_accpt.is_valid():
        form_accpt.save()
        messages.success(request, 'Zapisano zmiany.')
    else:
        messages.success(request, 'Ups. Cos poszło nie tak.')

    return render(request, "manager_accpt.html", {'db_all_free_days': db_all_free_days, 'form_accpt': form_accpt})

你知道吗选择.py你知道吗

manager_ch = [("1", "manager 1"),
              ("2", "manager 2")
               ]

accpt_ch = [
    ("1", "accept"),
    ("2", "reject")
]

经理_接受.html你知道吗

<table class="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">Pracownik</th>
      <!--Skip some col-->
      <th scope="col">Przełożony</th>
      <th scope="col"></th>
    </tr>
  </thead>
  <tbody>


  {% for tmp in db_all_free_days %}
    {%if tmp.manager_accpt == null %}
    <tr>
      <th scope="row">{{tmp.id}}</th>
      <td>{{tmp.worker}}</td>
      <td>{{tmp.manager}}</td>
      <td>
          <form role="form">

            {% csrf_token %}
            {{form_accpt|bootstrap}}
<button type="submit" class="btn btn-success">Wyślij wniosek&nbsp<i class="far fa-save"></i></button>
  <button type="button" class="btn btn-outline-secondary" onclick="goBack()">Cofnij</button>
          </form>
      </td>
    </tr>
    {% else %}
    <div class="alert alert-dark" role="alert">
  No free days to accept
    </div>
  {% endif %}
  {% endfor %}

屏幕-页面上的结果: enter image description here


Tags: formfalsetruefreedbmodelsmanager按钮