Latest Entries »

I used Django 1.3.1, Python 2.7.1, virtualenv 1.6.4 and virtualenvwrapper 2.10.1 for the scenario described in this post.

After I’d deployed my first Django site that used virtualenv, I attempted to run sudo ./manage.py shell on the server to inspect some data on my site, but I got an error that django.core.management couldn’t be found. I figured I hadn’t set some path properly when I saw the error, but I was a little puzzled as to which, since my site worked perfectly in the browser, but failed to run Django management commands. I opened my project’s manage.py, and noticed the interpreter directive was just pointing at the system’s python. Since I was using virtualenv, I simply changed the Python in my manage.py to point at the one in my virtualenv and management commands worked awesomely again. See below:

Replace the first line with in manage.py with:#!/usr/bin/env your-virtualenv-dir/your-virtual-env/bin/python

Update: 10/27/2011

Why Did I Replace the Default Python on Mac OS X?

Mainly, because tests were failing in my Django web application on my Mac that were passing on my Ubuntu setup. So I set out to create an up-to-date Python development environment on my Mac that works the same as the one on my Ubuntu box. Having successfully put Python 2.6.6 from python.org on my Mac, and fixed my web app, I’m sharing my results, setup, and solution with community.

Problem: I want to see how my Django website looks
in a Parallels VM with Internet Explorer 7, 8, or some other Windows
version of a browser, but using an already-running Django
development server running on my Mac to do so.

Solution:

Shutdown your current Django development server.

At the terminal type: ifconfig

In the terminal output look for “en1:” > “inet”,
and copy the IP address next to inet.

Open your settings.py or settings_local.py, and assign
the IP you copied to SITE_URL.

“Note that the default IP address, 127.0.0.1, is not accessible from
other machines on your network. To make your development server
viewable to other machines on the network, use its own IP address
(e.g. 192.168.2.1) or 0.0.0.0.”

Note: I couldn’t get the 0.0.0.0 version of this to work, but
any tips, suggestions, solutions, or otherwise are greatly appreciated.

Purpose: speeding up development with nose tests

This document describes how to setup your local development environment to run unit and/or integration tests in your Django project using nose, nose-exclude, and django-nose.

Why not just use Django’s test runner?

Recently, I worked on a heavily tested Django project. Running all tests in the project took several minutes, and the dependencies that were marked external in our Subversion project repository ran all of their tests too–each time project tests were run! I work on a team where testing frequently and committing often are tantamount to productivity. Nose tests can improve both testing speed, and subsequently accelerate development. As we walk through this tutorial you’ll begin to see how nose and its plugins (nose-exclude, django-nose) improve the productivity on any size Django project.

And so much more. Once you’ve completed this tutorial, open a terminal and type: nosetests –help.

What you’ll need to set up nose for Django

Although information in this tutorial can be used in almost any development environment, the walk-through for setting up nose and the related plug-ins was created and tested (of course) using the following:

Don’t use TransactionTestCase; nose wraps every test in a transaction (batteries included)

Don’t use the word “test”, or any variation thereof, in any method or class name; however, you can “exclude” certain non-tests from nose in the setup.cfg file discussed later in this tutorial.
Thanks to Jon Sackett for his comment on using the decorator @nottest on methods that have “test” in the name.

Open a terminal, and pip uninstall or remove any previous versions of django-nose, nose, nose-exclude.Note: This step may require finding and deleting older versions of pip installed on Ubuntu; ie. pip-0.3.1 (type pip –version to see which you have)

Next, install pip-0.6.3 if you haven’t already. Download the source tarball (pip-0.6.3.tar.gz), extract it, and cd into the extraction directory.

IMPORTANT: cd out of the current project you’re working in (some projects will have ‘nose-exclude’ as part of the build directory); I cd into my home directory, and then type: pip install nose-exclude.

