Day two of working with Django, I think I am loving it. It seems so simple and clean. Plus, I have always favored Python a little, just never had it in me to start playing with it. Want to try it out yourself? Follow me to Python-land!
I assume the following:
- You have Python installed, working, and you know how to start it from the CLI
- You have the mental ability to go to Django and install the framework
Now I have also fallen in love with a great Python addition called virtualenv. It basically creates a virtual environment for you to complete your Python project in (allowing you to install modules needed on a project specific basis), Sweet. Let’s add that to the list of things I expect from you:
- Go install virtualenv if you want to
Okay, you’ve got all that, now let me share with you how I start a project.
1. Create my virtualenv instance:
>>virtualenv --system-site-packages /Users/user/Sites/env/project_name
Do this if you decided to install virtualenv, I decide to keep the files in my Sites folder, just so everything is tidy for me – but you can place your virtual environments anywhere you like. Also, the flag --system-site-packages
includes the python packages you have preinstalled, I would recommend using it.
2. Change into the directory you just created
>>cd /Users/username/Sites/env/project_name
3. Activate your virtualenv instance
>>source bin/activate
You are now running a separate instance of python, anything you install here will not be usable with other instances of pyton. You will notice your path changes to include the name of your project in parenthesis directly before your machine name as well.
4. Move into directory where you would like to create your project
5. Create project
>>django-admin.py startproject project_name
6. Move into directory that you just created
7. Edit your settings.py file to include your database settings
>>vi settings.py
8. Do the database sync to populate tables your application needs
>>python manage.py syncdb
9. Start the built in, handy dandy, web server
>>python manage.py runserver
Ta-da! Mission: Accomplished…