lost password?

home
•  xaraya
•  rails
•  django +
•  webdev
•  xamp +
•  musings

rss
Tag this page
   

» Blogs that link here
last modified: Feb 26, 2007
(first posted: Feb 10, 2007)
(2239 Reads)
keywords: django install
Permalink

Installing Django on my iMac

Using python, django, and mysql5.

Installing Django and Python was easier than Rails, but mostly because I already did the hard work with the Ruby/Rails install a few weeks ago. That included MacPorts, Apache2, MySQL5, and more.

Install

For my starting point, I used the Django online documentation, http://www.djangoproject.com/documentation/install/

But really I decided to use the MacPorts tool as much as I can.

The first step is to see if Python is on the Mac and what version

$ python
Python 2.3.5 (#1, Dec 25 2005, 07:24:19)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin

Seems good enough.

Next, Django itself. When i look in ports (sudo port list *django*) it says

py-django-devel @0.95 python/py-django-devel

But Django is at 0.95.1, so I assume ports is out of date, so I'll installing it manually (which is probably better since I may eventually go with the edge version in the Subversion repository).

So, I link to http://www.djangoproject.com/download/ and download the Django-0.95.1.tar.gz file. Then,

$ tar -xvzf Django-0.95.1.tar.gz
$ cd Django-0.95.1
$ sudo python setup.py install

It requires setuptools, and downloads it for me.

The next step is installing mod_python for Apache.

 $ sudo port install mod_python 

Oops, that's going ahead and installing a newer Python 2.4.3. No worries, probably a good idea.

When its done, I'm told to add the following line to my Apache2 httpd.conf (located on my system at /opt/local/apache2/conf/httpd.conf)

 LoadModule python_module modules/mod_python.so 

Actually, for the Django tutorials I'm going to use the built-in simple http server, so I won't get around to testing this mod_python is installed and working just yet.

Database. The docs recommend Postgres SQL, but I have mySQL and I'll use that. Install the python mysql drivers.

 $ sudo port install py-mysql

This installs 1.2.1_p2

So far so good. OK, lets fire up python and install Django. Assuming we're still in the Django directory,

 $ python
>>> import django
>>> django.VERSION
(0, 95.099999999999994, None)

A bit of a rounding error!? I assume this is python, not django's doing. Somehow I'm starting to get the feeling this framework is a bit geeky :)

One of the things this import does is create the django-admin.py file we need in our page, so I edit my ~/.profile and add

 export PATH=/opt/local/Library/Frameworks/Python.framework/Versions/Current/bin:$PATH 

Test It

Let's start with the Tutorial from documentation (http://www.djangoproject.com/documentation/tutorial1/) Abbreviated version:

I'm putting my project into ~/django/ so cd there to start. We'll call this "tut" (for tutorial)

$ django-admin.py startproject tut
$ cd tut
$ python manage.py runserver


- browse to http://127.0.0.1:8000/

- create a database to use (e.g using mysqladmin or, in my case, CocoaMySQL). Name it "tut".

- edit settings.py as needed to setup the database etc.

Create default tables defined in settings

$ python manage.py syncdb


That should verify our db connection etc. I check the db tables in cocoamysql.

Create App

$ python manage.py startapp polls

edit polls/models.py

class Poll(models.Model):
	question = models.CharField(maxlength=200)
	pub_date = models.DateTimeField('date published')
 
class Choice(models.Model):
	poll = models.ForeignKey(Poll)
	choice = models.CharField(maxlength=200)
	votes = models.IntegerField()

edit polls/settings/ph, add to INSTALLED_APPS

'tut.polls',

manage.py is a powerful tool. Let's sShow generated SQL:

 $ python manage.py sql polls

Check errors in model

 $ python manage.py validate polls

Show initial data required

 $ python manage.py sqlinitialdata polls

Show drop tables

 $ python manage.py sqlclear polls

Show create index

 $ python manage.py sqlindexes polls

Show combined sql

 $ python manage.py sqlall polls

Create the tables

 $ python manage.py syncdb

Play with api

 $ python manage.py shell
 >>> from tut.polls.models import Poll, Choice
 >>> Poll.objects.all()
 >>> from datetime import datetime
 >>> p = Poll(question="What's up?", pub_date=datetime.now())
 >>> p.save()
 >>> p.id
 >>> p.question 
 >>> p.pub_date
 >>> p.pub_date=datetime(2005,4,1,0,0)
 >>> p.save()
 >>> Poll.objects.all()
 [<Poll: Poll object>]

- edit models.py, add under class Poll

 def __str__(self):
 	return self.question

- add under class Choice

 def __str__(self):
	return self.choice

- restart console, or

 >>> reload(moduleName)
 >>> Poll.objects.all()

 

Admin Stuff

- edit settings.py, add to INSTALLED_APPS

'django.contrib.admin',
$ python manage.py syncdb

- edit urls.py, uncomment the admin pattern
- restart console
- browse to http://127.0.0.1:8000/admin/
login admin pwd
- change Poll class, and Choice class per tutorial http://www.djangoproject.com/documentation/tutorial2/
- edit settings.py

 TEMPLATE_DIRS = (
 	"/User/jonathan/django/tut/mytemplates",
 )

- copy base_site.html
from /usr/local/Django-0.95.1/admin/
to /User/jonathan/django/tut/mytemplates/admin/

Generate code for custom admin/index.html

 $ python manage.py adminindex polls

You can continue on your own from here, http://www.djangoproject.com/documentation/tutorial3/

Enjoy!

 

Bonus: iPython

$ sudo port install py-ipython  

This from #django:

[03:14a] cilkay​​: ​linoj: As I tell everyone who is new to Django and Python, install ipython <http://ipython.scipy.org/>. That will help you get up to speed quickly.
[03:15a] cilkay: python manage.py shell
[03:16a]: ​... and you'll be able to interactively prototype your Django app.
cilkay​​: ​linoj: say I'm fetching objects from the Category model like so: theCategory = Category.objects.get(slug=category_slug)
cilkay​​: ​In ipython, if I type theCategory. and hit tab, it will give me all the attributes and functions for theCategory
[03:18a] ​linoj​​: ​cool
[03:18a] ​cilkay​​: ​That's much more powerful than the standard Python shell.lin
[03:20a] linoj :​i was wondering about debugging in general, can you set breakpoints and examine variables etc in django apps?
[03:22a] ​cilkay​​: ​linoj: yes, but I rarely have to do that because I often prototype in ipython.
[03:22a] cilkay: pdb (Python debugger) is what you'd use if you wanted to debug.
[03:23a] cilkay: Besides, Python's stack traces when you have an exception are usually quite good, unlike PHP which just says "You have an error somewhere near here, I think."

 

There are no comments attached to this item.

Post a new comment

: This is not spam

Name :