background preloader

Python

Facebook Twitter

Writing Django Unit Tests for Views. Django powered AJAX Chat – Part 1 « The Python Haven. In the neverending adventure of developing our game website we have stumbled upon the “live chat” rock. I’ve seen some interesting things here using a pure django implementation for chat and there using django and comet for chat. I browsed the first one and I didn’t like it too much, and the comet implementation is clearly out of the picture for now. We have to finish this project in 4 to 6 weeks and we cannot take the risk of messing around with yet another technology (although I will keep this django+comet link bookmarked for future use). So I decided to make one quick and dirty chat app myself. I had a very clear idea of how I wanted to implement the chat, it is really simple after all: Client loads HTML + JavascriptClient queries the Server every X seconds for new messagesServer responds messages serialized in JSON formatClient attaches new messages to the message box To save us from that, diverse solutions exists.

The philosophy that I have for this application is: Simple huh? Writing your first Django app, part 1. Let’s learn by example. Throughout this tutorial, we’ll walk you through the creation of a basic poll application. It’ll consist of two parts: A public site that lets people view polls and vote in them.An admin site that lets you add, change and delete polls.

We’ll assume you have Django installed already. You can tell Django is installed by running the Python interactive interpreter and typing import django. If that command runs successfully, with no errors, Django is installed. Where to get help: If you’re having trouble going through this tutorial, please post a message to django-users or drop by #django on irc.freenode.net to chat with other Django users who might be able to help. Creating a project If this is your first time using Django, you’ll have to take care of some initial setup. From the command line, cd into a directory where you’d like to store your code, then run the following command: django-admin.py startproject mysite Script name may differ in distribution packages Note. Getting Started with virtualenv (Isolated Python Environments) | Mitch Fournier. Like South, virtualenv is a helper utility that I put off using for too long.

Looking back, it is so easy to get up and running (just like South, see below) that there is no reason for you to hold off like I did. In a nutshell, virtualenv is a tool for creating isolated Python environments. This is particularly useful if you host multiple Django projects on a single dev box. As an example, virtualenv allows you to easily work on one site built on Django 1.1 and django-registration 0.7 and another one built on Django 1.2 with django-registration 0.8. It is also invaluable if you want to deploy a Django project to a shared host where you don’t have root access to the main “site-packages” directory. Once you create a virtualenv for your project, an isolated copy of Python and “site-packages” is created which you own and can write to. Basic virtualenv Start-up Steps A helper alias: Now you can run “ams” to quickly activate your “mysite.com” environment. PyPy :: PyPy.