[英]How to combine a QuerySet and a Dictionary in Django Template?
I have Django an model object that looks like this: 我有一个Django的模型对象,看起来像这样:
class myObj(models.Model):
A = models.TextField()
B = models.DateField()
C = models.ForeignKey(User)
I am obtaining a querySet of myObjs like this: 我正在获得像myObjs的querySet:
mList = myObj.objects.all()
I also have a dictionary of Users that looks like this: 我也有一个用户字典,看起来像这样:
uDict = {1: "Hello", 2: "World"} //The keys of this dictionary correspond to the field myObj.C
In my view I am sending both the mList and the uList to my HTML template using the Context. 在我看来,我正在使用上下文将mList和uList发送到我的HTML模板。
In my template, I would like to list all the mLists on separate lines. 在我的模板中,我想在单独的行中列出所有mLists。 Those mLists that have a matching User entry in the uDict should appear with the matching value from the uList.
在uDict中具有匹配的User条目的那些mList应该与uList中的匹配值一起出现。
How should I do this? 我应该怎么做? In the template I can easily iterate through the mList.
在模板中,我可以轻松地遍历mList。 But I don't know how to match the myObj.C to the uDict key.
但是我不知道如何将myObj.C与uDict键匹配。 I know that this won't work:
我知道这行不通:
{% for currObj in mList %}
{{currObj.A}} {{currObj.B}} {{uDict.currObj.C}}
{% endfor %}
... because uDict.currObj.C won't evaluate correctly. ...因为uDict.currObj.C无法正确评估。
So what's the best way to do what I'm trying to do? 那么做我想做的最好的方法是什么?
You can't use a for loop variable as a key to a dictionary in Django unfortunately. 不幸的是,您不能使用for循环变量作为Django中字典的键。
What you can do is package them all in some list or other data set. 您可以做的是将它们全部打包在某个列表或其他数据集中。 Something like this:
像这样:
mList = myObj.objects.all()
uDict = {1: "Hello", 2: "World"}
data_for_template = []
for i in range(len(mList)):
data_for_template.append(
{
'mListObjA':mList.A,
'mListObjB':mList.B,
'uDictObj':uDict[i], # or i+1 for your example uDict
}
)
Then in your template you'll be able to access this data in a similar fashion. 然后,您可以在模板中以类似方式访问此数据。
{% for item in data_for_template %}
{{item.mListObjA}} {{item.mListObjB}} {{item.uDictObj}}
{% endfor %}
Essentially you should try and do most of the prep work for your data in your views.py
file to simplify the accessibility in the template. 本质上,您应该尝试对
views.py
文件中的数据进行大部分准备工作,以简化模板中的可访问性。
EDIT: 编辑:
Something I would try next, in light of your comment: 根据您的评论,我接下来将尝试以下操作:
views.py: views.py:
mList = myObj.objects.all()
uDict = {1: "Hello", 2: "World"}
data_for_template = []
for i in range(len(mList)):
data_for_template.append(
{
'mListObj':mList,
'uDictObj':uDict[i], # or i+1 for your example uDict
}
)
html HTML
{% for item in data_for_template %}
{{item.mListObj.A}} {{item.mListObj.B}} {{item.uDictObj}}
{{item.mListObj.C.F.G}}
{% endfor %}
{{data_for_template.N.mListObj.C.F.G}} <!-- Where N is the index of whatever Object you wish to access -->
I think the simplest approach is to add the uDict
values directly to the myObj
instances in your view. 我认为最简单的方法是将
uDict
值直接添加到视图中的myObj
实例。 This will give you a list rather than a qs in the context, but that shouldn't make any difference in your template. 这将为您提供一个列表,而不是上下文中的qs,但这不会对模板造成任何影响。
In your view: 您认为:
mList = myObj.objects.all()
for my_obj in mList:
my_obj.user = uDict.get(my_obj.C)
This adds the user values to the in-memory instances, so in the template you can get currObj.user
. 这会将用户值添加到内存实例中,因此在模板中可以获取
currObj.user
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.