【问题标题】:django extra views with CreateWithInlinesViews rendering in templatedjango 在模板中渲染 CreateWithInlinesViews 的额外视图
【发布时间】:2020-07-19 19:50:31
【问题描述】:

比方说,我有以下 django 代码......

views.py

from extra_views import CreateWithInlinesView, UpdateWithInlinesView, InlineFormSetFactory


class ItemInline(InlineFormSetFactory):
    model = Item
    fields = ['sku', 'price', 'name']


class ContactInline(InlineFormSetFactory):
    model = Contact
    fields = ['name', 'email']


class CreateOrderView(CreateWithInlinesView):
    model = Order
    inlines = [ItemInline, ContactInline]
    fields = ['customer', 'name']
    template_name = 'order_and_items.html'

    def get_success_url(self):
        return self.object.get_absolute_url()

在html模板中:

<form method="post">
  ...
  {{ form }}

  {% for formset in inlines %}
    {{ formset }}
  {% endfor %}
  ...
  <input type="submit" value="Submit" />
</form>

现在的问题是:我需要两个内联,即 ItemInline 和 ContactInline 在单个模板中的 html 代码的不同部分。这个应该怎么解决?

【问题讨论】:

    标签: django django-views django-templates


    【解决方案1】:

    解决方案:在模板的 forloop 中使用 Counter

    <form method="post">
      ...
      {{ form }}
    
      {% for formset in inlines %}
          {% if forloop.counter == 1 %}
                Item:
               {{ formset }}
          {% elif forloop.counter == 2 %}
               Contact:
               {{ formset }}
      {% endfor %}
      ...
      <input type="submit" value="Submit" />
    </form>
    

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 2019-09-24
      • 2011-05-08
      • 1970-01-01
      • 2016-07-27
      • 2022-07-22
      • 2021-03-04
      • 1970-01-01
      • 2011-09-22
      相关资源
      最近更新 更多