gpt4 book ai didi

python - Django templateTag 中的上下文 KeyError

转载 作者:太空宇宙 更新时间:2023-11-03 19:15:34 27 4
gpt4 key购买 nike

我的应用程序的 templatetag 代码因上下文变量中缺少键 (page) 引发 KeyError。在我的模板中,我不使用 context.variableKeyName 引用上下文变量,我仅引用variableKeyName(例如 {% if is_pagination %})。在我的模板中,我可以毫无异常(exception)地引用关键页面

我应该如何将上下文及其所需的键获取到我的模板标签中?

<小时/>

详细信息如下:

我正在使用 django-profiles 返回一些配置文件的列表:

url(r'^profiles/$', 'profiles.views.profile_list', 
kwargs={ 'paginate_by':10 },
name='profiles_profile_detail'),

这里调用这段代码: https://bitbucket.org/ubernostrum/django-profiles..

在我的模板中,我在调用 templatetag 之前测试 {% if is_pagination %}:

{% if is_paginated %}{% load paginator %}{% paginator 3 %}{% endif %}

(我使用的模板标签灵感来自 http://www.tummy.com/.../django-pagination/ 为 django 1.3 更新 http://djangosnippets.org/snippets/2680/ )

但这会导致“分页”的 KeyError。

最佳答案

http://djangosnippets.org/snippets/2680/ 的文档(类里面的评论)说:

Required context variables: paged: The Paginator.page() instance.

它也用在模板标签中:

paged = context['paged']

您需要提供此上下文变量才能使此模板标记正常工作。我认为最好的选择是复制 profiles.views.profile_list View 的代码并添加此上下文变量。不幸的是,它仍然是一个基于函数的 View - 否则扩展它会更干净、更容易。

关于python - Django templateTag 中的上下文 KeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11435253/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com