Overview

README

Installation virtualenv

sudo aptitude install python-virtualenv
Les NOUVEAUX paquets suivants vont être installés :
python-virtualenv
0 paquets mis à jour, 1 nouvellement installés, 0 à enlever et 0 non mis à jour.
Il est nécessaire de télécharger 2 111 ko d'archives. Après dépaquetage, 2 311 ko seront utilisés.
Prendre : 1 http://fr.archive.ubuntu.com/ubuntu/ quantal/universe python-virtualenv all 1.7.1.2-2 [2 111 kB]
2 111 ko téléchargés en 3s (702 ko/s)
Sélection du paquet python-virtualenv précédemment désélectionné.
(Lecture de la base de données... 917277 fichiers et répertoires déjà installés.)
Dépaquetage de python-virtualenv (à partir de .../python-virtualenv_1.7.1.2-2_all.deb) ...
Traitement des actions différées (« triggers ») pour « man-db »...
Paramétrage de python-virtualenv (1.7.1.2-2) ...

Installation virtualenvwrapper

sudo pip install virtualenvwrapper -U
Requirement already up-to-date: virtualenvwrapper in /usr/local/lib/python2.7/dist-packages
Downloading/unpacking virtualenv from http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.8.4.tar.gz#md5=1c7e56a7f895b2e71558f96e365ee7a7 (from virtualenvwrapper)
Downloading virtualenv-1.8.4.tar.gz (1.9MB): 1.9MB downloaded
Running setup.py egg_info for package virtualenv

warning: no previously-included files matching '*' found under directory 'docs/_templates'
warning: no previously-included files matching '*' found under directory 'docs/_build'
Requirement already up-to-date: virtualenv-clone in /usr/local/lib/python2.7/dist-packages (from virtualenvwrapper)
Requirement already up-to-date: stevedore in /usr/local/lib/python2.7/dist-packages (from virtualenvwrapper)
Downloading/unpacking distribute from http://pypi.python.org/packages/source/d/distribute/distribute-0.6.35.tar.gz#md5=e55298c7e3a233df1a47a4881a0c9800 (from stevedore->virtualenvwrapper)
Downloading distribute-0.6.35.tar.gz (644kB): 644kB downloaded
Running setup.py egg_info for package distribute

Installing collected packages: virtualenv, distribute
Found existing installation: virtualenv 1.7.1.2
Uninstalling virtualenv:
Successfully uninstalled virtualenv
Running setup.py install for virtualenv

warning: no previously-included files matching '*' found under directory 'docs/_templates'
warning: no previously-included files matching '*' found under directory 'docs/_build'
Installing virtualenv script to /usr/local/bin
Installing virtualenv-2.7 script to /usr/local/bin
Found existing installation: distribute 0.6.28dev-r0
Uninstalling distribute:
Successfully uninstalled distribute
Running setup.py install for distribute
Before install bootstrap.
Scanning installed packages
Setuptools installation detected at /usr/lib/python2.7/dist-packages
Non-egg installation
Moving elements out of the way...
Already patched.
/usr/lib/python2.7/dist-packages/setuptools.egg-info already patched.

Installing easy_install script to /usr/local/bin
Installing easy_install-2.7 script to /usr/local/bin
After install bootstrap.
Creating /usr/local/lib/python2.7/dist-packages/setuptools-0.6c11-py2.7.egg-info
Creating /usr/local/lib/python2.7/dist-packages/setuptools.pth
Successfully installed virtualenv distribute
Cleaning up.

Modification du .bashrc

export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenwrapper.sh
assr38@vercors:/usr/local/bin$ source /usr/local/bin/virtualenvwrapper.sh
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/get_env_details
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/prermproject
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/postrmproject
assr38@vercors:/usr/local/bin$

mkvirtualenv --no-site-packages django16

New python executable in django16/bin/python
Installing setuptools............done.
Installing pip...............done.
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/django16/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/django16/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/django16/bin/preactivate
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/django16/bin/postactivate
virtualenvwrapper.user_scripts creating /home/assr38/.virtualenvs/django16/bin/get_env_details

workon django16

