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

jamesOct. 23, 2019, 7:31 a.m.

weight loss is sometimes difficult to attain, it all depends on your motivation and genetics- 안전놀이터

liamNov. 2, 2019, 7:53 a.m.

Django is an interesting game, I used to play it all the time before. I even gave a review about it on And people liked my review.

Sophie MillerNov. 11, 2019, 4:06 p.m.

Je voudrais lire à ce sujet plus. Prompt, ce que la littérature pour étudier? number for live person

mtomNov. 9, 2019, 11:46 a.m.

Thanks for your writing. I would like to say a health insurance broker also works for the benefit of the actual coordinators of any group insurance policies. The health insurance broker is given a directory of benefits sought by an individual or a group coordinator. What any broker can is try to find individuals and also coordinators which best complement those requirements. Then he gifts his advice and if the two of you agree, the particular broker formulates a legal contract between the two parties. varicose veins cream reviews

frankNov. 13, 2019, 10:35 a.m.

Many thanks for the exciting blog posting! Simply put your blog post to my favorite blog list and will look forward for additional updates. Simply wanted to write down a word in order to say thanks to you for those wonderful tips. PSL teams 2020

mtomNov. 16, 2019, 1:01 p.m.

I have learn several excellent stuff here. Certainly price bookmarking for revisiting. I wonder how a lot attempt you place to make this sort of wonderful informative web site. Beard trimming midtown

seoagencyNov. 16, 2019, 3:51 p.m.

This particular is usually apparently essential and moreover outstanding truth along with for sure fair-minded and moreover admittedly useful My business is looking to find in advance designed for this specific useful stuffs… AP 10th Class Question Paper 2020

paulNov. 20, 2019, 7:20 p.m.

Someone might argue that it is better to golf-in-marrakech but have never been there so I can have an opinion. I will just wait for my turn to go there and then write about it.

jamesDec. 1, 2019, 12:54 p.m.

It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often.

mtomNov. 25, 2019, 2:30 p.m.

Amazing blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple adjustements would really make my blog jump out. Please let me know where you got your design. Cheers 사다리사이트

mtomNov. 27, 2019, 10:44 a.m.

Thanks for your kind data.. I realy appreciate it.. keep it up. Bubble Shooter

jamesNov. 27, 2019, 10:48 a.m.

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information LAYOUTindex

warismayNov. 27, 2019, 10:01 p.m.

That's really interesting and helpful. We are providing quality items sofa bed nz which you will be get online in Auckland in a single click.

mtomNov. 30, 2019, 11:57 a.m.

wonderful publish, very informative. I ponder why the other specialists of this sector do not notice this. You must proceed your writing. I am confident, you have a great readers’ base already! 안전놀이터

davidDec. 1, 2019, 12:55 p.m.

Many thanks for the exciting blog posting! Simply put your blog post to my favorite blog list and will look forward for additional updates. Simply wanted to write down a word in order to say thanks to you for those wonderful tips.

dris2Dec. 3, 2019, 3:55 a.m.

The road of love was only by himself so he had to step If love is only on the lips, I do not need you to say Hey dear, I still remember or have forgotten The places I vowed and the places I used to go We used to experience and taste the night together So how is the moon with the sun? How you, how you, how you krunker super smash flash 2

mtomDec. 5, 2019, 1:30 p.m.

Hello, I had been researching the web and I ran into your own blog. Keep up the wonderful work. Fitzrovia barbers

Thank you so much regarding giving me personally an update on this theme on your web page. Please realize that if a new post becomes available or if any modifications occur to the current publication, I would be considering reading a lot more and focusing on how to make good usage of those approaches you reveal. Thanks for your efforts and consideration of other individuals by making this site available. 안전공원

mtomDec. 10, 2019, 9:52 a.m.

After study a few of the weblog posts on your web site now, and I actually like your approach of blogging. I bookmarked it to my bookmark web site record and shall be checking back soon. Pls take a look at my web page as nicely and let me know what you think. 파워볼

mtomDec. 15, 2019, 12:16 p.m.

Normally I don’t read post on blogs, but I would like to say that this write-up very pressured me to check out and do so! Your writing taste has been surprised me. Thank you, quite nice article. Eyelash Extensions Las Vegas

JeromeDec. 16, 2019, 6:24 a.m.

Download and play geometry dash full game. It has lots of maps and obstacles.

Digital_ZoneDec. 16, 2019, 11:49 a.m.

