【问题标题】:Django modal popup on a list link列表链接上的 Django 模式弹出窗口
【发布时间】:2021-08-20 16:25:39
【问题描述】:

我想开发一个列表链接上的弹出窗口(请看截图)点击。弹出窗口也将是一个列表。 到目前为止,这是我下面的代码 模型类:

class PSCInscpection(models.Model):
    vessel = models.ForeignKey(Vessel, on_delete=models.CASCADE, null=True)
    inspection_id = models.CharField(max_length=100, null=False, unique=True)
    followup_inspection = models.BooleanField(null=False, default=False)
    date = models.DateTimeField(null=True, blank=True)
    port = models.CharField(max_length=50, null=True, blank=True)
    country = models.CharField(max_length=50, null=True, blank=True)
    number_of_defects = models.PositiveIntegerField(null=True, blank=True)
    days_detained = models.PositiveIntegerField(null=True, blank=True)

class PSCInscpectionDefects(models.Model):
    pscinscpection = models.ForeignKey(PSCInscpection, on_delete=models.CASCADE, null=True)    
    inspection_id = models.CharField(max_length=100, null=False, blank=False)
    defect_code = models.CharField(max_length=50, null=True, blank=True)
    defect_text = models.CharField(max_length=255, null=True, blank=True)
    class_is_responsible = models.CharField(max_length=50, null=True, blank=True)
    defective_item_code = models.CharField(max_length=50, null=True, blank=True)

views.py

inspections = PSCInscpection.objects.filter(
        vessel_id=self.kwargs['pk']).order_by('-date')
    renderableInspections = []
    for _insp in inspections:            
        item = {
            "date": _insp.date.strftime("%Y-%m-%d"),
            "port": _insp.port,
            "country": _insp.country,
            "defects": _insp.number_of_defects,
            "detained": _insp.days_detained
        }
        renderableInspections.append(item)
    context['inspections'] = renderableInspections


    

我从这个“PSCInscpection”对象和弹出窗口绑定的列表将从“PSCInscpectionDefects”对象绑定。请问有什么帮助吗?谢谢!

view.html:

{% for inspection in inspections %}
        <tr>
          <td>{{ inspection.date }}</td>
          <td>{{ inspection.country }}</td>
          <td>{{ inspection.port }}</td>
          <td>
            {% if inspection.defects %}
              <a href="#">{{ inspection.defects }}</a>
            {% else %}
            {{ 0 }}
            {% endif %}
          </td>
          <td>{{ inspection.detained|default:0 }} Days</td>
        </tr>
        {% endfor %}

【问题讨论】:

    标签: python django modal-dialog popup


    【解决方案1】:

    您应该能够通过反向外键关系访问与每次检查相关的缺陷,但目前您不能这样做,因为您发送到模板的上下文不是对象本身的查询集。相反,您手动构建了一个数据结构来处理这个,这是不必要的。

    您应该将上下文作为您的对象查询集发送,这样您就可以使用inspections.pscinscpectiondefects_set.all 在模板中直接访问您的反向外键关系。这非常冗长,因此我建议将您的 PSCInscpectionDefects 模型中的 pscinscpection 字段的 related_name 属性设置为 defects 或类似的,以便您可以使用 inspections.defects.all 访问它。

    简化您的视图以返回过滤后的查询集。

    # views.py
    
    context['inspections'] = PSCInscpection.objects.filter(
            vessel_id=self.kwargs['pk']).order_by('-date')
    return context
    

    我不会为模态提供 HTML/CSS/JS 代码,因为这个问题与 Django 相关。但是当用户单击缺陷数量的链接时,您应该能够启动一个。这是假设设置了 related_name 的模板更改。

    # view.html
    
    {% for inspection in inspections %}
            <tr>
              <td>{{ inspection.date }}</td>
              <td>{{ inspection.country }}</td>
              <td>{{ inspection.port }}</td>
              <td>
          
          {% if inspection.defects %}
            <a href="#">{{ inspection.defects.count }}</a> <!-- link to modal here -->
            {% for defect in inspection.defects.all %}
              {{ defect }}
            {% endfor %}
          {% else %}
            {{ 0 }}
          {% endif %}
        </td>
        <td>{{ inspection.detained|default:0 }} Days</td>
      </tr>
    {% endfor %}
    

    【讨论】:

    • 非常感谢您的回复。请您发送 HTML/CSS/JS 代码。
    • 对不起,我没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 2011-02-28
    相关资源
    最近更新 更多