Fixing Site-Wide Caching in Django

Posted on Sept. 18, 2012

Caching in Django is elegant and simple, especially the site-wide cache for anonymous users. But if you use Google Analytics and other external cookies, it might not be working the way you intended. Read this tutorial to find out how to make your site as fast as it can be.

By enabling the site-wide cache in Django, you probably intended for pages every page to be cached for every anonymous users. That's the point, at least. The problem is, the page cache is set to Vary: Cookie. That means that if you have a session cookie or anything of the like, the cache will be different for each user.

Learning Django? Subscribe to my Django articles, tips and tutorials.

Fortunately, that's not a problem with Django itself, since anonymous users won't have cookies set. But it is a problem if you're using Google Analytics. That's because Google Analytics sets cookies that trigger the site-wide cache's Vary: Cookie condition. Luckily, fixing this is as easy as using a small piece of middleware and a touch of inheritance.

Let's create a new middleware file, call it and place it in your project root. Add the following snippet to the file1:

from django.middleware.cache import UpdateCacheMiddleware

import re

class SmartUpdateCacheMiddleware(UpdateCacheMiddleware):                        
    STRIP_RE = re.compile(r'\b(_[^=]+=.+?(?:; |$))')

    def process_request(self, request):                                         
        cookie = self.STRIP_RE.sub('', request.META.get('HTTP_COOKIE', ''))   
        request.META['HTTP_COOKIE'] = cookie

So what does this do? It replaces the middleware that comes with Django. Before passing on the cookies to the rest of the application, it strips out the cookies set by Google Analytics. That way, the Vary: Cookie condition won't be triggered, as your application won't see any of these cookies. To deploy these changes, simply find your middleware classes in, and replace django.middleware.cache.UpdateCacheMiddleware with middleware.SmartUpdateCacheMiddleware. Your middleware settings should look like so:

