Created on 01 May 2018 ;    Modified on 19 Sep 2018 ;    Translationenglish

Avviare un progetto Django usando virtualenv

Attenzione. Se si usa Python in versione 3.3 o superiore, virtualenv è incluso nelle sue librerie, nel modulo venv.

Per questo motivo il comando suggerito per installare un nuovo ambiente virtuale è divenuto:

pyhton -m venv venv

dove il primo venv richiama il modulo, e il secondo venv è (per convenzione, si può usare un qualunque nome) la directory in cui installare l'interprete Python e le librerie volute.

Una nota su come avviare un progetto Django usando virtualenv.

Creare il progetto

Creare la directory del progetto, ad esempio git-directory:

$ mkdir git-directory    # ancestor of all master directories. will be:
                         #   git-directory/
                         #       |-- .git/
                         #       |-- venv/
                         #       +-- project-name
$ cd git-directory
$ virtualenv venv        # create venv directory,
                         # it contains python, django, other pyth.libs
$ venv\Scripts\activate  # activate the project virtual environment
                         # in MS Windows Op.System. In linux use:
                         # source venv/bin/activate
$ pip install django     # load django libs in venv
$ django-admin.py startproject project-name # create the django project
                                            # (alias: site) directory.
                                            # BEWARE: app(s) will have
                                            # other name(s) then this

Creare il file .gitignore nella git-directory. Deve contenere:

__pycache__
migrations
venv
*.DS_Store
*.lnk
*.mo
*.pyc
*.sqlite3
settings.py
secretkey.txt

Inizializzare il repository git:

$ git init
$ git add -A
$ git commit -am "initial commit"

Crare il DB del progetto Django e il supeuser, controllare che il progetto si avvi, salvarlo nel repository git:

$ cd project-name
$ python manage.py migrate
$ python manage.py createsuperuser
... # compiling the request answers
$ python manage.py runserver       # starts django's http server
... # browse to http://localhost:8000 it shows the welcome django window
^C                                 # stops django's http server
cd git-directory
git status
git commit -am "empty django-project"

Abbiamo la seguente struttura di directory:

git-directory/
   |-- .git/
   |-- venv/
   +-- project-name/
          |-- manage.py
          +-- project-name/
                |-- __init__.py
                |-- settings.py
                |-- urls.py
                +-- wsgi.py

e siamo nella directory git-directoy/project-name.

Come creare una applicazione (app)

Per creare una applicazione (app) Django:

$ python manage.py startapp app-name
... # edit app-name/models.py to define db entities used by app
$ python manage.py makemigrations app-name
$ python manage.py migrate

ottenendo la seguente struttura di directory:

git-directory/
   |-- .git/
   |-- venv/
   +-- project-name/
          |-- manage.py
          |-- project-name/
          |      |-- __pycache__/  # this one will exists after
          |      |                 # at least a run of django's server
          |      |-- __init__.py
          |      |-- settings.py
          |      |-- urls.py
          |      +-- wsgi.py
          +-- app-name/
                 |-- __pycache__/  # as above
                 |-- migrations/
                 |-- templates     # we'll create this one manually
                 |                 # if our app uses its own templates
                 |-- __init__.py
                 |-- admin.py
                 |-- models.py
                 |-- tests.py
                 |-- urls.py
                 +-- views.py