Django Per-site caching using memcached - django

So I'm using per-site caching on a project and I've observed the following, which is kind of confusing. When I load a flat page in my browser then change it through admin and then do a refresh (within the cache timeout) there is no change in the page--as expected. However when I stat a new session in a different browser and load the page (still within the timeout) the app is hit instead of the cache, with the
Isn't the cache key generated from the URL? it seems that the session state is getting in there somewhere, which is causing a cache miss.
MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.gzip.GZipMiddleware',
'django.middleware.http.ConditionalGetMiddleware',
'django.middleware.doc.XViewMiddleware',
'ittybitty.middleware.IttyBittyURLMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
'maintenancemode.middleware.MaintenanceModeMiddleware',
'djangodblog.middleware.DBLogMiddleware',
'SSL.middleware.SSLRedirect', #SSL middleware to handle SSL
'django.middleware.cache.FetchFromCacheMiddleware',
)

Related

Setting up local django environment for multiple domains

I'm trying to set up a Django application that will accept multiple subdomain URLs. I'd like to test this locally. Since I can't nail this first step of passing in a url that has a subdomain, I can't get to the second part (figuring out if the URLconf I've set up for django-subdomains is working). The middleware checks for a subdomain, and chooses a URLconf file based on that subdomain.
I've set the following configs in /etc/hosts. When I've got the server running, and I hit these URLs, I go to the real test.com.
127.0.0.1 payments.test.com
127.0.0.1 rampup.test.com
(in case it matters) django-subdomains middleware settings from settings.py:
SUBDOMAIN_URLCONFS = {
'payments': 'main.urls',
'rampup': 'rampup.urls'
}
MIDDLEWARE_CLASSES = (
'subdomains.middleware.SubdomainURLRoutingMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
# Uncomment the next line for simple clickjacking protection:
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
Try this:
0.0.0.0 payments.test.com
0.0.0.0 rampup.test.com

More Django projects are overwritting memcached

on my server I have one project in Django with combination Nginx + Gunicorn. I use memcached in this project and it works fine to me.
Now I would like to add next project. Everything is fine since I add caching to my new project. Then this two projects are overwritting cache of the other one. When I go to page of one project, I see the second.
In my settings.py I have:
MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
...
and
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backend.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
What have I wrong?
Change the value of CACHE_MIDDLEWARE_KEY_PREFIX in your settings. It should be unique for each project.

Ordering middleware classes for Django-nonrel on GAE

Can anyone clarify how to order below middleware classes?
'mediagenerator.middleware.MediaMiddleware',
'autoload.middleware.AutoloadMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
.
.
.
'django.middleware.cache.FetchFromCacheMiddleware',
'google.appengine.ext.appstats.recording.AppStatsDjangoMiddleware',
In documents, every middleware in the top group is told to come first,
and both middleware in the bottom group is told to come last.
This is confusing.
Below is the whole list of middleware that I'm using:
'mediagenerator.middleware.MediaMiddleware',
'autoload.middleware.AutoloadMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.csrf.middleware.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'google.appengine.ext.appstats.recording.AppStatsDjangoMiddleware',
Any help and explanation will be appreciated.
Please check if everything is working:
'autoload.middleware.AutoloadMiddleware', # This is loading other modules, so it goes first because we want everything set before processing.
'mediagenerator.middleware.MediaMiddleware', # Serves/caches static files with urls starting with DEV_MEDIA_URL
'django.middleware.cache.UpdateCacheMiddleware', # Must be before other middleware that changes the header, so it seems that the right place for this is here
'google.appengine.ext.appstats.recording.AppStatsDjangoMiddleware', # Must be 'first' too. It collects stats of all middlewares below this. If you want stats from the middlewares above move it to the top
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.csrf.middleware.CsrfViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',

How to prevent HTTP 304 in Django test server

I have a couple of projects in Django and alternate between one and another every now and then. All of them have a /media/ path, which is served by django.views.static.serve, and they all have a /media/css/base.css file.
The problem is, whenever I run one project, the requests to base.css return an HTTP 304 (not modified), probably because the timestamp hasn't changed. But when I run the other project, the same 304 is returned, making the browser use the file cached by the previous project (and therefore, using the wrong stylesheet).
Just for the record, here are the middleware classes:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.transaction.TransactionMiddleware',
)
I always use the default address http://localhost:8000.
Is there another solution (other than using different ports - 8001, 8002, etc.)?
You can roll your own middleware for that:
class NoIfModifiedSinceMiddleware(object):
def process_request(self, request):
request.META.pop('HTTP_IF_MODIFIED_SINCE', None)
Basically, it just removes HTTP_IF_MODIFIED_SINCE header from the request.
Afterthought: Or you can monkeypatch django.views.static.serve and replace was_modified_since function by the one, that always returns True.

django internationalization doesn't work

I have created translation strings in the template and in the application view.
Then I ran:
django-admin.py makemessages -l it
and the file it/LC_MESSAGES/django.po has been created
I have now translated strings in the django.po file, and then I ran:
django-admin.py compilemessages
And I receive:
processing file django.po in /home/jobber/Desktop/library/books/locale/it/LC_MESSAGES
My settings.py looks like this:
LANGUAGE_CODE = 'it'
TEMPLATE_CONTEXT_PROCESSORS = ( "django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media", )
USE_I18N = True
MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
)
but I still always see English text. Why?
Maybe your browser language is English. The LocaleMiddleware tries to detect the language based on this algorithm (i.e. Accept-Language HTTP header).
So you can either remove the LocaleMiddleware to avoid this or use the set_language redirect view.
In addition to the accepted answer: If you're using Firefox you can install Quick Locale Switcher to test your application with different languages (based on the browser language). It adds a button at the bottom of your screen where you can easily change your browser language.