python - 让两个数据库模型显示在一个 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
解决方案
推荐阅读
- java - Spring Boot 测试:如何使用 AOP 部分模拟服务
- java - Java - 将方法调用传递给方法
- android - 如何在 Android Studio 中使用 Kotlin 和 React Native?
- html - 如何保持两个div彼此相邻?
- tomcat - 访问 Tomcat 外部图像的网络磁盘上下文问题
- javascript - 为什么当非承诺抛出错误时 Promise.all 不拒绝?
- python - 如何在列表中拆分字符串
- angular - Angular+Springboot+WSO2 身份服务器:401 未授权错误
- python - Heroku 中使用 gunicorn 的 Flask 应用程序的服务器请求超时错误
- php - Instagram Basic Display API 和刷新 Access Token 的精度