[Notes] Django Django Profile, environment building, blog recommendation

[Notes] Django Django profile, set up the environment, learning resources

table of Contents

1. Learning Resources

2.Django Profile

2.1. MVC / MVT Profile

2.2. Django Profile

3. environment to build

3.1. Virtual Environment

Virtual Environment:

Virtual operating environment package

3.2. Installation package Django

4. Test Django

4.1. Create a project in the command line


 

1. Learning Resources

django official website

Django documentation  official document (recommended)

Django source

Recommended blog: Django2.2 Tutorial

 

2.Django Profile

 

2.1. MVC / MVT Profile

MVC

MVC (Model-View-Controller) , i.e., the model (model) - view (view) - the controller (Controller), is a software design pattern, to the conventional input (INPUT), processing (Processing), output ( output) task to use graphical user interaction model and design. This idea of division of labor is the standing, widely used in software engineering, is a typical and widely used software architecture model.

Later, MVC idea is applied in the Web development with business logic, data, interface display method for the separation of the organization code, business logic gathered a component inside, while improving and customization interface and user interaction, not need to re-write the business logic. It is known as Web MVC framework. Popular to say that the code into different files, and the different types of files into different directories.

MVC framework of core ideas are: decoupling , so reduce the coupling between different code blocks of code to enhance the scalability and portability, for backward compatibility.

The current mainstream of development languages ​​such as Java, PHP, Python in both MVC framework.

  • M is a spelling Model, the main package access to the database layer, the data in the database to add, delete, change, search operation.
  • V spelling of View, for enclosing a result, content generated html page display.
  • C is a spelling Controller, for receiving a request, the business logic process, and to interact with Model View, return results.

MVT :

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

  • M spelling for the Model, and M functions in the same MVC is responsible for interacting with the database, data processing.
  • V spelling of View, and C functions in the same MVC, receives the request, performs service processing and returns a response.
  • Template T is spelling, and V in MVC same function, responsible html package structure to be returned.

2.2. Django Profile

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

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.

The latest version 3.0 release.

 

3. environment to build

Ubuntu, for example to set up the environment. Python3, pip3 IDE and install their own environment.

 

3.1. Virtual Environment

Generally , the development, when you need to use python packages can be networked installation

sudo pip3 install 包名称

Using the above command, automatically install the latest version Pypi provided , the package will be mounted to the lower /usr/local/lib/python3.5/dist-packages.

When installing the specified version followed by the version number to:

sudo pip3 install django==2.2

However, in the actual work you may be required on a single machine, while developing multiple projects to use different versions of the same package. Using the above command, installed in the same directory or updated, the new installation will overwrite the package on the installation of the package, other projects will not have to run. You can use the virtual environment to solve this problem.

 

Virtual Environment:

Virtual environment is actually a copy of the real pyhton environment , so that the copy of python environment, the installation package will not affect the real python environment. By creating multiple virtual environments, developed in a different virtual environment projects to achieve isolation between projects.

 

Create a virtual environment:

  • First install the virtual environment:
sudo pip3 install virtualenv #安装虚拟环境

 

  • Install virtual environments Expansion Pack:
sudo pip3 install virtualenvwrapper

Virtualenv which is a tool to create an independent python environment.

virtualenvwrapper a tool based on the virtualenv, it will all unified management of virtual environments.

The purpose of installing a virtual environment wrapper is to use a more simple commands to manage the virtual environment.

 

  • Modify the configuration file .bashrc in the user's home directory, add the following:
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

Use the command source .bashrc configuration file to take effect.

  • You can choose to use a python interpreter, create a virtual environment python3 command as follows (Both methods):
mkvirtualenv -p python路径 虚拟环境名称 # -p参数指定Python解释器程序路径
例:
mkvirtualenv -p /usr/bin/python3.5 py_django 

mkvirtualenv -p python3 虚拟环境名称
例:
mkvirtualenv -p python3 py_django

 

Note: to create a virtual environment in several formats

  1. name mkvirtualenv virtual environment    # create a clean environment
  2. mkvirtualenv --system-site-packages virtual environment name of     # this third-party modules will inherit your local environment of all installed
  3. mkvirtualenv -p Python interpreter program path name of the virtual environment   # -p parameter specifies the path to the Python interpreter program

 

  • summary
    • Once created, it will automatically work in this virtual environment.
    • Create a virtual environment requires networking.
    • Work in a virtual environment, the front will prompt "(virtual environment name)."
    • All of the virtual environment, are located in a hidden directory .virtualenvs home directory.

 

  • Exit the virtual environment:
deactivate
  • See all virtual environments:

Tip: workon two space + tab key

workon 两次tab键
  • Use the virtual environment:

Tip: Give the first part of the name, you can use the tab key padded

workon 虚拟环境名称
例:
workon py_django
  • To delete a virtual environment command as follows:
rmvirtualenv 虚拟环境名称
例:
先退出:deactivate
再删除:rmvirtualenv py_django

 

Virtual operating environment package

Operation can use the Command pip python package in a virtual environment, the installation command follows:

pip install 包名称

Note: Do not use sudo pip install the package name in the virtual environment to install python package, such as installation package is actually mounted on the actual host environment.

View command python package installed as follows:

pip list  
pip freeze

These two commands can already see which package python current virtual work environment is installed, but the format is slightly different.

 

3.2. Installation package Django

Press the above steps to create a virtual environment .

Then install the package django2.2 command is as follows:

pip install django==2.2

expand:

apt-get install software

pip install python package name

 

Verifying the Installation

Method 1: Use the pip listcommand to see if there Django modules.

Method 2: Enter the Python interactive environment (note be sure to enter just installed Django Python interpreter), according to the version shown below to view the installation:

>>> import django
>>> print(django.get_version())
2.2

 

note:

If the system is a win, after the successful installation of Django, python installation file in the Scripts folder path to find django-admin.exethe file, it can be added to the operating system environment variables, easy call.

 

4. Test Django

4.1. Create a project in the command line

New and enter the directory you want to create a Django project (eg: / home / python / pytest /), and then create a project in the directory:

Create project command is as follows :

django-admin startproject 项目名称
例:
django-admin startproject test1

This will create a Django project called test1 in the current directory.

└── test1
    ├── manage.py
    └── test1
        ├── __init__.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

 

You can help us see that Django automatically create a folder test1, which is the root directory of the project. In test1 root directory, there are a test1 directory, which is the configuration file directory of the project (and not to the root directory of the same name, confused), there is a manage.py file, the entire project management script.

In the project root directory , run python manage.py runserver, Django will 127.0.0.1:8000start the development server this default configuration. In the browser address bar 127.0.0.1:8000, if you see the following interface, indicating that Django everything is normal. Safe to use pycharm IDE and other open directory project development project.

 

 

You can also use Pycharm create a Django project in 2018 after Pycharm versions support while creating a virtual environment and Django project.

 

 

 

More detailed introductory tutorial can refer to: https://www.liujiangblog.com/blog/36/

 

-----end-----

 

 

Published 50 original articles · won praise 10 · views 6600

Guess you like

Origin blog.csdn.net/qq_23996069/article/details/104609428