Specify the View Cache method in URLconf in Django framework

Source: Internet
Author: User
This article describes how to specify the View Cache in URLconf under the Django framework. Django is the most popular among the various Python web frameworks, if you need it, you can see that the view is coupled with the cache system, which is not ideal in several aspects. For example, you may want to reuse the View function on a site without caching, or you may want to publish the view to those who do not want to use it through caching. The solution to these problems is to specify the View Cache in URLconf, rather than the view function itself.

It is very easy to do this: a cache_page is simply wrapped when these View functions are used in URLconf. Here is the URLconf you have just used: this is the previous URLconf:

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

The following is the same URLconf, but the my_view is wrapped in cache_page:

From django. views. decorators. cache import cache_pageurlpatterns = ('', (r' ^ foo/(\ d {1, 2})/$ ', cache_page (my_view, 60*15 )),)

If you use this method, do not forget to import cache_page in URLconf.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.