MIDDLEWARE_CLASSES = (                                                        
    # All your other middleware.                                              

And that's it. You're done.

  1. Many thanks to the people over at for the middleware. 


Lottery sambadSept. 17, 2019, 2:20 a.m.
Winning a Nagaland State Lottery price in Lottery Sambad is not only based on luck. You also need to learn the correct strategy on how to win the Lottery Result Today . You see, there are certain techniques that you need to master for you to hit the Nagaland lottery sambad price!
The truth of the matter is - there is probably no secret or trick in playing Nagaland lottery . In fact, people who have won the Nagaland state lottery result for more than once shared that there are certain strategy that you can do to increase the chance of winning.
You can watch that video where Richard breaks down his secret formula. He will cover his steps to the system (Opens in a new window):

So, what is your chance of winning the lottery?

For you to know your chances of winning, you need to know first how Nagaland lottery result works. You need first to buy a Nagaland state lottery sambad ticket with your chosen set of numbers. You need to select the numbers based on the certain range of numbers from the Nagaland state lottery today you are joining to. The numbers will be randomly selected from this range.
If your numbers match with the winning number – then you won the lottery sambad night prize! You may also win a smaller prize if you match a certain number with the dear Lottery Sambad numbers.
You also have to note that your prize will also depend on how many tickets have a similar winning number. If two of you won the same number, you will have to divide the Rajshree lottery sambad price into half.

Nine Tips on How to Win the Lottery

If you want to boost your chance of winning the Lottery Sambad morning, here are the nine helpful tips to increase your winnings.
  1. To increase your probability of winning, you need to buy more tickets. But the downside of this is that you may need to spend lots of money to win a prize. The worth of your winnings may not be fully compensated because of the high investment you put in buying tickets. This was proven by an Australian firm who try this trick in a local Lottery Sambad result. But then again, buying more Lottery Sambad today result ticket may help improve your chances of winning.
  2. Form a lottery syndicate where you gather money from lottery players. This means you get more tickets and Lottery Sambad old numbers which means you will all have a better chance of hitting the West bengal state lottery . The downside is, you may have to share the lottery price with many people. But maybe, you will not complain if your group won $500 million dollars and you have to divide it between 10 winners! If you don’t want to spend a fortune, playing a Khanapara teer result syndicate will increase your odds of winning Khanapara teer .
  3. Don’t choose consecutive numbers. For example, if you play a Khanapara teer counter with five winning numbers and you have to choose until numbers 55. The total numbers must be between 104 and 176. Studies have shown that 70% of the Khanapara teer result today have sums that fall in this range.
  4. Don’t choose a number that falls in the same number group or ending with a similar digit. Although, there is a possibility that you may win, but the probability is very low.
  5. Look for more unpopular games played at odd times. Don’t go for Khanapara teer result live games that always have a winner try to explore other unpopular games. Most likely, people will all go to that Khanapara teer common number game, so your chance of winning is lesser. Go to the less popular, with lesser teer result today game player, so your odd of winning is higher.
  6. Better if you will play less popular lottery games with fewer players, so you will have less competition. You may try less popular teer result such as Suprenalottery, Euro shillong teer, and Super shillong teer plus. The teer result price may seem small but the odds of winning are higher.
  7. Some people tend to play lottery based from the numbers of their birthday, or the birthday of a family member. If this happens, you will usually get more numbers from 1 to 31, as this corresponds to the days in the calendar method. If you choose numbers above 31, this may not increase your chance of winning shillong teer result but it can boost your probability of not sharing the shillong teer result prize with anyone.
  8. Keep in mind that every number in the lottery has an equal probability of being chosen as the winning number. There is no software that could predict the exact winning number. But you can increase your chance of not splitting the shillong teer facebook price if you use the not so common number.
  9. Play the right games. There are many kinds of shillong teer facebook making number games that you could play. If you join the national shillong teer common number draw, there is a much broader number pool as compared with a local or state shillong teer number. In a local shillong teer previous result, you have to be physically present during the shillong teer counter draw, but the odds of winning is higher compared with a nationwide shillong night teer result.

Seven Time Lottery Grand Prize Winner Shares His Secret

Did you know that Richard Lustig won seven shillong teer result today grand prizes including a:
  1. $10,000 from Scratch-off ticket
  2. $13,696.03 in Florida Fantasy 5
  3. Holiday to Los Angeles, amounting to $3,594.66 from scratch-off ticket 2nd chance draw
  4. Holiday to Memphis, around $4,966 from scratch-off ticket 2nd chance draw
  5. $842,152.91 from Florida Mega Money
  6. $73,658.06 in Florida Fantasy 5
  7. $98,992.92 from Florida Fantasy 5
django_devilNov. 3, 2017, 12:48 p.m.

please update the file. It's not working...

silviogutierrezNov. 4, 2017, 5:43 p.m.

This was written for Django 1.2. I believe Django changed its middleware structure around Django 1.10, so I'm not sure when this stopped working. The concepts might still apply though.

Ben KaleskiMarch 20, 2019, 12:03 p.m.

Thanks for sharing these codes in your site and I will always get helped from your site. This website is also share in best papers writing service social media page because there mostly students are stuck in different codes.

dsdsdMay 6, 2019, 5:57 a.m.

Laisser les choses évoluer dans le bon sens. panerai montres La contingence exige du vendeur qu’il ait un esprit flexible et qu’il fasse les choses comme il se doit, mais pas trop, afin que les gens puissent être convaincus, laissant ainsi une bonne impression au client, transformant les inconvénients en avantages. Le boîtier de la patek philippe montres est en titane. La conception en matériau léger rend la montre plus dynamique. Le diamètre de 47 mm est idéal pour les hommes aux poignets épais, et le matériau léger ne fait pas le poignet. La table est trop lourde.

James HiltonJune 11, 2019, 9:58 a.m.

Çok destekleyici içerik, hakkında konuştuğunuz aynı problemle karşı karşıya kaldım Hoşgeldin Bonusu Veren Bahis Siteleri talimatlarınıza göre hareket ettikten sonra şimdi bu sorunun üstesinden geliyorum.

LinaJune 13, 2019, 5:44 a.m.

I am totally mad about racing but once i tried to race and i got injured. But i love to watch racing. Now i am fighting against whatsapp sniffer and making a group.

angilaJune 27, 2019, 12:26 p.m.

The music is the key to relax but not anytime when you got bad vibes everything seems bad to you-you don't want to get interact with anything because it makes you uncomfortable. can someone do my homework for me Its natural phenomena when you are worried about something so you don't like anything at all. Because your mind doesn't want to accept anything.

Sumitsingh72July 23, 2019, 12:07 p.m.
muneerAug. 5, 2019, 11:04 a.m.

Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. There tend to be not many people who can certainly write not so simple posts that artistically. Continue the nice writing Medical Emergency Help

mtomSept. 26, 2019, 2:23 p.m.

Indian Microwave Recipes at my site feel free to check it out. 먹튀검증

MityaAug. 22, 2019, 9:36 a.m.

I have been working on wen designing and have designed may websites. Now i am working on a site on topic kannada whatsapp status video. It will be completed soon and will be available for my users soon.

mtomSept. 3, 2019, 3:31 p.m.

hi, rv solar power panels is the world-class factor upon planet. I’ve merely bought a utilized solar panel packages, if You have forex You must do exactly the same! Goodbye! Barber Shop Birmingham

shawn josephSept. 13, 2019, 2:21 p.m.

This information is quite interesting and give much benefits to everyone. However, most of the student may face lot of difficulties while doing their essay, project or dissertation so we are here to help you and understand you and give you the best Academic Writing Help with cheapest cost and rate.

molly jonesSept. 25, 2019, 11:49 a.m.

We are driven by convenience - we understand this must be the place splendid organization will begin from. Our point is to meet and outperform the wants for our guests through phenomenal organizations and proportions of neighborliness, tidiness of our workplaces, and sensibility of our property as well. If you are scanning for hotels in hamra beirut, by then we can ensure the best benefits in the town. Our cabin is in like manner a supportive scene for guests who mean to stay for long. It is in like manner enveloped by the most remarkable schools, crisis facilities, and government associations.

Toby PlastererOct. 3, 2019, 12:42 p.m.

Hey there, it turned out to be a really fabulous adventure for me personally when I visited your web site. I hope you don't mind if I commend you on the superb quality of the work and additionally to send you the best of luck with everything as you move forward over the coming months. It was certainly a satisfying adventure to explore your blog and I'll most definitely be stopping by once again to ascertain precisely how you are doing. Thanks a bunch and with a bit of luck I shall see you here once more in a little while - Toby Plasterer

kevinOct. 7, 2019, 7:58 p.m.

I really think that you can start you writing career on They provide an opportunity to beginners to post their work on the site. And it is actually a great thing to do.

IDN PokerOct. 11, 2019, 4:04 p.m.

Kronospoker Situs Judi Online, IDN Poker, IDN Poker Mobile, Agen Poker Terpercaya Indonesia untuk permainan Poker Online, Domino Qiu Qiu, Ceme Keliling, Capsa Susun dan Super10


Post New Comment