django - 如何在 django 模板中迭代查询集字典

标签 django dictionary django-templates

这是我从 View 中获得的字典:

{'questions_in_topic': <QuerySet [{'question_id__description': 'Describe your most significant leadership experience'}, {'questi
on_id__description': 'Which kind of leader are you?'}]>}

如何在 django 模板中显示单个问题?

View 如下:

def get_question_from_topic(request):
    if request.method == "GET":
        questions_in_topic = QuestionTopic.objects.filter(topic_id=request.GET['topicId']).values('question_id__description').order_by('question_id__description')
        print(questions_in_topic)
        context = { 'questions_in_topic': questions_in_topic }
        print(context)
        return render(request, 'recruiter/add_question_library.html', context)
    return render(request, 'recruiter/add_question_library.html', context)

最佳答案

{% if questions_in_topic %}
{% for question in questions_in_topic %}
    Question: {{ question }}
{% endfor %}
{% endif %}

关于django - 如何在 django 模板中迭代查询集字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52094738/

相关文章:

list - R中的字典数据结构

python - iter,值,字典中的项目不起作用

python - 以dict形式访问类属性 python django

python-3.x - 如何将计算变量添加到 wagtail StructBlock 模板上下文中?

Django - 发生服务器错误。请联系管理员

database - 用户创建的动态数据库 - django

django - AttributeError : 'WSGIRequest' object has no attribute 'session'

python - 使用子查询注释计数

python - 反转 '' *'',参数 '()' 和关键字参数 '{}' 未找到

python - 无法在 Django 测试中登录刚刚创建的用户