带Django的Bootstrap手风琴:如何仅为开放式手风琴部分加载数据?

时间:2016-10-17 03:02:22

标签: python django twitter-bootstrap django-templates templatetags

我正在尝试制作一个网页,其中会以自举式手风琴的形式显示食谱(see here)。 这就是我现在这样做的方式:

<div class="panel-group" id="accordion">
    {% for recipe in recipes %}
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#accordion" href="#collapse{{ forloop.counter }}">
                    {{ recipe }}
                </a>
            </h4>
        </div>
        <div id="collapse{{ forloop.counter }}" class="panel-collapse collapse">
            <div class="panel-body">
                <table class="table table-hover">
                    {% for ingredient in  foodtype|ingredients_in_recipe:recipe %}
                        <tr>
                            <td>
                                {{ ingredient.ingredient_name }}
                            </td>
                            <td>
                                {{ ingredient.ingredient_quantity }}
                            </td>
                        </tr>
                    {% endfor %}
                    <p>{{ recipe.details }}</p>
                </table>
            </div>
        </div>
    </div>
    {% endfor %}
</div>

我为此制作了一个自定义模板标签:

@register.filter
def ingredients_in_recipe(foodtype, recipe):
    return foodtype.ingredient_set.filter(recipe=recipe).order_by("ingredient_name")

问题是我有200多个食谱并加载所有这些数据太慢了。理想情况下,模板标记函数ingredients_in_recipe应仅在用户单击配方时调用。但是根据我的理解,这是不可能的,因为Django运行它然后将呈现的HTML发送给用户。

无论如何我可以绕过这个问题,同时仍然保持手风琴的风格,如图所示?

提前致谢, 最大

编辑:以下是我的观点

def detail(request, foodtype_id):
     foodtype = get_object_or_404(foodtype, id=foodtype_id)
     recipe = foodtype.recipe_set.values_list('recipe').order_by('recipe').distinct()
     context = {
         'foodtype': foodtype,
         'recipe': recipe,
     }
     return render(request, 'main/detail.html', context)

1 个答案:

答案 0 :(得分:1)

在到达模板之前总是更好地做这个逻辑。如果您在成分上设置顺序然后您不必在模板中订购它们会怎样?这是否有效并改善了性能?

class Ingredient(models.Model):
  ...

  class Meta:
    ordering = ['ingredient_name']


<div class="panel-group" id="accordion">
    {% for recipe in recipes %}
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#accordion" href="#collapse{{ forloop.counter }}">
                    {{ recipe }}
                </a>
            </h4>
        </div>
        <div id="collapse{{ forloop.counter }}" class="panel-collapse collapse">
            <div class="panel-body">
                <table class="table table-hover">
                    {% for ingredient in recipe.ingredient_set.all %}
                        <tr>
                            <td>
                                {{ ingredient.ingredient_name }}
                            </td>
                            <td>
                                {{ ingredient.ingredient_quantity }}
                            </td>
                        </tr>
                    {% endfor %}
                    <p>{{ recipe.details }}</p>
                </table>
            </div>
        </div>
    </div>
    {% endfor %}
</div>