Snippet List
On our site [Fornebuklinikken - A cosmetic surgeon in Norway](http://www.fornebuklinikken.no) we also have a domain [http://fornebuklinikken.com](http://www.fornebuklinikken.no) which should be using the 'en' language.
We didn't wan't to use the standard locale lib, and wrote our own middleware which lookups the correct language corresponding to the domain (.no or .com)
Any questions? Contact me on herman.schistad (at) gmail.com
- internationalization
- middleware
- multilingual
- locale
- domain
- localeurl
Adds http://hostname or https://hostname before every URL generated by a Django url function.
**Example:**
Normally, something like YourModel().get_absolute_url() would return `/2009/09/02/slug`. However, this is not an absolute URL, because it does not include an HTTP schema or host.
With this middleware, YourModel().get_absolute_url() will return `http://yourdomain.com/2009/09/02/slug`.
This will also work for calls to reverse() or the {% url %} template tag.
**Installation:**
Drop this code into yourproject/middleware/scriptprefix.py.
**Usage:**
In your settings.py, add:
MIDDLEWARE_CLASSES = (
# ...
'yourproject.middleware.scriptprefix.ScriptPrefixMiddleware',
# ...
)
- url
- domain
- get_absolute_url
- host
- set_script_prefix
Permit to redirect desired domain name to the 'domain' of Site app.
Useful if you have different domains name for the same website.
#1. Add to your settings DOMAINS_ALIAS like this:
DOMAINS_ALIAS = (
'my-second-domain.com',
'www.my-second-domain.com',
'third-domain.com',
'www.third-domain.com',
)
notice: all these domains are redirected to the **domain** db entry of Site ID.
#2. add all these domains to ServerAlias directive in your vhost apache configuration.
#3. enable the middleware by adding to your MIDDLEWARE_CLASSES:
MIDDLEWARE_CLASSES = (
...
'utils.middleware.domainsalias.DomainsAliasMiddleware',
...
)
- middleware
- redirect
- domain
From time to time I often have to work with a site that has two domain names. This can be an issue when dealing with mapping api keys so to solve this problem I whipped up a decorator function that allows a preferred domain to be enforced on a per view basis.
It wouldn't be too much to take this onto a middleware, but for my usage I just wanted a per view granularity and a decorator function suited my needs quite nicely.
- decorator
- domain
- preferred-domain
Sometimes you want to generate a **really** absolute URL, but the built-in url tag only generates a URL relative to the current domain. This context processor adds the extra information needed to the request context, so you can generate an absolute URL in a template like so:
`{{ protocol }}://{{ domain }}{% url someview %}`
This is similar to how the password reset email from contrib.auth generates the full URL in the email.
Save this somewhere as context_processors.py (or add to existing file if you have one), and add context_processors.site to your TEMPLATE_CONTEXT_PROCESSORS setting.
- contextprocessor
- domain
- site
To activate, store this file as `mysite/winauth.py` and use in settings.conf:
AUTHENTICATION_BACKENDS = ('mysite.winauth.DomainControllerAuthBackend',)
Needs [pywin32 extensions](http://sourceforge.net/projects/pywin32/) installed (and obviously only runs on Windows).
- authentication
- domain
- windows
- controller
- pdc
Sometimes you need to make redirects that involve domains, you can't define those on the site urls, this middleware lets you define multiple redirects on your site settings.
Note: *You also can use the web server to do this, but I have found this middleware a useful tool to quickly or temporarily define redirects*.
Depending on your needs you may also find useful [snippet 434](http://www.djangosnippets.org/snippets/434/).
- middleware
- url
- redirect
- domain
7 snippets posted so far.