Application of Python+django Framework (I.)

Source: Internet
Author: User
Tags install django

Django Introduction:

Django is an open source framework developed in the Python language, released in 2005. Early in the news and Content management site, providing a very powerful post-management system.

Django Official website: https://www.djangoproject.com

Frame mode:

Djando Frame Mode-MTV:

M (Models)-------> Model layer: Responsible for database modeling and CRUD (increase and deletion) operation;

T (Templates)--------> Template layer: Used to process user-displayed content, such as HTML;

V (views)--------> View layer: Handles some of the actions that interact with the user, gets the data from the model, sends the data to the template, and displays it to the user.

PS:MTV is a framework model of Django, and there is a more famous model--MVC besides MTV;

The functions of MTV and MVC do a simple correspondence, as follows:

M-------M (Models): Model layer

T-------V (views): View Layer

V------C (Controllers): Control layer

Here's an MTV-mode frame chart:

    

Django Installation

Ps:ubuntu system

First, check to see if Django is installed,

Enter Python3 interactive mode in the terminal, import Django; If successful, enter a command to view the version

    Django. VERSION

Second, start the installation

1, online installation

      sudo pip3 install Django (the highest version installed by default)

      sudo pip3 install django==1.11.8 (specifies that the installation version is 1.11.8)

PS: Here according to their own needs

2. Offline installation

1). Download the required Django installation package first to the official website

2). Unpack the Django package in the environment

TAR-XVF django-1.11.8.tar.gz

3). Go to the extracted directory for installation

        sudo python3 setup Install

 

The simple use of Django

To create a Django project, enter the following command in the terminal:

Django-admin Startproject Project Name

Example: Django-admin startproject mytext

Second, start the service and visit the website

Use the manage.py file to start the service as follows:

Python3 manage.py Runserver

PS: You can also use the./manage.py runserver to start, but if you are using Python3, you need to go to the manage.py file and change the first line to #!/usr/bin/env Python3(you don't have to change it)

Structure of the Djanogo project

First, manage.py----> is responsible for executing the various operation files in Django:

Such as:

Start a service, create an administrator, create an app, synchronize operations on a database, and more ....

Second, the main directory (name and project name)

1, __init__.py

The initialization file for the project, which is automatically executed when the service starts.

2, urls.py

The project's underlying URL configuration file

3, wsgi.py

Application Server configuration file

4, settings.py

Project Pig Setup file, the following describes the meanings of some of the important variables in the settings.py file:

Base_dir: Gets the root directory path of the current project

Debug: Debug mode, recommended to use true in the development process, the recommended is false on-line operation

Allowed_hosts: Sets the list of addresses allowed to access this item, default is empty, indicates only local, * represents any address

Installed_apps: Specify an installed app, and if you have an app you've created, you need to register it here

Middleware: specifying the registered middleware

Root_urlconf: Specify the underlying routing profile for the project

TEMPLATES: Specifying information for a template

DATABASES: Specifying information for the database

Language_code: Specify the language of the site display, Chinese (zh-hans)

Time_zone: Specify time zone, China time Zone (Asia/shanghai)

Use of URLs

1. urls.py (file)

The default is in the home directory, the primary routing profile, which contains the most basic address mappings; After each request arrives

will be matched by the URL of the Urlpatterns list in the urls.py file, and after the URL () function matches, it may be given to other

urls.py file or view (views) processing.

2. URL () function

Syntax: URL (regex, views, Kwargs=none, Name=none)

Regex: Regular expression, matching URL of request

Views: View processing functions or other urls.py files

Kwargs: A dictionary used to pass arguments to views

Name: A string that aliases the URL.

      

3. Pass the URL to the views

1). Using regular expression to pass the parameter
Using child group parameters, a subgroup is a parameter, you can use multiple subgroups if you want multiple arguments
Sub-group-()
Urlpatterns = [
When #访问路径是run/, make a run_views to handle it.
URL (r ' ^run/$ ', run_views),
#访问路径是run/Any two-digit number, give run_args_views to handle
URL (r ' ^run/(\d{2}) ', run_args_views),
#访问路径是run/four digits/two digits/, given to run1_views processing
URL (r ' ^run/(\d{4})/(\d{2})/$ ', run1_views),
]
Ps:
1. In the URL (), a subgroup () represents a parameter
2, in the views.py, the corresponding processing function according to the URL () number of sub-groups, the corresponding definition of parameters, the definition of parameters to be located after the request
Example:
1, url (r ' ^run/(\d{2}) ', run1_views),

def run1_views (Request,num)
2, url (r ' ^run/(\d{2})/(\d{4}) ', run2_views)

def run2_views (request,num1,num2)

4, use the URL () the third parameter, the dictionary to pass the parameter
URL (r ' ^show/$ ', show_views,{' name ': ' Laoshe ', ' age ': ' 89 '})

def show_views (request,name,age):
Name: Represents the value of the dictionary name parameter
Age: Represents the value of the Dictionary age parameter
Ps:
1. In the view handler function, the parameter must be declared
2. The name and location of the parameter must be consistent with the name and location in the dictionary

   

    

Application of Python+django Framework (I.)

Related Article

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.