在模板中使用CreateWithInlinesViews渲染的django额外视图

时间:2020-07-19 19:50:31

标签: django django-views django-templates

比方说,我有以下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>

现在的问题是:我需要两个内联函数,即在单个模板中HTML代码的不同部分中的ItemInline和ContactInline。该如何解决?

1 个答案:

答案 0 :(得分:0)

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

$.each( sArray, function(i, k) {

   $.ajax({
       success: function(data){
          
          log(i, k);

          // returns unordered
          // [2] ELEMENT TWO
          // [1] ELEMENT ONE
          // [3] ELEMENT THREE
          // [5] ELEMENT FIVE
          // [4] ELEMENT FOUR
          // etc...
       }
    });
});