【问题标题】:In Django REST Framework, how to access kwargs context in html template? {{ view.kwargs.foo }} doesn't work for me在 Django REST Framework 中,如何在 html 模板中访问 kwargs 上下文? {{ view.kwargs.foo }} 对我不起作用
【发布时间】:2023-03-12 19:58:02
【问题描述】:

Referring this answer

我无法通过 {{ view.kwargs.foo }} 访问 html 模板中的 kwargs。不知道为什么,是不是因为 DRF 有些不同,所以我需要不同的语法来访问它?

我的 html 模板('polls/character_description_list.html'):

{% block content %}
<table>
    {% for description in descriptions %}
    <tr>
        <td><b>{{ description.title }}</b></td>
        <td>{{ description.content }}</td>
    </tr>
    {% endfor %}
</table>

<form action="{% url 'polls:description_detail_create_from_character' %}">
    <input type="hidden" value="{{ view.kwargs.character_id }}" name="character_id"> <!-- This is where I attempt to access the kwargs but can't get it, although I can attempt to output it anywhere else for debugging -->
    <input type="submit" value="New Description"/>
</form>
{% endblock %}

因此在提交时我希望去:

http://localhost:8000/myapp/description_detail_create_from_character/?character_id=1

但实际上缺少 id:

http://localhost:8000/myapp/description_detail_create_from_character/?character_id=

为了检查我正在寻找的 character_id 令牌是否在 kwargs 中,我确实尝试在 get_serializer_context 中设置断点(使用 PyCharm):

    def get_serializer_context(self):
        context = super(CharacterDescriptionListView, self).get_serializer_context()
        return context

检查上下文,我可以找到 'view' -> kwargs -> 'character_id',具有我期望的值,所以它应该可以工作。

这是我的 views.py

# Already has a chain of hierarchy but basically it descends from generics.ListCreateAPIView
class CharacterDescriptionListView(DescriptionViewMixin, CustomNovelListCreateView):
    template_name = 'polls/character_description_list.html'

    def get_filter_object(self):
        return get_object_or_404(Character, id=self.kwargs['character_id'])

    def get_queryset(self):
        characterObj = self.get_filter_object()
        return Description.objects.filter(character=characterObj)

【问题讨论】:

  • 尝试使用{% debug %}检查模板中的上下文。
  • 我不确定您为什么在这里使用 DRF 视图。为什么不是标准的 Django 视图?
  • 什么是CustomNovelListCreateViewDescriptionViewMixin?我不明白你为什么认为有一个 view 上下文变量,但也许这些类提供了它......你唯一需要的就是将 id 添加到你的上下文中。如果您查看类有 get_context_data() 方法,请覆盖该方法并将变量添加到那里的上下文中。然后,您可以像访问任何其他上下文变量一样在模板中访问它。
  • @Alasdair 哇,我不知道!经过反复试验,我得到了答案:是的,如果使用 DRF 获取上下文的方式不同 - 它类似于 {{ serializer.context.character_id }}。关于我遇到的问题以及它是如何解决的,你值得“接受的答案”:-)
  • @DanielRoseman 是的,这是一个很好的问题,我正在通过 Django 进行学习,并且听说如果我以后可能会使用 API 的话,最好早点使用 DRF。 TBH 它给了我比我使用标准曲线更深的学习曲线,但即使只是 html 也有一些优点,主要是序列化程序。您认为 html 的 DRF 最糟糕的是什么?我很好奇:)

标签: python django django-rest-framework


【解决方案1】:

这是因为我使用的是 DRF 的 generics.ListCreateAPIView。在这种情况下,访问我正在寻找的上下文的语法应该是:

{{ serializer.context.character_id }}

感谢@Alasdair,我了解到我可以在 html 模板中{% debug %} 后发现。


这可以通过检查 DRF 中GenericAPIView 的源代码来确认——它源自View 但不是TemplateView,因此它没有get_context_data。因此对于GenericAPIView 只能访问{{ serializer.context.character_id }} 这样的上下文,而不能访问{{ view.kwargs.character_id }}

【讨论】:

    猜你喜欢
    • 2016-05-25
    • 2015-09-08
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2014-05-25
    • 2021-09-07
    相关资源
    最近更新 更多