Development with Django INDEX

[Pages:53]Development with Django

INDEX 1. Preparation 2. Install Django 3. A new Django Project 4. The django-marcador app 5. Create the Models 6. Database Migration 7. Admin Site 8. URLs and Views 9. Templates 10. Setting Up Frontend Login 11. Forms

Python programming Batches are conducted at SCTPL@ Chembur & Kandivali Only

Page 1

1. Preparation

We'll use Django Version 1.8. To get started we need to do a little preparation.

1.1. Python

Django is written completely in Python. Therefore Python needs to be installed first.

Note Django 1.8 supports Python from version 2.7. If you have an older version of Python, you should update it. Django supports Python 3 since version 1.5. You can find out which version of Python you're running by using the command line option--version:

$ python --version Python 2.7.6

Note If you are using Python 3 please make sure you have Python 3.3.2 or greater installed. Otherwise there will be problems.

Also consider adding the following future-import on top of every Python file you are going to edit to ensure Python 2 and 3 compatibility:

from __future__ import unicode_literals

This way all regular strings will be unicode string literals.

If you want to learn more read the Python 3 part of the Django documentation. If you've already got the right version of Python installed, you can skip ahead to Python Package Managers.

Python programming Batches are conducted at SCTPL@ Chembur & Kandivali Only

Page 2

1.1.2. Linux

Many Linux distributions come with Python already installed. If you haven't got a version of Python installed, you can normally use your package manager to download and install it.

Alternatively, you can get the Python Sources from the website and compile it yourself.

1.1.3. Windows

Download the Installer from the Python Website and install it.

So that Python works under Windows as expected, you need to change the environment variable%PATH%. In the examples, we'll assume that your Python is installed in C:\Python27\.

1.1.3.1. Windows 7

1. Start, then right click on Computer 2. Now click the context menu option Properties 3. Next, in the window that just opened, click on the Advanced System

Settings 4. A further window will open, click the Environment Variables 5. Under System Variables, select the PATH 6. Now click on Edit and add the required

directory: ;C:\Python27\;C:\Python27\Scripts;. (The semi-colon at the beginning is required!) 7. Now close the windows Environment Variables and System Properties by clicking on OK.

1.1.3.2. Windows XP

1. Start Control Panel System Advanced

Python programming Batches are conducted at SCTPL@ Chembur & Kandivali Only

Page 3

2. Click on the Environment Variables, then a new window will open. Under "System Variables" select Path

3. Now click on Edit and add the required directory: ;C:\Python27\;C:\Python27\Scripts;. (The semi-colon at the beginning is required!)

4. Now close the windows Environment Variables and System Properties by clicking on OK.

1.2. Python Package Manager

Python has its own package system to manage distribution and installation of Python packages. Because we will need to install several packages, we must first make sure the package manager pip is installed. pip was originally written as an improvement of easy_install.

To decide which installation procedure is right for you check again your Python version:

$ python --version Python 2.7.6

1.2.1. Python 3.4 and Python 2.7.9

If you have Python 3.4 or Python 2.7.9 (or newer) installed you can use ensurepip to install or upgrade pip:

$ python -m ensurepip --upgrade

If your Python version is too old you will simply see an error message like that:

$ python -m ensurepip --upgrade /usr/bin/python: No module named ensurepip

1.2.2. Python 3.3 and Python 2

If you have Python 3.3 or Python 2 with a version lower than 2.7.9 installed, your Python installation does not contain the module ensurepip. In this case pip can be installed with the help from a bootstrap script. If curl is

Python programming Batches are conducted at SCTPL@ Chembur & Kandivali Only

Page 4

installed, you can use it to download pip at the command line. Otherwise just use the browser.

$ curl -O

When the bootstrap script has been downloaded execute it to install pip:

$ python get-pip.py

Note

Under Linux and Mac OS X root privileges may be required. In this case use:

$ sudo python get-pip.py

Note

If you have problems running get-pip.py because of the configuration of your internet connection please have a look the pip installation documentation. You can delete the bootstrap script when the installation has been finished.

1.2.3. Test pip

After a successful upgrade or installation, you can test pip as follows:

$ pip --version pip 7.0.1 from /Users/keimlink/.virtualenvs/django-marcador/lib/python2.7/sitepackages (python 2.7)

Python programming Batches are conducted at SCTPL@ Chembur & Kandivali Only

Page 5

2. Install Django

Let's install Django! We'll use version 1.8 in this tutorial. Newer releases should work as well, but if you are in doubt, use 1.8:

$ pip install --upgrade Django==1.8.2

Note

Under Linux and Mac OS X root privileges may be required. In this case use:

$ sudo pip install --upgrade Django==1.8.2

Note

If you want to isolate the Python packages from the system site directories you have two options:

1. Install the package(s) into a directory in your home directory using:

2. $ pip install --user

3. Use a virtual environment for your project. One way to create a virtual environment is to install virtualenv. If you are using Python 3 you can also use the new venv module.

If you go for one of the options you can use it any time this tutorial asks you to run pip install. After a successful installation, you can check the Django version number with the following command:

$ django-admin.py --version 1.8.2

Note

It could be that the file django_admin.py is actually called django-admin. That's not a problem, just leave off the extension .py.

Python programming Batches are conducted at SCTPL@ Chembur & Kandivali Only

Page 6

On Windows you may get an ImportError when you try to run django-admin.py. Prefix all commands that use .py files with python and use the full path to the file, like so:

> python C:\Python27\Scripts\django-admin.py

2.1. Install extra packages for this tutorial

We will use Bootstrap 3 to get the frontend done quickly. Since Bootstrap expects a certain markup structure, we will use the excellent django-crispyforms package to get our forms rendered nicely.

Starting with version 1.4, Django supports Timezones. This is activated by default and it is highly recommended to install the pytz package.

To install both packages execute:

$ pip install pytz django-crispy-forms

Note Under Linux and Mac OS X root privileges may be required.

Python programming Batches are conducted at SCTPL@ Chembur & Kandivali Only

Page 7

3. A new Django Project

3.1. Start the Django Project

You can create a new Django project with the following command:

$ django-admin.py startproject mysite

After you've run the command you'll find the following structure:

mysite |-- manage.py `-- mysite

|-- __init__.py |-- settings.py |-- urls.py `-- wsgi.py

3.2. Test the Development Server

After you've created the project, you can change to the directory mysite:

$ cd mysite

And try out the development server with the following command:

$ python manage.py runserver Performing system checks... System check identified no issues (0 silenced). May 30, 2015 - 17:19:37 Django version 1.8.2, using settings 'mysite.settings' Starting development server at Quit the server with CONTROL-C.

Note

If you are on Windows and the command fails with an UnicodeDecodeError, use this command instead:

$ python manage.py runserver 0.0.0.0:8000

Now you can open the "Welcome to Django" site from . After you've opened the site, you can kill the development server with CTRL + C.

Python programming Batches are conducted at SCTPL@ Chembur & Kandivali Only

Page 8

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download