Python path _day105_django cache, signal, admin

Source: Internet
Author: User

First, the cache

Cache is to save some of the commonly used data in memory or memcache, in a certain period of time when someone to access the data, no longer to perform operations such as database and rendering, but directly from the memory or memcache cache to obtain data, and then returned to the user.

Django provides 6 cache in: (1) Developer Debug Cache, (2) memory cache, (3) file cache, (4) Database cache, (5) using the Python-memcached module's memcache cache ; (6) Use the memcache cache of the PYLIBMC module. The cache is often used for file caching and Memcache caching.

For specific cache usage, please refer to the blog: Http://www.cnblogs.com/yuanchenqi/articles/8034442.html#_label2, the following are examples of the three types of cache applications in Django:

1. Global use of caching

The global cache refers to the caching of views throughout the project, such as in the case of middleware, where the middleware is configured as follows: By configuring the following two middleware and cache expiration time, all view query data will be able to fetch data from the cache within the cache expiration time range.

middleware_classes = (    'django.middleware.cache.UpdateCacheMiddleware',                           #  all middleware starts ... ..... ..... ..... ...     .........................     ' Django.middleware.cache.FetchFromCacheMiddleware ',                        # at the end of all middleware ) cache_middleware_seconds=10                                                    # used to set the cache expiration time

2. The view uses the cache

The view cache is the only cache setting for the data within the specified view, primarily configured as follows: By giving the adorner @cache_page (cache time) to the view that needs to be cached, the view can be cached, as in the following example, the current time is displayed on the page, If you are accessing this view for a second time during the cache failure period, the time displayed on the page is the same.

 from Import Cache_page Import time@cache_page ()                                                                 # Cache failure Time 15s def Index (Request):     t=time.time ()     return render (request," index.html ", Locals ())

3. View Local Cache

View local cache refers to a piece of data in the view cache settings, such as, when refreshing the page, the entire page has a part of the implementation of the cache, the following example: The view of the local cache implementation is mainly in the HTML file settings, the following example, in the effective cache time range of two access to this page, The second time the page is displayed does not change.

View functions:

Import  Time def Index (Request):     t=time.time ()     return render (request,"index.html  ", Locals ())

HTML file:

{% Load cache%}#The first step<! DOCTYPE html>"en">"UTF-8"> <title>Title</title>

"Color:green"> do not cache: {{T}}{% Cache 10'name'%}#Step Two: Requires two parameters, the first is the cache time, the second is the name{% Endcache%}</body>

Second, the signal

The effect of the signal implementation is to listen to the specified action, when the specified action occurs after the corresponding operation, a bit with the meaning of the callback function. Django provides a lot of action signals, described as follows: When the specified signal is configured, the corresponding action occurs, the signal will execute its corresponding callback function.

Model Signals Pre_init#The Django modal automatically fires before executing its construction method.Post_init#when Django's modal executes its construction method, it automatically triggersPre_save#The Django Modal object is automatically triggered before it is savedPost_save#when the Django modal object is saved, it is automatically triggeredPre_delete#automatically triggers a Django modal object before it is deletedPost_delete#automatically triggers when a Django modal object is deletedM2m_changed#automatically triggers before and after the third table (Add,remove,clear) in the Django modal using the auto-Work fieldClass_prepared#when the program starts, detects registered apps in the modal class, for each class, automatically triggersManagement Signals Pre_migrate#automatic triggering before executing the migrate commandPost_migrate#automatically triggers after executing the migrate commandrequest/Response Signals request_started#Auto-Trigger before request arrivesRequest_finished#automatically triggered when the request is completeGot_request_exception#automatic triggering after an exception is requestedTest Signals setting_changed#automatically triggers when a configuration file is modified using test testsTemplate_rendered#automatically triggers when a render template is tested with testDatabase Wrappers connection_created#automatically triggers when a database connection is created

If you need to log entries in your project to add data to the database, you can use the post_save signal, which executes the callback function for the signal once the data is added to the project. Note that the signal settings are generally configured in the init.py of the application or project, ensuring that the signal is activated after the project is run. The following example:

Mode one: Signal binding relationship with callback

 from Import Pre_save, Post_save def Callback (sender,**Kwargs):   "
Execute save log logic, sender
and Kwargs parameters have the data you want
"" Print (sender) Print (Kwargs) Post_save.connect (callback)

Way two: Give callback and decorate the device

 from Import Post_save  from Import receiver@receiver (Post_save) def Callback (sender,**Kwargs):    print(sender)    print( Kwargs)

Three, admin management tools

For more information, refer to the blog: http://www.cnblogs.com/yuanchenqi/articles/8323452.html

Python path _day105_django cache, signal, admin

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.