Django
Django RSS feed • Hire me for Django web development-
Django 1.5, Python 3.3, and Virtual Environments
Today I wanted to tinker around with the experimental support for Python 3 in Django 1.5 (alpha). So, my first question was, how well does
virtualenvplay with Python 3? And that's when I learned about the new(ish)venvmodule available in Python 3.3. -
Django Contact Form with reCAPTCHA
The quix.django.contact app is a very basic contact form for Django 1.4. It simply allows users to send a message to email addresses specified in
settings.py.This is how the
quix.django.contactform can be extended with django-recaptcha to add a reCAPTCHA field to the form. -
Notes on Serving Django Apps with uWSGI
I've been playing around with deploying/migrating some Django projects to a Rackspace Cloud Server with Nginx and uWSGI. These are my notes on getting started with uWSGI.
It should be noted that I am a developer and not a sys admin. Moreover, I am still learning and experimenting with uWSGI. This setup is being tested and tweaked on a few small projects that only handle a few thousand visitors per day. Rapid scalability and heavy loads are not a concern for these projects.
-
Getting an IP Address in Django behind an Nginx Proxy
When running Django behind Nginx as a reverse proxy, the
request.META['REMOTE_ADDR']may store the proxy IP address (eg.127.0.0.1) rather than the client's IP address. To get the client's IP address in Django, you can set theX-Forwarded-ForHTTP header in your nginx proxy configuration:proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;And then access it in Django with
request.META['HTTP_X_FORWARDED_FOR']:ip = request.META['HTTP_X_FORWARDED_FOR']You would use
request.META['HTTP_X_FORWARDED_FOR']only when running Django behind a reverse proxy where you know that your server is setting the value and it is not being sent in the request. Therefore, getting the IP fromrequest.META['HTTP_X_FORWARDED_FOR']vsrequest.META['REMOTE_ADDR']must be determined at the project-level and not within a reusable app.So, if you don't want your reusable apps using conditionals to determine where to get the IP address, what do you do? Well, I use a variation of the infamous
SetRemoteAddrFromForwardedFormiddleware. Once upon a time, Django shipped with this middleware to set the value ofrequest.META['REMOTE_ADDR']torequest.META['HTTP_X_FORWARDED_FOR']if it exists. It was removed in Django 1.2 as it was deemed too easy to be used incorrectly.I create
XForwardedForMiddlewareand put it intomiddleware.pyat the project-level.class XForwardedForMiddleware(): def process_request(self, request): if 'HTTP_X_FORWARDED_FOR' in request.META: request.META['REMOTE_ADDR'] = request.META['HTTP_X_FORWARDED_FOR'] return NoneWith this middleware installed, reusable apps can simply use
request.META['REMOTE_ADDR']and be blissfully unaware of the fact that it's behind a proxy. -
SSL behind an Nginx Reverse Proxy in Django 1.4
We recently upgraded one of our e-commerce websites to Django 1.4 and were pleased to find the new
SECURE_PROXY_SSL_HEADERsetting.Our server is using nginx to proxy requests to Apache which serves the Django project. Therefore, Apache/Django is not aware of when incoming requests are on
https. TheHttpRequest.is_secure()will always returnFalse. That means any decorators or middleware which redirect a view to a secure URL will result in an infinite redirect loop.The solution is:
-
Have the proxy server (nginx in our case) set the
X-Forwarded-ProtocolHTTP header. In the nginx config:proxy_set_header X-Forwarded-Protocol $scheme; -
Tell Django to use the
X-Forwarded-ProtocolHTTP header to determine if the request is secure. We defineSECURE_PROXY_SSL_HEADERinsettings.py:SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
The way that works is nginx will set
X-Forwarded-Protocolto "http" on normal connections and "https" on secure connections. Django'sHttpRequest.is_secure()method will returnTruewhen it's set to "https".Make sure you read the security warning for
SECURE_PROXY_SSL_HEADERif you're goingn to use it. -
