Setting up local django environment for multiple domains - django

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

Related

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',

Django middleware: Isn't a middleware module error

I am using the middleware provided in https://gist.github.com/426829 to do cross site scripting.
However, when I add the middleware to MIDDLEWARE_CLASSES, I get the error:
ImproperlyConfigured: isn't a middleware module.
My MIDDLEWARE_CLASSES looks like this:
MIDDLEWARE_CLASSES = ('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'TempMiddleware',)
I have not changed any code in the gist. process_request and process_response methods are there. I am on Ubuntu running the latest versions of Python and Django.
What's TempMiddleware? The name of the module, or the name of the class? As you can see with the other entries, you need the fully-qualified Python path of the actual class. If TempMiddleware is the name of the module, you need TempMiddleware.MyMiddlewareClass (and you should really follow PEP8 naming conventions) - and if it's the name of the class, you need my_module.TempMiddleware.
Edit:
TempMiddleware is not importable. It's the name of the class, you must put the entire import path.
E.g:
'django.contrib.auth.middleware.AuthenticationMiddleware'
and not
'AuthenticationMiddleware'
So if your class is in app_name/middleware.py, it should be
app_name.middlaware.TempMiddleware
It just mean that in your settings file, the variable MIDDLEWARE_CLASSES contains a list of modules in which one of the listed module is not a middleware.
Possible causes:
you added a middleware that doesn't declare middleware methods: fix that by removing the last middleware you added
you added a correct middleware but forget to put a coma at the end of the name, so strings are concatenated and it make django thinks 2 middlewares are in fact one: fix that by adding the missing coma

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.

Django Per-site caching using memcached

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',
)