Created on 01 May 2018 ;    Modified on 01 May 2018 ;    Translationitalian

How start a Django project using virtualenv

Caution. If you use Python in version 3.3 or higher, virtualenv is included in its libraries, in the venv module.

For this reason the suggested command to install a new one virtual environment become:

pyhton -m venv ven

where the first venv invoke the module, and the second is (by convention, you are free to choose a whatever name you like) the directory in which to install the Python interpreter and the needed libraries.

Just a quick note about starting up a Django environment using virtualenv.

Create the project

Creating the project 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

Create the .gitignore file in git-directory. It contains:

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

Initializing the git repository:

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

Creating Django's project DB and superuser, checking project is running, saving it in git repository:

$ 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"

Now we have:

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

and we are in git-directoy/project-name directory.

How create an application (app)

Creating a django's app:

$ 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

obtainig this directory structure:

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