Installation
Django can be obtained in several ways
directly from the project homepage http://djangoproject.com
if you are using Linux, you can install Django from your package repository
installing withpip- tool for installing Python packages - recommended option
The recommended way of installation is to usevirtualenv, orpyvenvto isolate application specific dependencies from a shared Python installation.
[info] For info
Use this for infomation messages.
#include <stdio.h> int main(){ printf("kurnik"); }
Creating virtual environment
Create new directory for your project and runvirtualenvinside:
$ mkdir project/
$ cd project/
$ virtualenv env
Activate the environment
$ source env/bin/activate
(env)$
After activation, the$PATH
environment variable has changed:
(env)$ echo $PATH
/.../env/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:...
Note: It is not neccessary to activate environment. In this case you have to type path to the tool you want to use in environment manualy:
$ env/bin/python
Installing django with pip
First, search for django in PyPI package database:
(env) $ pip search django
The list will contain all the entries, where the text “django” is located in the name or description of the package.
To install django, type:
(env) $pip install django
Downloading/unpacking django
Downloading Django-1.6.5-py2.py3-none-any.whl (6.7MB): 6.7MB downloaded
Installing collected packages: django
Successfully installed django
Cleaning up…
(env) $
Comment: To show info about currently installed packages, use info
option for pip
command:
(env) $ pip show django
Name: Django
Version: 1.6.5
Location: /home/mirek/Documents/kurzy/python/django/env/lib/python2.7/site-packages
Requires:**
(env) $
Note: It is possible to install concrete version of package. For example - to install version 1.5.1 of django, type:
(env) $pip install django=='1.5.1'
Checking installation
To check, if Django is installed, open Python’s interactive shell and type:
(env) $ipython
>>>import django
>>>django.get_version()
'1.6.5'
Creating a project
After installation,django-admin.pycommand is available in theenv/bin/directory.django-adminisDjango’scommand-line utility for administrative tasks.It has several commands, which you can use for project manipulation.
In the case of project creation, use commandstartprojectfollowing with the name of newly created project:
(env) $ django-admin.py startproject megaproject
New project will be created in the directory with the name of your project. Let’s examine the project structure:
(env) $ tree megaproject
mysite/
├── manage.py
└── mysite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
1 directory, 5 files
These files are:
The outermysite/root directory is just a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
manage.py: A command-line utility that lets you interact with thisDjangoproject in various ways.
The innermysite/directory is the actualPythonpackage for your project. Its name is thePythonpackage name you’ll need to use to import anything inside it (e.g.mysite.urls).
mysite/__init__.py: An empty file that tellsPythonthat this directory should be considered a Python package.
mysite/settings.py: Settings/configuration for thisDjangoproject.
mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site.
mysite/wsgi.py
: An entry-point for WSGI-compatible web servers to serve your project.
Running the project
Django comes with development web server - lightweight web server written in pure Python for rapid development purposes. To start the web server with the project, run manage.py
from the mysite/directory
with the following syntax:
(env) $ python manage.py runserver
Validating models...
0 errors found
August 08, 2014 - 18:51:55
Django version 1.6.5, using settings mysite.settings'
Starting development server at
http://127.0.0.1:8000/
Quit the server with CONTROL-C.
By accessing thehttp://127.0.0.1:8000/URL you will see the Django’s welcome page.
Note:The built-in web server is for development purposes only! Don’t use it in the production!
To change port, pass the port number as another argument:.
(env) $python manage.py runserver 4000
To change the server IP address to listen for all public connections, pass it as parameter:
(env) $python manage.py runserver 0.0.0.0:1234
Note:If your code changes, the web server reloads your code automatically. Open’n’Save one of the files of the project to see web server will reload the project.