cache decorators in urls.py error

107 views
Skip to first unread message

Phang Mulianto

unread,
Apr 7, 2012, 10:53:57 AM4/7/12
to django-users
Hi,

i have my apps and try to use cache decorator in url.py but got error.

Here are the urls.py

from django.views.decorators.cache import cache_page

urlpatterns = patterns('article.views',
    #(r'^$','index', { 'template_name': 'blog/public_list.html'}, 'index'),
    (r'^$',cache_page('index'), { 'template_name': 'blog/public_list.html'}, 'index'),
)

the error is :
cache_page must be passed a view function if called with two arguments

what is wrong in the url.. note i also use the DDT (django debug toolbar) .

THanks for pointing me out..

Tom Evans

unread,
Apr 10, 2012, 9:42:11 AM4/10/12
to django...@googlegroups.com

Phang Mulianto

unread,
Apr 13, 2012, 8:51:48 AM4/13/12
to django...@googlegroups.com
Hi ,

i already try (r'^$',cache_page(index), { 'template_name': 'blog/public_list.html'},
> 'index'),

and even :

(r'^$',cache_page(index()), { 'template_name': 'blog/public_list.html'},
> 'index'),

but different error comes out...
i don't know whats wrong.. anyone can help point it out..






Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Tom Evans

unread,
Apr 13, 2012, 9:27:27 AM4/13/12
to django...@googlegroups.com
On Fri, Apr 13, 2012 at 1:51 PM, Phang Mulianto <brav...@gmail.com> wrote:
> Hi ,
>
> i already try

Randomly trying things is unlikely to fix anything.

cache_page decorates a function. Therefore, if this doesn't work:

> (r'^$',cache_page(index), { 'template_name':
> 'blog/public_list.html'},
>> 'index'),
>

then probably the error is that you have not imported your view
function. It's hard to tell, as you just say 'different errors come
out'.

> and even :
>
> (r'^$',cache_page(index()), { 'template_name': 'blog/public_list.html'},
>> 'index'),
>

This one is nonsense. Your function 'index' is to be called when it is
to be run. Here you are trying to decorate the return value of the
function, without passing it any arguments - which is probably one of
the 'different errors'.

> but different error comes out...
> i don't know whats wrong.. anyone can help point it out..
>

If you want to decorate a view in your urls.py, you must import the
view function, and then refer to the view function by name. Like in
the example I linked you to previously…

https://docs.djangoproject.com/en/1.3/topics/cache/#specifying-per-view-cache-in-the-urlconf

So if your urlconf looked like this:

urlpatterns = ('',
(r'^foo/(\d{1,2})/$', my_view),
)

then to add the cache_page decorator to that view, you make it like this:

urlpatterns = ('',
(r'^foo/(\d{1,2})/$', cache_page(my_view, 60 * 15)),
)

If you continue to have problems, then you will have to tell us what
those problems are. "It doesn't work", or "different errors" is not
good enough, you must tell us word-for-word what those errors are.

Cheers

Tom

Phang Mulianto

unread,
Apr 13, 2012, 10:38:57 AM4/13/12
to django...@googlegroups.com
Hi Tom,

i already fix it, it is because

"then probably the error is that you have not imported your view
function. It's hard to tell, as you just say 'different errors come
out'."

i just realize i not import my view in the url.py , as in documentation is not said i have to import it first before use it in url.py.

somebody gonna fix the documentation ?

it only remind you "If you take this approach, don't forget to import cache_page within your URLconf." 
please add "and import your view function to be called also" 

anyway.. thank Tom for the light..

Regards,
Mulianto


Cheers

Tom

Reply all
Reply to author
Forward
0 new messages