Python Web framework Django project construction process, pythondjango

Source: Internet
Author: User
Tags install django

Python Web framework Django project construction process, pythondjango

Python Web framework Django Project Construction 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 use pip freeze to view installed library version information.

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

Django features

  1. Powerful database functions
  2. With python class inheritance, several lines of code can have a rich and dynamic database operation interface (API), and you can execute SQL statements as needed.
  3. Powerful background Functions
  4. A few lines of simple code give your website a powerful background and easy management of your content! Elegant website
  5. Use the regular expression to match the URL, pass it to the corresponding function, and define it as you want!
  6. Template system-a powerful and scalable template system with simple design, code and styles separated for easy management.
  7. Cache System-used with memcached or other cache systems for better performance and faster loading speed.
  8. Internationalization-fully supports multilingual applications, allowing you to define translated characters and easily translate them into different languages.

The installation of Python and MySQL is not described here. You can find and solve it on the network. Install django and pymysql using the pip install * command.

After installing Django, you can use the django-admin.py management tool to create a project. First let's take a look at the command introduction to the django-admin.py, and enter the django-admin.py in the command line to view available project management commands.

 

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

Step 1: select the project storage directory before setting up the Django project. Switch to the project storage directory in the Dos window CD.

Step 2: Create a project execute the django-admin.py startproject HelloWorld

Open IDEA and you can see the created project directory as shown in:

 

Directory description:

  1. HelloWorld: The project container.
  2. Manage. py: A Practical command line tool that allows you to interact with this Django project in various ways.
  3. HelloWorld/init. py: An empty file that tells Python that the directory is a Python package.
  4. HelloWorld/settings. py: Set/configure the Django project.
  5. HelloWorld/urls. py: The Django project URL Declaration; a Django-driven website "directory ".
  6. HelloWorld/wsgi. py: a wsgi-compatible Web server portal to run your project.

Next, enter 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 allows other computers to connect to the development server. 8000 is the port number. If not specified, the default port number is 8000.

Enter the ip address and port number of your server in the browser. If the server is started properly, the output result is as follows:

 

Step 3: Create an application enter the django-admin.py startapp demo in the command line

Open IDEA and you can see the created project directory as shown in:

 

Directory description:

  1. Demo: Application container. Note: After the page design file is created in this directory, the file named xx.html is saved here.
  2. Init. py: The above init. py file
  3. Migrations: database-related directories. Data classes will appear after the database is synchronized.
  4. Admin. py: admin files
  5. Apps. py: app application file management
  6. Models. py: describes a data table using a Python class called a model ). Using this class, you can use simple Python code to create, retrieve, update, and delete records in the database without having to write one SQL statement after another.
  7. Tests. py: Test File
  8. Views. py: contains the business logic of the page.

Create a super Administrator

Python manage. py createsuperuser # enter the user name and password as prompted. Leave the mailbox blank. the user name and password are required. # You can use python manage. py changepassword username to modify the user password.

Appendix

Server Response to client request process

The flowchart is as follows:

 

The flowchart above can roughly describe the process of Django processing the request. According to the annotation in flowchart 2, it can be divided into the following steps:

1. the user requests a page through a browser.

2. When a Request arrives at the request Middlewares, middleware processes the Request or directly responds to the request.

3. URLConf uses the urls. py file and the requested URL to find the corresponding View.

4. View Middlewares is accessed. It can also process the request or directly return response.

5. Call the function in View.

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

7. All Model-to-DB interactions are completed through manager.

8. If needed, Views can use a special Context.

9. Context is passed to the Template to generate a page.

A. The Template uses Filters and Tags to render the 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: Return to the browser and present it to the user

Url () function

Django url () can receive four parameters, which are two required parameters: regex, view, and kwargs and name. The following describes these four parameters in detail.

  1. Regex: regular expression. The matching URL executes the second view parameter.
  2. View: used to execute URL requests that match the regular expression.
  3. Kwargs: The dictionary type parameter used by the view.
  4. Name: used to obtain the URL in reverse order.

Django project deployment

In the previous introduction, we used python manage. py runserver to run the server. This is only applicable to test environments.
For officially released services, we need a stable and continuous server, such as apache, Nginx, and lighttpd. This article will take Nginx as an example later.

Set your own IP address to access the project

1. First Run> manage. py runserver 0.0.0.0: 8000.

2. Add ALLOWED_HOSTS = "*" in setting. py "*".

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.