The whole process of building a Django project in Python

Source: Internet
Author: User
This article mainly introduces the Python web framework Django project to build the whole process, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.

Python's web framework Django project build whole process

IDE Description:

    1. Win7 system

    2. python:3.5

    3. django:1.10

    4. pymysql:0.7.10

    5. mysql:5.5

Note: You can view the installed library version information through PIP freeze.

Django is a free open source website framework developed by Python that can be used to quickly build high-performance, elegant websites!

Django Features

    1. Powerful Database capabilities

    2. With Python's class inheritance, a few lines of code can have a rich, dynamic database manipulation interface (API), and you can execute SQL statements if needed.

    3. Comes with powerful back-Office features

    4. A few simple lines of code let your site have a powerful background, easy to manage your content! An elegant website

    5. Use regular match URLs, pass them to the corresponding functions, and define them as you like!

    6. Template System – A powerful, easy-to-extend template system designed for ease of design, code, style, and easier to manage.

    7. Cache system – Used with memcached or other cache systems for better performance and faster load times.

    8. Internationalization – fully supports multi-lingual applications, allowing you to define translated characters and easily translate them into different countries ' languages.

The installation of the Python,mysql is no longer elaborated here, you can find the solution on the network itself. Where the Django,pymysql installation is done using the PIP Install * command.

Once Django is installed, you can use the DJANGO-ADMIN.PY management tool to create a project. First, let's take a look at django-admin.py's command introduction, and at the command line enter django-admin.py to see the available project management commands.


The specific process for creating a HelloWorld project for a Django project is as follows:

STEP1: Before building a Django project, first select the project storage directory. Then switch to the project storage directory in the DOS window CD.

STEP2: Create project execution django-admin.py Startproject HelloWorld

Open idea and see the project directory you created as shown:

Catalogue Description:

    1. HelloWorld: The container for the project.

    2. manage.py: A useful command-line tool that allows you to interact with the Django project in a variety of ways.

    3. helloworld/init.py: An empty file that tells Python that the directory is a python package.

    4. helloworld/settings.py: Setup/configuration for this Django project.

    5. helloworld/urls.py: URL declaration for the Django project; A "directory" of Django-driven Web sites.

    6. helloworld/wsgi.py: A WSGI-compatible WEB server's portal to run your project.

Next we go into the HelloWorld directory and enter the following command to start the server:

Python manage.py runserver 0.0.0.0:8000

0.0.0.0 allow other computers to connect to the development server, 8000 is the port number. If not stated, the port number defaults to 8000.

In the browser input your server's IP and port number, if normal startup, the output is as follows:


STEP3: Create app at command line input django-admin.py Startapp demo

Open idea and see the project directory you created as shown:


Catalogue Description:

    1. Demo: The application's container. Note: The following page design file, created in this directory templates, the file named Xx.html is placed here.

    2. init.py: such as the previous init.py file

    3. Migrations: Database related directories, data classes will appear after synchronizing the database.

    4. Admin.py:admin Background Management files

    5. Apps.py:app Application Management Files

    6. models.py: The main use of a Python class to describe a data table, called model. Using this class, you can create, retrieve, update, and delete records in a database with simple Python code without having to write one or two SQL statements.

    7. tests.py: Test File

    8. views.py: Contains the business logic for the page.

Create a Super Administrator

Python manage.py createsuperuser# Follow the prompts to enter the user name and the corresponding password is OK The mailbox can be left blank, user name and password required # Modify user password can be used: Python manage.py ChangePassword Username

Report

The service side responds to the client request process

The flowchart is as follows:


The flowchart above can roughly describe the process of Django processing request, according to Flowchart 2, can be divided into the following steps:

1. The user requests a page through the browser.

2. Requests arrive at request Middlewares, the middleware does some preprocessing to the request or response requests directly.

3.URLConf find the corresponding view via the urls.py file and the requested URL.

4.View middlewares is accessed, it can also do some processing of request or directly return to response.

5. Call the function in view.

The methods in 6.View can selectively access the underlying data through models.

7. All model-to-db interactions are done through the manager.

8. If necessary, views can use a special context.

9.Context is passed to the template to generate the page.

A.template using filters and tags to render output

B. The output is returned to the view

C.httpresponse is sent to response middlewares

D. Any response middlewares can enrich response or return a completely different response

E.response returned to the browser, presented to the user

URL () function

The Django URL () can receive four parameters, which are two required parameters: Regex, view, and two optional parameters: Kwargs, name, followed by the four parameters in detail.

    1. Regex: A regular expression that matches a URL that executes the corresponding second parameter view.

    2. View: Used to perform a URL request that matches a regular expression.

    3. Kwargs: The parameter of the dictionary type used by the view.

    4. Name: Used to reverse get the URL.

Django Project Deployment

In the previous introduction we used the Python manage.py runserver to run the server. This is used only in test environments.
The official release of the service, we need a stable and continuous server, such as Apache, Nginx, LIGHTTPD, etc., this article will take Nginx as an example.

Set up to access items with your own IP address

1. The first step is to execute the >manage.py runserver 0.0.0.0:8000.

2. You need to add allowed_hosts= "*" Inside the setting.py.

"Recommended"

1. Python Free video tutorial

2. Python Learning Manual

3. Marco Education Python Basic grammar full explanation video

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.