We love designing and developing websites, but what really drives us is solving problems and cultivating strong relationships with our clients
Using subdomains with Django
By : shabda
Django makes using beautiful urls a snap. However using subdomains with Django is not so obvious. Here is an article explaining this.
Advantages of subdomain
- Sometimes looks more professional. Users will prefer someblog.wordpress.com over wordpress.com/someblog.
- If each part of your site is Under a different untrusted users, you might want to give them a separate subdomain, so that if they do Blackhat Seo on their part, the main site is not harmed.
- Your users can point a Cname to their subdomain to use domain of their choice with their subdomain.
Disadvantages of subdomain
- When you need subdomains, you will know it. If you do not if you need it or not, you probably do not.
Getting the current subdomain is ridiculously easy.
bits = urlparse.urlsplit(request.META['HTTP_HOST'])[0].split('.')
bits[0]
now bits[0] is you subdomain.
However if you are using subdomains you are probably going to be needing this,
- In all your views.
- In all your templates.
So you need to expose the subdomains using
- A Middleware for all requests.
- A request context for all templates.
A Middleware is nothing but a normal Python class which can implement process_request, process_response and others.
The code to expose subdomains for all requests via a middleware is,
import urlparse
class GetSubdomainMiddleware:
def process_request(self, request):
bits = urlparse.urlsplit(request.META['HTTP_HOST'])[0].split('.')
if not( len(bits) == 3):
pass#Todo Raise an exception etc
request.subdomain = bits[0]
The way to populate subdomain in all templates is similar
def populate_board(request):
"Populate the board in the template"
return {'board':request.subdomain}#request.subdomain has been populated via the Middleware.
And now you need to edit you settings.py file to add TEMPLATE_CONTEXT_PROCESSORS and MIDDLEWARE_CLASSES to include your Middleware and context processor.
You are almost ready to go, however your cookies will not work across sub domains. To make your cookies work across subdomains, add this line to your settings.py
SESSION_COOKIE_DOMAIN = '.example.com' if not DEBUG else '.local
Comments
- How to use pep8.py to write better Django code
- Screencast: Django Tutorial Part 1
- How and why to use pyflakes to write better Python
- Getting started with South for Django DB migrations
- A brief overview of Vagrant
- Writing jQuery plugins using Coffeescript
- Behind the Scenes: Request to Response
- Using SQLite Database with Android
- Haml for Django developers
- Coffeescript for Python programmers
- rails
- django
- linkroundup
- django opinion
- opinion
- business
- API
- appengine
- python
- satire
- startup
- Uncategorized
- marketing
- personal
- rambling
- search
- interviews
- seo-interviews
- 5startupideas
- ideas
- seo
- tips
- forms
- paypal
- utilities
- datetime
- web2.0
- Amazon
- algorithms
- presentations
- products
- pinax
- satchmo
- ecommerce
- microsoft
- yahoo
- book
- tutorial
- models
- aggreagtion
- meta
- India
- apps
- about
- CSS
- Design
- wordpress
- test slug
- vim
- urls
- reviews
- javascript
- xmpp
- emacs
- Typography
- Grid Theory
- Color Theory
- iphone
- android
- titanium
- mobile applications
- CSS3
- Browser Compatibility
- mobile
- jobs
- lamson
- django setup
- files
- upload
- jsTree
- hierarchical view
- web page
- Treeview
- coffeescript
- request
- response
- South
- django south
- django migration
- --fake
- screencasts
- February 2012
- January 2012
- December 2011
- October 2011
- September 2011
- July 2011
- June 2011
- April 2011
- February 2011
- January 2011
- December 2010
- November 2010
- October 2010
- September 2010
- June 2010
- April 2010
- March 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- April 2009
- March 2009
- February 2009
- November 2008
- October 2008
- June 2008
- May 2008
- April 2008
Very useful, thank you!