Minimal Nginx and Gunicorn configuration for Django projects

By : Akshar Raaj

We will see how to deploy a Django project with nginx and gunicorn.

It was easier for me to understand nginx and gunicorn on development machine and then move to a publicly accessible server. So, we will cover this post in a similar sequence:

On development machine

  • Use django's runserver to serve the site.
  • Stop runserver and start using gunicorn instead.
  • Add nginx to this configuration.

On server

  • Deploy the project on a publicly accessible server using same stack.


Use runserver

We will use a minimal project with two apps and few static files. You can view this project ...

more info..

Writing thread-safe django - get_or_create

By : Javed Khan

In this blog post, we'll discuss thread-safety, why it's important and how to write thread-safe django code, especially for bulk operations like management commands. We'll take a simple example - get or create.

Thread-safety:

Thread-safety means that our code can be run in multiple threads and behave as expected. The reason that code can be unsafe with regard to threads is because we'll be manipulating shared memory (e.g. database) from the threads and there's a chance of a race-condition which will produce unexpected results.

To avoid this, we have the option of using read-write locks ...

more info..
Topics : django python threads

Deploying django using docker

By : Javed Khan

In this blog post we'll take a look at Docker and discuss ways to deploy django using docker.

About Docker:

Docker is a tool that helps you manage lxc containers and images. You can create an image, launch container from the existing image, attach to a running container, and generally play around with the containers.

The most important benefit of using docker and containers in general is that you have a clean, hygeinic and portable runtime enivorment for your app. This means you don't have to worry about missing dependecies, packages and other pain points during subsequent deployments ...

more info..
Topics : django deploy docker

Common testing scenarios for Django app.

By : Akshar Raaj

People are often confused regarding what tests to write. Let's look into some scenarios for which tests can be written.

Setting up the project

We start a Django project inside a virtual environment. In this post we would be using django 1.4.

(dj)~/play/dj$ django-admin.py startproject testcases

Let's start an app named blog.

(dj)~/play/dj/testcases$ python manage.py startapp blog

We will have the following model in blog/models.py:

class BlogEntry(models.Model):
    title = models.CharField(max_length=100)
    text = models.TextField()
    is_published = models.BooleanField(default=True)
    user = models.ForeignKey(User)

We ...

more info..
Topics : django testing

Serving static files in Django

By : Akshar Raaj

Serving static files, especially during development, is frequently a pain point. In this post, we will discuss the various settings and directory structure and how they interact with each other. Let's start with development when you have DEBUG = True.

We will create a Django project from scratch so that we are fully aware about what file in what directory we are talking about. So, this blog post will be a little longer than it should be. We will try these things with Django 1.4, but these will work on Django 1.3 as well. I have not tested ...

more info..
Topics : django static

Two Scoops of Django: Review

By : Shabda Raaj

Django best practices

Two scoops of Django is the new book on Django best practices by Daniel Greenfeld and Audrey Roy. I just finished reading it and found it extremely useful.

