自定义上下文处理器中的Django缓存
我有一个自定义上下文处理器,用于生成动态菜单和当前应用程序列表,站点范围:自定义上下文处理器中的Django缓存,django,caching,Django,Caching,我有一个自定义上下文处理器,用于生成动态菜单和当前应用程序列表,站点范围: from portal.models import * def base_items(request): return { ... 'app_items': App.objects.filter(isonline=True), 'menu_items': MainMenu.objects.all().order_by('position'), }
from portal.models import *
def base_items(request):
return {
...
'app_items': App.objects.filter(isonline=True),
'menu_items': MainMenu.objects.all().order_by('position'),
}
编辑:
My template(请注意,这些URL中有许多在Django框架之外,并且根据语言的不同而有所不同。因此需要将它们硬编码到db字段中:
<ul class="menu">
{% for item in menu_items %}
{% if LANGUAGE_CODE = "en-us" %}
<li><a title="{{ item.title_en }}" href="{{ item.url_en }}">{{ item.title_en }}</a>
<ul>
{% for subitem in item.submenu_set.all %}
<li><a title="{{ subitem.title_en }}" href="{{ subitem.url_en }}">{{ subitem.title_en }}</a></li>
{% endfor %}
</ul>
</li>
{% else %}
<li><a title="{{ item.title_es }}" href="{{ item.url_es }}">{{ item.title_es }}</a>
<ul>
{% for subitem in item.submenu_set.all %}
<li><a title="{{ subitem.title_es }}" href="{{ subitem.url_es }}">{{ subitem.title_es }}</a></li>
{% endfor %}
</ul>
</li>
{% endif %}
{% endfor %}
</ul>
{菜单项中的项的%u%}
{%if LANGUAGE_CODE=“en-us”%}
-
{item.submenu_set.all%}中的子项为%
{%endfor%}
{%else%}
-
{item.submenu_set.all%}中的子项为%
{%endfor%}
{%endif%}
{%endfor%}
我的问题是-如何缓存这些不会频繁更改的结果
我已经尝试了@cache\u页面装饰器,但是我可以看到我的页面仍然在访问菜单项对象的db
非常感谢您的帮助。您可以使用django。请注意,您可能不得不这样做
或,您可以使用这些查询集,因为。您可以使用django。请注意,您可能不得不这样做
或,您可以使用这些查询集,因为。尝试将lamdas表达式返回到模板:
'app_items': lambda: App.objects.filter(isonline=True),
这样它就不会被编译/缓存并动态工作。尝试将lamdas表达式返回到模板:
'app_items': lambda: App.objects.filter(isonline=True),
这样它就不会被编译/缓存并动态工作。hmmm。我尝试了模板缓存,但它不考虑语言状态。请参阅编辑。您可以通过将语言传递到{%cache%}templatetag来缓存与当前语言相关的模板片段。如下所示:
{%cache 600欢迎语言\u code%}{%trans“欢迎访问example.com”%}{%endcache%}
hmmm。我尝试了模板缓存,但它不考虑语言状态。请参阅编辑。您可以通过将语言传递到{%cache%}templatetag来缓存与当前语言相关的模板片段。如下所示:{%cache 600欢迎语言\u code%}{%trans“欢迎访问example.com”%}{%endcache%}