Now go back to your project directory, and create a new file named nose_exclude.txt. See Using File-Based Exclusion List for what should go in this file. My recommendation is to create a file named nose_exclude.txt.tmpl with some common settings to keep track of the application dependencies or directories that you don’t want or need to test.

Likewise, create a new file in you project’s root directory named setup.cfg. This file contains the options or flags that you want to pass to “python manage.py test” every time you run tests. Optionally create one or more template files with predefined settings much like the nose_exclude.txt.tmpl file recommended above. This will help with different testing scenarios. For example, you probably don’t want to run coverage every time you execute all of your tests, but you might create a template that caters specifically to coverage or any other options not normally executed in your test runs.

See django-nose for changes you’ll need to make to settings.py in order to run tests using nose. Depending on your development environment, you may prefer to place these edits in a different file that gets pulled into settings.py.

Finally, cd into your Django project, and type python manage.py test as you normally would. Now you have nose!

Command differences between django-nose and the Django test runner

Classes that inherit from Django’s TestCase class and their test methods require special syntax. See below for examples.

Rejoice in Unicode for Technical and Non-Technical People

If you’re not familiar with Unicode (what is unicode?),
it’s really no big deal (unless you’re a developer: why you need to know
unicode. Thank you Jeff Elmore for pointing me to this link.).
This article would addresses how to take advantage of
the symbols and shapes you can extract using Unicode. Don’t worry: few developers
have an in-depth understanding of Unicode and/or its significance, but that’s
not what this article is about so you can read on blissfully ignorant or
well-aware an not be confused or bored.

Finding Graphical Purpose Through Simplicity

In web development I frequently use arrows and other symbols to draw
attention to a particular feature on a page. Simplicity is key, because the
actual content is what’s important right? These symbols/shapes can be widely
found in most graphics programs (free and paid) with a little digging or
familiarity. However, sometimes you want a shape or symbol that stands apart
or neatly fits the bill for exactly the graphic you want display. Enter Unicode.

Unicode in Graphics as an Alternative

There are an abundance of articles dealing with how to display symbols and
shapes in an HTML web page using Unicode. Few of these suggest using graphics.
In fact, you may discover that some articles advise against using graphics
in place of text, but most of these are referring to human-readable words, not
symbols/shapes. While you could make use of Unicode textually, I advise against
it. Browser-support can be limited or spotty, styling just adds more technical
requirements to achieve what you want, Unicode-based symbols/characters will
be meaningless to vision-impaired people and don’t we type enough already?

Resources Used to For The Unicode Symbol to Graphic Conversion

My life a as a web developer has become much easier because of the
following resources (worth the download even if you don’t use them to
for the technique described in this article):

How to Make the Conversion from Unicode Symbol to Graphic

Open FireFox and Firebug: go to any Page

From the Firebug console that opens in the bottom of the brower,click the tab labeled HTML.

Next, click Edit, located to the right of the bug graphic after the
word Inspect.

Begin typing the following somewhere in the body element of
the HTML that you see: <p> & #x25BC; (remove the space between & and #)<p>. Notice that a new
symbol now appears in the page that you’re on. Keep this page open.

Right-click for Aardvark

Right-click anywhere on the page. Select Start Aardvark from the
context menu that appears.

Move the mouse over the (down-arrow) symbol that you created. An orange
border should appear. With the symbol highlighted press the i on
the keyboard. Now the symbol is all by itself.

Now press CMD/apple key (or CTRL if you’re on a PC) plus the plus symbol “+”
until you enlarge the arrow as much as possible.

Grab it with Screengrab

Right-click on the page. Select Screengrab > Save…
> Selection…

Notice that the page is highlighted in pink or some color. Click and hold
at a point to the top and left of the symbol.

While holding the left-mouse
button down, drag in a south-east direction until the shape is surrounded
by a rectangle and release.

In the Save As textbox of the dialog that appears rename your
shape to “DownArrow.png” (without the quotes).

Open it with GIMPshop and begin making it the symbol you’ve always wanted.
Enjoy!