assr38@vercors:~$ workon django16
(django16)assr38@vercors:~$

deactivate

(django16)assr38@vercors:~$ deactivate assr38@vercors:~$

Installation de Django (pip install --upgrade git+http://github.com/django/django.git)

pip install --upgrade git+http://github.com/django/django.git


Downloading/unpacking git+http://github.com/django/django.git
Cloning http://github.com/django/django.git to /tmp/pip-yONIHq-build
Running setup.py egg_info for package from git+http://github.com/django/django.git

warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
Installing collected packages: Django
Found existing installation: Django 1.5c1
Uninstalling Django:
Successfully uninstalled Django
Running setup.py install for Django
changing mode of build/scripts-2.7/django-admin.py from 664 to 775

warning: no previously-included files matching '__pycache__' found under directory '*'
warning: no previously-included files matching '*.py[co]' found under directory '*'
changing mode of /home/assr38/.virtualenvs/django16/bin/django-admin.py to 775
Successfully installed Django
Cleaning up...

Vérification de la version installée

(django16)assr38@vercors:~$ python
Python 2.7.3 (default, Sep 26 2012, 21:53:58)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'1.6'
>>>

which django-admin.py

(django16)assr38@vercors:~$ which django-admin.py /home/assr38/.virtualenvs/django16/bin/django-admin.py

Tutorial 1

django-admin.py startproject mysite

django-admin.py startproject mysite

python manage.py runserver 8080

February 16, 2013 - 17:10:47
Django version 1.6, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8080/
Quit the server with CONTROL-C.

Create *.sh file commands

chmod +x *.sh

chmod +x syncdb.sh chmod +x run_server.sh

Edit the settings.py file

# https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/home/assr38/projects/djangotest/mysite/db.sqlite3',
    }
}
# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/
LANGUAGE_CODE = 'fr-FR'
TIME_ZONE = 'Europe/Paris'

syncdb.sh (python manage.py syncdb)

assr38@vercors:~/projects/djangotest/mysite$ ./syncdb.sh
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session

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
Username (Leave blank to use 'assr38'):
E-mail address: pvergain@gmail.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
No fixtures found.

Creating models

Now that your environment – a “project” – is set up, you’re set to start doing work.

Each application you write in Django consists of a Python package that follows a certain convention.

Django comes with a utility that automatically generates the basic directory structure of an app, so you can focus on writing code rather than creating directories.

python manage.py startapp polls

assr38@vercors:~/projects/djangotest/mysite$ tree
├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── polls
│   │   ├── __init__.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   └── views.py
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── run_server.sh
└── syncdb.sh

sqlpolls.sh (python manage.py sql polls)

assr38@vercors:~/projects/djangotest/mysite$ ./sqlpolls.sh
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

Now, run syncdb again to create those model tables in your database:

./syncdb.sh
assr38@vercors:~/projects/djangotest/mysite$ ./syncdb.sh
Creating tables ...
Creating table polls_poll
Creating table polls_choice
Installing custom SQL ...
Installing indexes ...
No fixtures found.

Playing with the API

Now, let’s hop into the interactive Python shell and play around with the free API Django gives you.

To invoke the Python shell, use this command:

python manage.py shell
assr38@vercors:~/projects/djangotest/mysite$ chmod +x manage_shell.sh
assr38@vercors:~/projects/djangotest/mysite$ ./manage_shell.sh
**********************************************************************
Welcome to IPython. I will try to create a personal configuration directory
where you can customize many aspects of IPython's functionality in:
/home/assr38/.ipython
WARNING:
Installation error. IPython's directory was not found.
Check the following:
The ipython/IPython directory should be in a directory belonging to your
PYTHONPATH environment variable (that is, it should be in a directory
belonging to sys.path). You can copy it explicitly there or just link to it.
IPython will create a minimal default configuration for you.
Please press <RETURN> to start IPython.
**********************************************************************
Python 2.7.3 (default, Sep 26 2012, 21:53:58)
Type "copyright", "credits" or "license" for more information.
IPython 0.8.3.svn.r3001 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

 from mysite.polls.models import Poll, Choice