在Django模板中访问自定义列表

时间:2019-07-08 19:13:30

标签: django python-3.x

在我看来,我创建了如下列表:

dict = {"visited": "positions"}

try:
    positions = Position.objects.filter(company=request.user.userprofile.company, status='ACTIVE')
    positions_dict = {}
    for k, position in enumerate(positions):
        try:
            hiringPos = HiringPosition.objects.get(position=position)
        except HiringPosition.DoesNotExist:
            hiringPos = None
        positions_dict[k] = {"position": position, "hiringPos": hiringPos}

except Position.DoesNotExist:
    positions_dict = None

dict['positions'] = positions_dict
print(dict['positions'])
return render(request, 'user_view.html', context=dict)

当我将其打印到控制台时,它会像这样返回:

{0: {'position': <Position: Position object (1)>, 'hiringPos': None}}

有人可以告诉我如何在user_view.html模板中进行迭代吗?我在那里:

{% for obj in positions %}

{% endfor %}

但是任何尝试都没有打印出诸如obj.positionpositions[obj]positions.obj ...之类的东西。

我的Django版本:2.2.2和python版本:3.6.4

0 个答案:

没有答案