The book is a collection of tips, their justification and code organized in logical areas. I have been using Django since 2008, but I still found a few tips which were new to me and many which served as good reminder. At about 200 pages, its comprehensive but not overwhelming. If you are an advanced Djangonaut, you can read it in a weekend (And you probably are using all its recommendations anyway ...

more info..

Easy client side form validations for Django: Django Parsley

By : Shabda Raaj

Parsleyjs is a JavaScript library to do client side data validations. It does this in a non-intrusive way via adding a data-* attributes to form fields. Check it out, it's really slick.

Django forms do a great job of server side data validations. By integrating Parsley with Django form, you get good client side data validations as well. Get the app here.

Some design considerations

When I started writing this app I wanted to make it really easy to use. My first thought was to make this a filter or a template tag which would have allowed you to ...

more info..
Topics : django

Not exactly, not exactly tim the enchanter

By : Javed Khan

I've been putting off writing about my experience with django form wizard for some time now. I came across this blog post by Kenneth Love which finally compelled me to write this down.

About Django Form Wizard:

Form Wizard can be handy when you have a huge form, and you want to make it less intimidating to the user by splitting it into multiple steps.

If you have glued multiple forms together with sessions or whatnot and it turned out to be a mess, you now what I'm talking about :)

Think surveys, checkouts, lengthy registrations etc.

The class ...

more info..
Topics : django forms wizard

The missing documentation for django.utils.datastructures

By : Thejaswi Puthraya

Note

django.utils.datastructures is intentionally not documented by the django core devs because it is an internal API and is liable to change without any notice. This file is not governed by django's lenient backwards-compatible policy. You have been sufficiently warned!

With the note out of the way, let's look at the interesting datastructures in this file. You may ask why we should learn about those when we shouldn't be using them? Reading code is the best way of learning and this file has some beautiful code.

MergeDict is the first of the datastructures in the ...

more info..
Topics : django internals

Dropbox file upload handler for django

By : Thejaswi Puthraya

Dropbox announced new pro plans last week and some accounts have had their storage size doubled. Wouldn't it be wonderful if we could upload all our files to dropbox from our django webapp?

In this post, I write a custom file upload handler that will upload files from our application to dropbox.

Let us see how to use the custom file upload handler.

Install the Dropbox Python SDK before you setup your django app to handle the file uploads.

In your settings.py, add the following attributes (with the values filled):

DROPBOX_APP_KEY = ""
DROPBOX_APP_SECRET_KEY = ""
DROPBOX_APP_ACCESS_TOKEN = ""
DROPBOX_APP_ACCESS_TOKEN_SECRET = ""

# Optional values below

# The ...

more info..

Deploying Django apps on Heroku

By : Shabda Raaj

Deploying Django apps on Heroku:

Read this first: http://devcenter.heroku.com/articles/django.

This is a great article by the Heroku. I am just filling in some more details and making this step-by-step.

  1. Get your Django project code.
  2. Create a virtualenv with a no-site-packages command ::

    virtualenv vent --no-site-packages

  3. Install Django, psycopg2 (postgres connector), gunicorn and any other required Django libraries.

  4. Confirm that you have all the required libraries and you can run your code locally using manage.py runserver.
  5. Create a requirement.txt by using ::

    pip freeze > requirements.txt
    
  6. Make sure you have a requirements.txt at the root ...

more info..
Topics : django

Deploy Django App in 5 Easy Steps

By : Saket Bhushan

So you just bought a new VPS, have installed Ubuntu and want to deploy your django app, GREAT!! We shall get your app, up and running in 5 easy steps, using best(arguably) of tools available. The post is targeted to audience who are new to deployment arena, but assumes you are comfortable with developing basic django apps. We shall be using gunicorn as our server and nginx nginx as our reverse proxy and static hanlder. Here we go:

1. Login and OS Updation:

$ ssh root@{your_ip}

# apt-get update

# apt-get upgrade

# apt-get dist-upgrade

# dpkg-reconfigure tzdata #choose your time zone

2 ...

more info..

Dynamically attaching SITE_ID to Django Caching

By : Anoop Thomas Mathew

It would be useful and convenient, if you have an automatic way to add the SITE_ID, especially, when you have multiple sites running on the same deployment. Django provides a cache prefix function KEY_FUNCTION in settings which can be used to achieve this.

Just follow the following steps, and your cache, automatically prepends SITE_ID to the cache key, making it unique across multiple sites.

  1. Put the following into the settings file.

         CACHES = {
    
         'default': {
    
          'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
    
           'LOCATION': 'cache_table',
    
           KEY_FUNCTION = projectname.appname.modulename.functionname,
    
                    }
    
             }
    
  2. Write a function to get current site id, say, get_current_site(), which returns current ...

more info..

Deploying Django apps on Heroku

By : Shabda Raaj

Read this first: http://devcenter.heroku.com/articles/django.

This is a great article by the Heroku. I am just filling in some more details and making this step-by-step.

  1. Get your Django project code.

  2. Create a virtualenv with a no-site-packages command

    virtualenv vent --no-site-packages
    
  3. Install Django, psycopg2 (postgres connector), gunicorn and any other required Django libraries.

  4. Confirm that you have all the required libraries and you can run your code locally using manage.py runserver.

  5. Create a requirement.txt by using
    pip freeze > requirements.txt
    
  6. Make sure you have a requirements.txt at the root of your repo. Heroku uses ...

more info..
Topics : django

Getting started with South for Django DB migrations

By : Akshar Raaj

South is a migration tool used with Django.There will be times when you would be adding fields to a model or changing the type of field (eg: Field was an IntegerField and you want to change it to FloatField). In such cases syncdb doesn't help and South comes to your rescue.

There were times when i tried "python manage.py migrate southapp", got some error and then tried "python manage.py migrate southapp 0001 --fake". In some cases, that worked. When it did not work, i tried something else and so on. There were confusions regarding what --fake ...

more info..
Topics : django South south

Behind the Scenes: Request to Response

By : Thejaswi Puthraya

In the previous installment of "Behind the Scenes", we saw how the control flows from Form to File Storage. Today, we are going to see how the application reacts from request to response.

In this post, we are going to assume that we are using django's inbuilt runserver. The flow doesn't change much for other WSGI servers available.

When you invoke the runserver management command, the command line options are validated and an instance of WSGIServer is created and passed the WSGIRequestHandler, which is used to create the request object (WSGIRequest). After the request object is created and ...

more info..

Haml for Django developers

By : Shabda Raaj

Haml is taking the Ruby(and Rails) world by storm. Its not used as heavily by Python(and Django) developers as the Python solutions aren't as mature. I finally gave Haml a try and was pleasantly surprised how easy it was.

The most mature Python implementation of Haml is hamlpy, which converts the hamlpy code to Django templates. Others are shpaml and GHRML

Lets look at some templates from Django tutorial

{% if latest_poll_list %}
    <ul>
    {% for poll in latest_poll_list %}
        <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>
    {% endfor %}
    </ul>
{% endif %}

Here is this template converted to Haml

-if ...

more info..
Topics : django

Behind the Scenes: From HTML Form to Storage

By : Thejaswi Puthraya

In this post, we are going to see what happens behind the scenes when a file is uploaded to a django powered web application.

File Upload Flow

An HTML form with a file input (atleast one) and encoding set to multipart/form-data is submitted. The MultiPartParser parses the POST request and returns a tuple of the POST and FILES data (request.POST, request.FILES). The MultiPartParser processes the uploaded data using the File Upload Handlers objects (through the new_file, receive_data_chunk and upload_complete methods). The request.FILES values are a sequence of instances of UploadedFile.

In the django form, we pass the request.FILES ...

more info..
Topics : django files upload
Javed
Comments
Reactions

Real time applications with Django, XMPP and StropheJS

By : Javed Khan

TL;DR:

Introduction:

PubSub is a XMPP extension which allows publishing and subscribing to events. This is useful when you instantly want to notify many clients about something interesting happening on your server.

Quoting the authors of PubSub specs:

The protocol enables XMPP entities to create nodes (topics) at a
pubsub service and publish information at those nodes; an event
notification (with or without payload) is then broadcasted to all ...

more info..
Javed
Comments
Reactions

Seven reasons why you should switch to Vim

By : Javed Khan

So you want a better IDE for developing django, huh? Why not give good old vim a try?

Use pathogen to maintain your vim plugins (and sanity). Using this, you can clone the repositories listed here to .vim/bundle/ and start using them right away.

Also, consider adding your .vimrc and .vim to a repository. Include .vimrc inside .vim and symlink .vim/.vimrc to ~/.vimrc to version control your .vimrc.

My vim files can be found here. Also, here's an imgur album demonstrating these plugins in action.

1. Syntax highlighting for django templates

Starting from vim 7.1, syntax ...

more info..
Topics : django vim
© Agiliq, 2009-2012