如何将never_cache应用于第三方Django应用的网址? [英] How do you apply never_cache to a third party Django app's urls?

查看:159
本文介绍了如何将never_cache应用于第三方Django应用的网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 urls.py 中使用 @never_cache 装饰器。我可以使用

  url(r'^ about /',never_cache(TemplateView.as_view(template_name =about_us.html ))),

但是很多次我使用外部应用程序,我所做的只是 url('',include(shop.urls))一个例子是 django-oscar 。现在我在这种情况下应用never_cache吗?

解决方案

您可能会从 django-snippets

从django.views.decorators.cache导入never_cache 

def never_cache_patterns(prefix,* args):
pattern_list = [],tterns,
for args:
if isinstance(t,(list,tuple)):
t = url(prefix = prefix,* t)
elif isinstance(t,RegexURLPattern):
t.add_prefix(前缀)

t._callback = never_cache(t.callback)
pattern_list.append(t)

return pattern_list


urlpatterns = never_cache_patterns('',
(r'foo / $','myview')

看起来不像有办法以更清洁的方式。



如果要将其应用于另一个应用程序的一组网址,您无法编辑,那么您可以尝试:

  uncached_pa​​tterns = never_cache_patterns('',
url(r'^ $',include('shop.urls')),


urlpatterns = patterns('',
include(uncached_pa​​tterns),
#其他模式
#eg。 url(r'^ myapp /',include('myapp.urls',namespace =myapp)),
#...

/ pre>

I'm trying to use the @never_cachedecorator in the urls.py. I think one can use

url(r'^about/', never_cache(TemplateView.as_view(template_name="about_us.html"))),

but a lot of times I am using external apps and all I do is url('', include(shop.urls)) One example is django-oscar. Now do I in this case apply never_cache to that?

解决方案

You might find luck with this approach from django-snippets:

from django.conf.urls.defaults import url
from django.views.decorators.cache import never_cache

def never_cache_patterns(prefix, *args):
    pattern_list = [], tterns,
    for t in args:
        if isinstance(t, (list, tuple)): 
            t = url(prefix=prefix, *t)
        elif isinstance(t, RegexURLPattern):
            t.add_prefix(prefix)

        t._callback = never_cache(t.callback)
        pattern_list.append(t)

    return pattern_list


urlpatterns = never_cache_patterns('',
    (r'foo/$', 'myview')
)

It doesn't look like there is a way to do it in a cleaner fashion.

If you want to apply it to a set of urls from another app which you cannot edit then you can try:

uncached_patterns = never_cache_patterns('',
   url(r'^$', include('shop.urls')),
   )

urlpatterns = patterns('',
    include(uncached_patterns),
    # other patterns
    # eg. url(r'^myapp/', include('myapp.urls', namespace="myapp")),
    # ...
)

这篇关于如何将never_cache应用于第三方Django应用的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