首页 > 解决方案 > 让两个数据库模型显示在一个 html 页面上

问题描述

目前在一个网页上工作,我想显示两个不同的信息表,一个是针对单个食物的,另一个是针对食谱的,但是我只能让第一堂课来提取我测试过的任何信息,看看第一堂课是否类可以调出配方数据库,事实上,它目前还没有关于下一步要尝试什么的想法。

class SearchFoodResultsView(ListView):
    model = Food
    template_name = 'nutrihacker/search.html'
    context_object_name = 'food_list'




    # overrides ListView get_queryset to find names containing search term and pass them to template
    def get_queryset(self):

        query = self.request.GET.get('term')
        if (query == None):
            return Food.objects.all()
        else: # If there are any foods containing the query, they will be in the resulting object_list which is used by search.html in a for loop
            food_list = Food.objects.filter(
                Q(name__icontains = query)
            )


            return food_list


class SearchRecipeResultsView(ListView):
    model = RecipePreset
    template_name = 'nutrihacker/search.html'
    context_object_name = 'recipe_list'

    # overrides ListView get_queryset to find names containing search term and pass them to template
    def get_queryset(self):

        query = self.request.GET.get('term')
        if (query == None):
            return RecipePreset.objects.all()
        else: # If there are any recipes containing the query, they will be in the resulting object_list which is used by search.html in a for loop
            recipe_list = RecipePreset.objects.filter(
                Q(name__icontains = query)
            )


            return recipe_list

标签: pythondjango

解决方案


推荐阅读