There has been an increase in the need for web apps, however, most programming languages which support app development are complex. This means that longer periods of times are spent while developing these apps. Python has a framework called Django which provides web developers with a mechanism to develop web apps in an easy and quick manner. So its a good idea for you to learn how to use this framework for the development of web apps, all of which is explained in this book. Make sure that you install Python 2.6.5 or higher. Enjoy reading !
This book will save you time. On many occasions weve seen clever students get stuck, spending hours trying to fight with Django and other aspects of web development. More often than not, the problem was usually because a key piece of information was not provided, or something was not made clear. While the occasional blip might set you back 10-15 minutes, sometimes they can take hours to resolve. Weve tried to remove as many of these hurdles as possible. This will mean you can get on with developing your application instead of stumbling along .
This book will lower the learning curve. Web application frameworks can save you a lot of hassle and lot of time. Well, that is if you know how to use them in the first place! Often the learning curve is steep. This book tries to get you going - and going fast by explaining how all the pieces fit together .
This book will improve your workflow. Using web application frameworks requires you to pick up and run with a particular design pattern - so you only have to fill in certain pieces in certain places. After working with many students, we heard lots of complaints about using web application frameworks - specifically about how they take control away from them (i.e. inversion of control) .
To help you, weve created a number of workflows to focus your development process so that you can regain that sense of control and build your web application in a disciplined manner .
This book is not designed to be read. Whatever you do, do not read this book! It is a hands-on guide to building web applications in Django. Reading is not doing. To increase the value you gain from this experience, go through and develop the application. When you code up the application, do not just cut and paste the code .
Type it in, think about what it does, then read the explanations we have provided to describe what is going on. If you still do not understand, then check out the Django documentation, go to Stack Overflow or other helpful websites and fill in this gap in your knowledge .
NlN
Introduction
It is a free and open source framework, written in Python, and projects follow the Model-View-Template structure (usually abbreviated to MVT). Django emphasizes the reusability of the components as well as the rapid development, as well as the principle of non-repetition.
Python is used in all joints of this framework, such as settings, database templates, and more.
Popular websites that use Django are: Pinterest, Instagram, Mozilla, The Washington Times, Disqus, National Geographic and many more.
Django was developed in 2003 by programmers Adrian Holovaty and Simon Willson of Lawrence Journal World when they switched to Python to build applications. Django was launched in 2005 under the BSD license, named after the guitarist Django Reinhardt.
MVT structure
The Django project structure is divided into three related sections, but different from other frameworks that follow the MVC - Model, View, Controller framework such as Laravel in PHP, etc. Projects in Django consist of Model, View and Template.
Django supports many databases, such as SQlite, MySQL, and PostgreSQL.
A view is a set of Python functions that respond to a specific URL, and the view function is to determine which data and information should be displayed.
A template is an HTML file that determines how the information displayed by the display section will appear.
So where is the controller? The controller here is the framework itself, the mechanism by which the request is sent to the appropriate view based on a specific URL.
Install Django
The Django framework is one of the Python programming language packages.This language provides a special package manager called pip through which to install, update, and remove packages easily.Therefore, the first step in installing Django will be to make sure that the pip package manager is installed and installed if not available.
Install pip
To install the Django framework you will need a Python package manager which is pip. Fortunately, pip is available in Python 2.7.9 and later, and in Python 3.4 and later.
If you don't have pip installed in your installed Python version, you can install it by following these steps:
Download the file get-pip.py.
On the command line, go to where you downloaded the previous file, and then type the following instruction:
python get-pip.py
Use the command line
Now try using the Python package manager to install Django, now go to the command line and type the command:
pip install django == 1.9
Did you see an error message? What's the problem, did we just install pip?
This is true, but we did not tell the command line to direct any instruction starting with pip to the Python package manager, and to do so follow these steps:
In Ubuntu:
If you are using Python v3 or python-pip for Python v2, you must install the python3-pip package in order to use pip in the Ubuntu command line. To do this, type the following command at the command line:
sudo apt-get install python3-pip
Enter your password, and the installation process will start, and once finished you can install any Python package directly from the command line.
On Windows:
In Windows, the following line should be added:
C: \ Python34 \ scripts;
To do this, follow these steps:
Right-click the Computer icon and choose Properties from the drop-down menu:
Click the Advance system settings icon, and in the pop-up dialog click on the Environment Variables icon.
Double-click the Path system variable at the bottom of the pop-up dialog box.
Add the previous line to the end of the text string, after the semicolon (;) (if there is no semicolon at the end of the line, add it).
Click Ok and then close the rest of the windows by pressing Ok.
You can now use pip directly from the command line.
`
Virtual Environment
Before you begin installing Django, we will install a very useful tool that will help arrange the software environment on your computer. This step can be skipped, but it is strongly recommended.
The virtual environment isolates your Python or Django projects from each other, meaning that modifications to a particular website will not affect other projects you are working on.
The default environment will contain Python executables as well as a copy of the pip library that you can use to install various Python packages.
We will create a folder that will contain the default environment that we will create shortly.
mkdir mysite
cd mysite
Creating the virtual environment requires the installation of a virtualenv package and we will use pip to do this:
pip install virtualenv
To use virtualenv from the command line directly in Ubuntu, you must install the virtualenv package, and to do so, type the following command at the command line:
sudo apt-get install virtualenv
After the installation is complete, you can create the default environment as follows:
virtualenv myvenv
This code will create a virtual environment, which is a collection of folders.
To activate the new Windows virtual environment, use the following code:
myvenv \ Scripts \ activate
For Linux and OS X, use:
Next page