Awesome dispatch! I am indeed getting apt to over this info, is truly neighborly my buddy. Likewise fantastic blog here among many of the costly info you acquire. Reserve up the beneficial process you are doing here. crypto fashion

mtomDec. 18, 2019, 6:50 a.m.

Thank you, I’ve just been searching for info about this topic for a while and yours is the greatest I’ve discovered so far. But, are you sure about the source? 파워볼게임

Dawud PlasterDec. 22, 2019, 11:18 p.m.

Greetings, it was a truly superb experience for me when I looked around your web blog. I hope you don't mind if I compliment you on the excellent quality of the work you've done here and additionally to send your team bon chance with everything as you develop in the coming months. It was obviously enjoyable to check out your website and I'll definitely be dropping by again before long to find exactly how you're doing. All the best and I will doubtless see you here once again in the future - Dawud Plaster

mtomDec. 29, 2019, 9:11 a.m.

Internal direct, training, contentment, honour, forbearance plus sentence. Usually a percentage of problems that Tang Soo Write, the actual Mandarin chinese martial art related to self-defense, can educate we additionally instilling in you the strength not just in defend your business women and men capability related with spotting hazard joins to refrain from giving confrontation completely. 토토가족방

Very interesting details you have noted , thankyou for posting .This will be huge for smugglers sending these phones to countries that are not directly supported by Apple currently. Apple will move substantial units this way. This article only takes the American perspective, which is ridiculous. best cbd oil for pain

mtomDec. 30, 2019, 2:35 p.m.

Way cool, some valid points! I appreciate you making this article available, the rest of the site is also high quality. Have a fun. 먹튀검증

lisaJan. 2, 2020, 4:53 p.m.

This information is quite interesting and gives many benefits to everyone. I am indeed getting apt to over this info, is truly neighborly my buddy. Likewise fantastic blog here among many of the costly info you acquire. Visit This site for more information.

mtomJan. 4, 2020, 9:52 a.m.

Can I merely say that of a relief to discover somebody who truly knows what theyre preaching about online. You actually know how to bring an issue to light and earn it crucial. More people must check this out and understand this side with the story. I cant think youre no more common since you also undoubtedly have the gift. Haircut near me

mtomJan. 8, 2020, 8:36 a.m.

Aw, i thought this was a very good post. In thought I must set up writing like that additionally – taking time and actual effort to generate a excellent article… but so what can I say… I procrastinate alot and also by no means often get something carried out. Buy Linkedin Post Shares

Thank you for the sensible critique. Me & my neighbor were just preparing to do a little research on this. We got a grab a book from our local library but I think I learned more from this post. I am very glad to see such fantastic info being shared freely out there. 토토사이트

WilliamJan. 9, 2020, 7:13 a.m.

It is wonderful to be here with everyone, I have a lot of knowledge from what you share, to say thank you, the information and knowledge here helps me a lot about Custom Retail Boxes

AvirajJan. 9, 2020, 6:09 p.m.

Hey Guys We Provide Latest Collaction Of Whatsapp Status Video So Don't West Time And Visit Now.

If You Find Best Comments So We Give Best Comments On Girl Pic

mtomJan. 11, 2020, 10:41 a.m.

Hi there, just became alert to your blog through Google, and found that it is truly informative. I’m gonna watch out for brussels. I’ll be grateful if you continue this in future. A lot of people will be benefited from your writing. Cheers! led moving head wash

lisaJan. 13, 2020, 1:36 p.m.

The stuff that you mention in the above is acting as the best guide for any visitors. It really admired meiceland travel

mtomJan. 14, 2020, 10:45 a.m.

Hi there! I just wish to give an enormous thumbs up for the nice info you’ve right here on this post. I shall be coming again to your blog for extra soon. joker123

This posting is incredibly nicely written, and it in addition consists of numerous beneficial information. I appreciated you are specialist manner of creating this blog post. Thanks, you have created it simple and easy for me to comprehend. payday loan consolidation companies

SportszillaJan. 18, 2020, 7:29 p.m.

I thought this was an excellent post. In figured I should set up composing like that moreover – requiring some serious energy and genuine exertion to produce a fantastic article… however so what would i be able to state… I delay alot and furthermore in no way, shape or form regularly get something completed. Psl 2020 schedule pdf

mtomJan. 20, 2020, 9:14 a.m.

You created some decent points there. I looked on the internet for any issue and found most people should go as well as with the internet site. Barbers Tottenham court road

You seem to be very professional in the way you write.~~’ payday loans consolidation


Post New Comment