@ wrote... (5 years, 9 months ago)

Django works much better if you do things the right way. Here are the steps for future me.

I've compiled these instructions from several sources, some of which are here:

I'm assuming you're on OSX. If you're on Linux most of these steps will be the same. If you're on Windows… Install VMWare and then install Linux.

I've added a tl;dr at the end.

Get python working properly

I've moved this section into its own post. I highly recommend spending some pain now to get all this working, it will make your life so much easier in the future.

Automatically activate virtualenv

Make a django project directory

I put all my projects under ~/src/.

The is the actual command to create a virtual environment.

mkvirtualenv --no-site-packages project_name

And here is how to do it with some visual feedback.

mkvirtualenv --no-site-packages project_name
deactivate # this makes it more fun later
cd ~
mkdir src
cd ~/src
mkdir project_name
touch project_name/.venv
cd project_name # you should now be using virtenv automatically
cd .. # stop using virtenv automatically

Use source code control

If you're not going to use any source code control then go away and surf YouTube and post witty comments.

I prefer Mercurial over Git. Git the technology is awesome and powerful, git the command line tool is an abomination. But I digress. Use whichever tool you prefer (even subversion, but seriously, time to upgrade).

cd ~/src/project_name
hg init
hg add .venv
# create an .hgignore file
cat << EOF > .hgignore
^static
^media
pyc$
pyo$
EOF
hg add .hgignore
hg commit -m "initial commit with .venv and .hgignore"

Install django et al.

Make sure you're in your project directory so that you are using your virtual environment. which python should give something like /Users/kurt/.virtualenvs/project_name/bin/python, not /usr/bin/python.

pip install django # the latest version, 1.5 as I write this
pip freeze > requirements.txt
hg add requirements.txt

Whenever you install a new package with pip make sure you update your requirements.txt file so that somebody else can reproduce your environment (via pip freeze > requirements.txt). Then do a hg commit. Keeping track of your library versions is very important if you ever want to copy your project to another computer.

Create your project

Because we created our project directory first (because we wanted to use auto virtualenv activation) we need to do a little directory shuffling so our project isn't an extra layer deep.

cd ~/src/project_name # your virtenv should activate
django-admin.py startproject project_name . # the period creates the project in the current directory and is new in django 1.4
ls
# project_name  manage.py  requirements.txt
chmod +x manage.py # make your life easier in the future
hg add manage.py project_name
hg commit -m "initial commit of new django project: project_name"

Your project has now been started so that's the end of this tutorial.

Having said that you probably also want to install (and then add to settings.py:INSTALLED_APPS) the following: South, Pinax, django_extensions, debug_toolbar, debug_toolbar_autoreload, django-devserver.

See this for a more complete list of great apps.

Don't forget to update requirements.txt after library/app installs.

Useful commands

pip freeze # to see installed packages
pip freeze > requirements.txt # to save requirements
pip install -r requirements.txt # to install everything in requirements file
./manage.py runserver_plus 0.0.0.0:8000
./manage.py shell_plus
./manage.py show_urls

tl;dr

pip install virtualenv virtualenvwrapper
source `which virtualenvwrapper.sh`
echo "source ~/.bash_virtenv" >> ~/.bashrc
# cp .bash_virtenv from here to ~/
mkvirtualenv --no-site-packages project_name
pip install django
django-admin.py startproject project_name
touch project_name/.venv
deactivate
cd project_name # voila
Category: tech, Tags: django
Comments: 0
Click here to add a comment