【发布时间】:2019-01-14 01:04:25
【问题描述】:
我的模型 LandingSnippet 包含属性...model = CharField()...,它与上下文关键字相关(例如下面context 中的cars)
我的视图中有下一个代码
def GeneratedLanding(request):
snippets = LandingSnippet.objects.all().filter(view_on=True).order_by('order')
context = {
'snippets':snippets,
...
'cars':Car.objects.all(), # this is cars
...
return render(request,'qlanding/generateindex.html',{'context':context})
我如何通过关键字cars 将上下文中的querySet cars 作为字符串获取
例如
{{context}}
打印
{'snippets': <QuerySet [<LandingSnippet: Snippet1Title>, <LandingSnippet: 2 - about - Лучшая служба развозки детей>]>, 'services': <QuerySet []>, 'cars': <QuerySet []>, 'faqs': <QuerySet []>}
和
{{snippet.model}}
打印
cars
问题:
我怎样才能得到{{ context.cars }} ?我想像context[snippet.model] where sn-p.model='cars'
我想在包含时将其推送到另一个模板中
{% if snippet.module %}
{% with "qlanding/snippets/module/"|add:snippet.module|add:".html" as path %}
{% include path with model=context[snippet.model] %} # But this is incorect while rendering
{% endwith %}
{% endif %}
【问题讨论】:
-
我可以看看你的模型吗?
-
返回渲染(request,'qlanding/generateindex.html',context)
-
@MohammadAli 这里是我的模型文件dropmefiles.com/l1dFv
标签: django django-templates django-template-filters