Following on from my last post, I have two basic Django sites up and running.
So now I want to tackle the next round of topics. Note that there’s a great list of useful python packages in pythonanywhere’s “batteries included” page.
- virtualenv. virtualenv is a great package for making sure your apps continue to function when you update the underlying components’ versions… e.g. I’m using some features of Django 1.4 that I know will be deprecated in Django 1.5.
- South. I have already been frustrated when I add a new field to a table (or object), only to find Django cannot update the database tables for me. South sets out to solve this problem, and is as simple as adding another installed app to your settings file.
What follows is my record of how I got on with each, in the hope that I might help others who trip over the same pitfalls.
I found a great post that record some of the same discoveries here, covering git, virtualenv, south and fabric. I’m already using git, and plan to use fabric as my needs grow.
I just grabbed the single file virtualenv.py from the website, put it in my own Python directory and ran it by literally typing:
python virtualenv.py ENV
I wasn’t sure if ENV was meant to be ENV or actually represented an environment name of my choice, so to be safe I used ENV. It turns out to be the environment name of your choice.
I discovered later that what I should have done was:
python virtualenv.py ENV --system-site-packages
so that I could still access MySQLdb, for example, without having to install this separately into ENV. Unfortunately I don’t see a way to retroactively do this, even for individual packages, though there is virtualenvwrapper that may give some of this functionality (e.g. see this Google groups post). I might just go straight to using virtualenvwrapper.
OK, so now I have a subdirectory named ENV in the same place I put
virtualenv.py, with subdirectories
lib/. I typed
and understand that everything I do now is using this virtual environment. I tried to deactivate it in the same way, but actually all you need to type is:
With ENV activated, I installed Django-CMS into ENV via
sudo pip install Django==1.4 django-cms south
(And because I did not do the
--system-site-packages thing earlier, I also had to install MySQLdb pointing at my MAMP’s MySQL installation as follows…
pip install mysql-python . This gave the same error I found before when I tried to get this working – see this post. So I opened up
~/Python/virtualenv/ENV/build/mysql-python/site.cfg and added the line:
mysql_config = /Applications/MAMP/Library/bin/mysql_config . Then I retyped
pip install mysql-python, and it installed fine.)
Note you can use virtualenvwrapper at pythonanywhere using the advice here.
I added “south” to my existing project’s list of installed apps (in my Django project’s
settings.py file), and typed:
python manage.py syncdb
This set up the new South data table
This link explains how to load initial data into your tables.
Standard data you want in your model is called a fixture in Django. You can make a fixture by first entering data (e.g. through the admin interface), then using the
dumpdata command. E.g. To turn the data in the model
myapp.Colour into a fixture, you can just do this:
python manage.py dumpdata myapp.Colour --indent 2 > myapp/fixtures/colours.json
Later, use the
loaddata command to load this in.
There is actually a gotcha here – you need to specify an extension on your fixtures filename – i.e. do not just use
colours. When you load the data, you can leave the extension off.