【问题标题】:remove an element from select once added to the db (filtering)添加到数据库后从选择中删除一个元素(过滤)
【发布时间】:2021-11-26 16:49:44
【问题描述】:

发布代码和照片,因为我不知道如何解释这个问题。 我有这个绿色按钮,它打开一个模式,允许我创建一个组并在单击添加组后将其保存在数据库中。在数据库中添加对象后,我使用通过模式输入的数据重新加载页面,如果再次单击绿色按钮,我可以添加另一个对象。我总是可以在select中添加相同的对象的问题,我希望一旦添加就不再有可能添加它。

表格

class EserciziForm(forms.ModelForm):
    class Meta:
        model = models.DatiEsercizi
        exclude = ['gruppo_single']
    
class GruppiForm(forms.ModelForm):
    class Meta:
        model = models.DatiGruppi
    exclude = ['gruppi_scheda']

html

{% extends "base.html" %}
{% load widget_tweaks %}
{% block content %}

  <section class="container mt-3">
    <div class="d-flex align-items-center justify-content-between">
      <h1 class="nome-scheda">CREA</h1>
      <a href="{% url 'lista-gruppi' %}" class="btn btn-outline-primary">LISTA</a>
    </div>
    <hr>
    <div class="scheda mb-4">
      <div class="d-flex justify-content-between align-items-center">
        <h4 style="margin-bottom: 0;">Nome: {{ scheda.nome_scheda }}</h4>
        <div>
          <p style="margin-bottom: 0;">
            <span><strong>Inizio: {{ scheda.data_inizio }}</strong></span> |
            <span><strong>Fine: {{ scheda.data_fine }}</strong></span>
          </p>
        </div>
      </div>
    </div>
    <div class="box">
      {% for gruppo in scheda.gruppi_scheda.all %}
        <div class="gruppo mb-3">
          <div class="d-flex justify-content-between align-items-center">
            <p style="margin-bottom: 0;">{{ gruppo.dati_gruppo.nome_gruppo }}</p>
            <p style="margin-bottom: 0;">{{ gruppo.giorni_settimana }}</p>
          </div>
        </div>
      {% endfor %}

      <div class="text-end">
        <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#gruppo">Aggiungi gruppo</button>
      </div>
    </div>
  </section>

  <!-- Modal -->
  <div class="modal fade" id="gruppo" tabindex="-1" aria-labelledby="gruppoLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-xl">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="gruppoLabel">Aggiungi gruppo</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <form method="post">
            {% csrf_token %}
            <div class="gruppo mb-3">
              <div class="d-flex justify-content-between align-items-center">
                  <div class="d-flex align-items-center">
                    {{ gruppo_form.giorni_settimana.label_tag }}
                    {{ gruppo_form.giorni_settimana }}
                  </div>
                  <div class="d-flex align-items-center">
                    {{ gruppo_form.dati_gruppo.label_tag }}
                    {{ gruppo_form.dati_gruppo }}
                  </div>
                  
              </div>
            </div>
            <div class="text-end">
              <button type="submit" class="btn btn-success">Aggiungi gruppo</button>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Chiudi</button>
        </div>
      </div>
    </div>
  </div>
{% endblock content %}  

查看

def creazione(request, nome):
    scheda = get_object_or_404(Schede, nome_scheda = nome)
    if request.method == "POST":
        gruppo_form = GruppiForm(request.POST, prefix = 'gruppo')
        if gruppo_form.is_valid():
            gruppo = gruppo_form.save(commit = False)
            gruppo.gruppi_scheda = scheda
            gruppo.save()
            return HttpResponseRedirect(request.path_info)
    else:
        gruppo_form = GruppiForm(prefix = 'gruppo')

    context = {'scheda' : scheda, 'gruppo_form' : gruppo_form}
    return render(request, 'crea/passo2.html', context) 

【问题讨论】:

    标签: django django-models django-views django-forms django-templates


    【解决方案1】:

    我总是可以在选择中添加相同的对象的问题,我希望一旦添加就不再有可能添加它。

    如果问题的根源在于没有在模态中再次显示保存的数据,那么您需要确保保存的数据没有从views.py 传递到模板。

    更简单的解决方案是修改views.py 中的方法,以便在提交数据时将其从gruppo_form 中取出。这样,它将删除保存的数据,并且不会按要求显示。您还可以检查表单对象以查看该数据是否已存在,请参阅here,如果存在则不要将其返回到模板。

    【讨论】:

    • 我怎样才能从表单中删除它们?
    • @riccardotest 你不能为此使用过滤器吗? Django 过滤器,因此您只在选择中过滤掉该值,您可以在外部访问它。
    • 不是很实用,仍然是初学者我需要一个例子,在那个链接上我找不到我的问题的解决方案
    • @riccardotest this 是 Django 模板中过滤的示例。如果不清楚,我建议为此查找 django 文档。一旦你了解了它们的工作原理,你就可以过滤掉这些值
    猜你喜欢
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    相关资源
    最近更新 更多