Introduction of Django Django, development environment to build, to create a project application

Software and Django framework Introduction

Software framework

  • It is a software framework in which each of the software modules;
  • Each module has a specific function;
  • Between the module and the module is accomplished by cooperating software development.

Software framework is designed for a certain type of software problems that arise.

 

MVC framework

Xerox Palo Alto Research Center smalltalk language simula 67
software design patterns:

  • MVC generate ideas: the division of labor . Let special people do special things - input, processing, output
  • MVC core idea: decoupling .

 

Registered users through the browser MVC framework to understand information.

 

  • M: Model, model, interacting with the database;
  • V: View, View, generating html page;
  • C: Controller, controller, receives a request, processing, and interacts with the M V, return a response.

 

MVT Profile

Django Lawrence Publishing Group news content website. Python ---> MVC
rapid development and the DRY principle. Do not repeat yourself. Do not repeat some of the work themselves.

Registered users understand the browser frame MVT information.

 

  • M: Model, models, and M MVC, functionally similar, interacting with the database;
  • V: View, View, and the MVC C functionally similar, receives the request, processes, interact with T and M, returns a response;
  • T: Template, templates, and V in MVC same function, generate HTML pages.

 

Introduction to Django

Django, pronounced [ `dʒæŋɡəʊ], is written in python open source web development framework, and follow the MVC design.

Lawrence Publishing Group in order to develop content-based news website, and developed this framework, released under the BSD license in July 2005. The name comes from the jazz musician DjangoReinhardt Belgium, he is a gypsy, mainly playing guitar mainly, but also played violin and so on.

Because Django rapid development in recent years, more and more widely, has been selected to develop well-known IT magazine SDTimes as 2013SDTimes100, ranked "API, libraries, and frameworks" category No. 6, is considered the leader in this field.

The main purpose of Django is simple, rapid development of database-driven website. It emphasizes code reuse, multiple components can easily to "plug-in" as a service to the entire framework, Django has many powerful third-party plug-ins, you can even easily develop their own toolkit. This makes Django has strong scalability. It also stressed the rapid development and DRY (DoNotRepeatYourself) principles.

django official website: HTTPS: //www.djangoproject.com/
django Source: https: //github.com/django/django

Django framework that follows the MVC design, and there is a proper noun: MVT framework.

 

 

Django development environment to build

Virtual environment installation

  • Command to install python package before: sudo pip3 install the package name;
  • Installation path package: /usr/local/lib/python3.5/dist-packages;
  • Install different versions of the same package, the package will install after the installation of the original package overwritten . Thus, as two different versions of the same packet project depends on a machine, it will cause some projects fail.
  • The solution is to: a virtual environment.
  • Copy version of the virtual environment is real python environment.
  • python used in a virtual environment is a copy of python, python installation package is installed in the replicated.

 

Command to install the virtual environment :
1) sudo PIP virtualenv install # install the virtual environment
2) sudo pip install virtualenvwrapper # install virtual environment Expansion Pack
3) Edit home directory .bashrc file, add the following two lines.

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

4) Use source .bashrc about to take effect.

Example:

python@ubuntu:~$ pwd
/home/python
python@ubuntu:~$ vi .bashrc

// add two lines above
python@ubuntu:~$ source .bashrc

python@ubuntu:~$ mkvirtualenv -p python3 cp15
(cp15) python@ubuntu:~$ deactivate
python@ubuntu:~$ workon cp15
(cp15) python@ubuntu:~$ pip install django==1.8.2

 

 Creation and use of virtual environments

Create a virtual environment command: mkvirtualenv virtual environment name
Python3 create a virtual environment: mkvirtualenv - the p-CP15 python3
Enter the virtual environment to work: workon virtual environment name
How many virtual machine environment on View: workon space + two tab key
Exit the virtual environment: deactivate
To delete a virtual environment: rmvirtualenv virtual environment name


Virtual environment command to install the package: pip install the package name
Note: You can not use sudo pip install the package name, this command will install the package to the real host environment rather than installed into a virtual environment.
  APT - GET install software
  pip install python package name
Django installation environment: PIP install django == 1.8.2

View virtual environment in which to install python package:
  pip list
  pip freeze (and the list is that the difference will be less several packages, use the line when this command on the site)

Note, edit and create documents in a virtual environment, not only in the virtual environment, but also outside the virtual environment only to decide where to install those packages.
When you create a virtual environment, the virtual machine is networked.

 

Django projects and applications created

Project Creation

Create a project command:

django-admin startproject project name

Note: Create an application you must first enter the virtual environment.

 

And the role of the project directory as follows:

__init__ .py: Description test1 is a python package.
settings.py: project configuration file;
urls.py: url for routing configuration;
wsgi.py: Django inlet and a web server interaction;
manage.py: project management documents;

 

Application creation

A project consisting of a number of applications, each application to perform a specific function.
Creating an application command as follows:

python manage.py startapp application name

Note: You need to enter the project directory when creating applications.

Application Directory and role is as follows:

the __init__ .py: Contents module is a python;
models.py: write and database-related content;
views.py: receiving a request, a process, and to interact with the T M, returns a response;
Defining processing functions, view functions.
tests.py: write test code file;
admin.py: Site Admin relevant documents;

 

Establish the link between applications and projects, the need for the application to be registered.
INSTALLED_APPS modify configuration items in settings.py.

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    '应用名',
)

 

Running the development web server command:

python manage.py runserver

 

Django projects and applications to create the sample:

python@ubuntu:~$ workon cp15
(cp15) python@ubuntu:~$ mkdir day01
(cp15) python@ubuntu:~$ cd day01

(cp15) python@ubuntu:~/day01$ django-admin startproject test1
(CP15) python @ ubuntu: ~ / Day01 $ ls
test1
(cp15) python@ubuntu:~/day01$ cd test1/
(cp15) python@ubuntu:~/day01/test1$ tree
(cp15) python@ubuntu:~/day01/test1$ ls
manage.py test1

(cp15) python@ubuntu:~/day01/test1$ python manage.py startapp booktest
(cp15) python@ubuntu:~/day01/test1$ ls
booktest manage.py test1
(cp15) python@ubuntu:~/day01/test1$ cd booktest/
(cp15) python@ubuntu:~/day01/test1/booktest$ tree
.
├── admin.py
├── __init__.py
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
└── views.py

1 directory, 6 files

// use pycharm open the Home / Python / CP15 / test1, be registered in the application settings file
(cp15) python@ubuntu:~/day01/test1$ python manage.py runserver

After all development projects using the virtual environment, make changes to the project from the command line, you have to re-enter operation in a virtual environment.

 

Reproduced in: https: //www.cnblogs.com/yifchan/p/python-1-23.html

Guess you like

Origin blog.csdn.net/weixin_33695082/article/details/93651255