### UBUNTU 12.04 PYTHON 2.7 3 DJANGO 1.4.2
`sudo apt-get update; sudo apt-get install python curl`
`wget -qO- https://bootstrap.pypa.io/get-pip.py | sudo python # http://pip.readthedocs.org/en/latest/installing.html`
> ...Successfully installed pip
** Recommended: pip install virtualenv **
- - -
http://pypi.python.org/pypi/setuptools#windows
download http://peak.telecommunity.com/dist/ez_setup.py
python ez_setup.py
Installed c:\python27\lib\site-packages\setuptools-0.6c11-py2.7.egg
Ensure your PATH (echo %PATH% or $PATH) contains c:\Python27 and c:\Python27\Scripts
HIGHLY RECOMMEND git for windows MINGW32
- - -
python --version
sudo apt-get install curl
sudo pip install django
> ...Successfully installed django
python -c "import django; print( django.get_version() )"
django-admin.py --version
OPTIONALLY: pip install pytest mock requests (for GAE: webapp2 webob)
pip freeze # display all installed modules
- - -
### Create a new django project
NOTE: python (django) source code should not live in the web server's document root as it would be viewable
django-admin.py startproject PROJECTNAME
(ubuntu djano install = django-admin startproject PROJECTNAME )
(django 1.4.2 creates the following)
PROJECTNAME/
manage.py
PROJECTNAME/
__init__.py
settings.py
urls.py
wsgi.py
chmod +x PROJECTNAME/manage.py (shorter than typing "python manage.py" all the time)
PROJECTNAME/manage.py runserver (NEVER USE THIS WEB SERVER IN PRODUCTION, IT IS DEV ONLY)
http://127.0.0.1:8000/
It worked!
Congratulations on your first Django-powered page.
PROJECTNAME/manage.py runserver 0.0.0.0:8080 (binds to all ip addresses on port 8080)
(quit with control + c)
- - -
### Configure Django project settings
cd PROJECTNAME/PROJECTNAME
nano settings.py (use sqlite3 which is included with python and will auto create the database file)
DATABASES = {
'default': {
# 'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
# 'NAME': '', # Or path to database file if using sqlite3.
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': '/home/ubuntu/workspace/PROJECTNAME/db.sqlite3',
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
TIME_ZONE = 'UTC'
cd ..
./manage.py syncdb (if necessary creates the sqlite file and database tables)
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no):
yes , Enter the username, email, and password of the super user of the database
(django already includes a built in user database)
- - -
### CREATE A NEW APPLICATION AND MODEL
./manage.py startapp polls
nano polls/models.py
from django.db import models
# Create your models here.
class Poll( models.Model ):
question = models.CharField( max_length=200 )
pub_date = models.DateTimeField( 'date_published' )
def __unicode__(self):
return self.question
class Choice( models.Model ):
poll = models.ForeignKey( Poll )
choice_text = models.CharField( max_length=200 )
votes = models.IntegerField()
def __unicode__(self):
return self.choice_text
(MAKE SURE YOU GET YOUR INDENTATION RIGHT!)
./manage.py validate
- - -
### REGISTER OUR APPLICATION
nano PROJECTNAME/settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'polls',
)
(prepare the update of the database with the new application's model)
./manage.py sql polls
BEGIN;
CREATE TABLE "polls_poll" (
"id" integer NOT NULL PRIMARY KEY,
"question" varchar(200) NOT NULL,
"pub_date" datetime NOT NULL
)
;
CREATE TABLE "polls_choice" (
"id" integer NOT NULL PRIMARY KEY,
"poll_id" integer NOT NULL REFERENCES "polls_poll" ("id"),
"choice_text" varchar(200) NOT NULL,
"votes" integer NOT NULL
)
;
COMMIT;
./manage.py validate (check for errors in your model)
./manage.py syncdb (sync the database with the new model)
Creating tables ...
- - -
manage.py shell (shell specific to this project)
from polls.models import Poll, Choice
Poll.objects.all()
from django.utils import timezone
p = Poll(question="What's new?", pub_date=timezone.now())
p.save() #required!
p.id # now the database has a new object
p.question
"What's new?"
p.pub_date
datetime.datetime(2012 ...UTC)
Poll.objects.all()
- - -
- - -
IF YOU HAVE MULTIPLE VERSIONS OF PYTHON + DJANGO TO SUPPORT
sudo apt-get install python-virtualenv
virtualenv django142 (to remove use virtualenv clear django142)
source django_1_4_2/bin/activate
- - -
TO INSTALL AN OLD DEPRECATED VERSION OF DJANGO
sudo apt-get install python-django (hmmm, the old version 1.3)
python -c "import django; print( django.get_version() )"
IF YOU ENCOUNTER THE ERROR:
"An error occurred while trying to run get-pip.py. Make sure you have setuptools or distribute installed."
make sure you've run apt-get install python-setuptools