我有三个要添加搜索功能的Django应用程序。在每个搜索功能中,我需要传递三件事。 Selected
,title
和url
,我想在需要时添加一些详细信息。我想我可以压缩此代码,但我不知道如何。
这是我的搜索功能:
文章搜索:
def search(request):
return render(request, "article/search/search.html", {
"selected": "news",
"title": "Suche | Artikel",
"categories": Article.objects.all().order_by("Category").values_list("Category__title", flat=True).distinct(),
"exists": {k._meta.verbose_name.title(): k.objects.all().exists() for k in [Image, Audio, Video]},
"any_exists": Image.objects.all().exists() or Audio.objects.all().exists() or Video.objects.all().exists(),
"url": reverse("ajax:news_article_search")
})
民意调查:
def search(request):
return render(request, "poll/search/search.html", {
"selected": "news",
"title": "Suche | Umfrage",
"not_solved_exist": Poll.get_all_able(request).exists(),
"must_verified_exist": Poll.get_all_able(request).filter(must_verified=True).exists(),
"for_me_exist": Poll.get_all_able(request).exists(),
"url": reverse("ajax:poll_poll_search")
})
谜语搜索:
def search(request):
return render(request, "riddle/search/search.html", {
"selected": "news",
"title": "Suche | Rätsel",
"not_solved_exist": Riddle.objects.filter(solvedsolution__user=request.user).exists() if request.user.is_authenticated else Riddle.objects.filter(solvedsolution__ip=get_client_ip(request)).exists(),
"url": reverse("ajax:riddle_riddle_search")
})