首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django模板中的问题

Django模板中的问题
EN

Stack Overflow用户
提问于 2017-04-21 07:28:23
回答 1查看 35关注 0票数 0

实际上,我知道这在Django的模板中是不可能的,但是我想执行{{ Perception.objects.filter(loan__request__customer__pk = 207) }}

下面是我用于该模板的视图:

代码语言:javascript
复制
class PerceptionIndexView(StaffRestrictedMixin, FrontendListView):
    page_title = _('Perception')
    model = Perception
    template_name = 'loanwolf/perception/index.html'
    pjax_template_name = 'loanwolf/perception/index.pjax.html'
    row_actions_template_name = 'loanwolf/perception/list-actions.inc.html'
    url_namespace = 'perception'

    def active(self, obj):
        if obj.is_active:
            return icon(obj.get_icon(), css_class='green-text', tooltip=_('Active'))
        else:
            return icon(obj.get_icon(), css_class='red-text', tooltip=_('Inactive'))

    def notes_count(self, obj):
        return obj.notes.count()
    notes_count_label = _('Notes')

    def get_change_url(self, obj):
        return obj.get_absolute_url()

    def my_view(self, A_pk):
        filter_perceptions= Perception.objects.filter(loan__request__customer__pk=A_pk)
        return render_to_response('../template/loanwolf/perception/list-view-#2.inc.html', {'filter_perceptions': filter_perceptions})

我尝试使用不同路径的render_to_response,但到目前为止什么也没有发生。知道我的视图位于loanwolf/perception中,而我的模板list-view-#2.inc.html位于loanwolf/templates/loanwolf/perception中。此外,重要的是要知道FrontendListView使用ListView。我想我不能使用“render_to_response”,但它并不清楚。

有没有其他的解决方案,让我可以在我的模板中访问列表Perception.objects.filter(loan__request__customer__pk)

提前感谢!

EN

回答 1

Stack Overflow用户

发布于 2017-04-21 07:32:44

my_view中的代码放入名为get_context_data的方法中,如下所示:

代码语言:javascript
复制
def get_context_data(self, **kwargs):
    context = super(PerceiptionIndexView, self).get_context_data(**kwargs)
    filter_perceptions = Perception.objects.filter(loan__request__customer__pk=...)
    context["filter_perceptions"] = filter_perceptions
    return context

然后,将通过{{filter_perceptions}}在您的模板中提供filter_perceptions

如果您需要一些稍微不同的东西,请查看文档中的更多示例。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43531522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档