Sec understand Series | in the history of the most simple introductory tutorial Python Django

http://www.cnblogs.com/baiboy/p/django1.html

Summary: Django's tutorial is categorized, different forms. Or more official document system, or free style blog document, or tend to parse the document instance. Even official documents, chapters are more elaborate text cumbersome, and sometimes we just focus on a feature usage only, but the biggest problem free Bowen is copying each other, confusing structure, covering facet and errors are obvious. Thus, this paper sort data and project development experience during their studies, finishing up a more common and practical articles.

 

It applies to

 

  1. Getting Started, both C #, C, java, Python, R, etc. have any programming language can be the foundation;

  2. I would like to quickly understand and Django developers get started quickly by.

  3. Suitable as data query, technical point of reference.

Some basic concepts

 

Preconditions: assumes a basic Python language-based, or have the basis for a programming language. You also familiar with web development environment, understand some css, js, db and so on.

 

What Django that?

 

Django Web application framework is an open source, written in Python. Software using MVC design pattern, i.e. the model M, and a controller C. view V It was originally developed to manage Lawrence Publishing Group's news to some of the content-based sites. It was released in July 2005 under the BSD license. This framework is based on the Belgian gypsy jazz guitarist Django Reinhardt named. Django's main goal is to make the development of complex, database-driven Web site easier. Django focus on reusability and "pluggable" components, agile development and law DRY (Do not Repeat Yourself). It is commonly used in Django Python, even including configuration files and data models.

----Wikipedia

 

Django Web application framework is an open source, written in Python. MVC pattern using the frame, i.e. the model M, and a controller C. view V It was originally developed to manage Lawrence Publishing Group's news to some of the content-based website that is CMS (Content Management System) software. It was released in July 2005 under the BSD license. This framework is based on the Belgian gypsy jazz guitarist Django Reinhardt named.

----Baidu Encyclopedia

 

MTV development model?

 

Django is a framework based on MVC structure. However, in Django, the controller accepts user input portion discretion of the frame, so there is more concerned Django model (the Model), the template (Template) and view (the Views), called MTV mode. Their respective responsibilities are as follows:

 

  1. All transaction model (Model), that is, data processing and data access layer related to: how to access, how to verify the validity of the relationship between behavior and what data is contained and so on.

  2. View (View), namely the presentation layer processing performance-related decisions: how to display the page or other types of documents.

  3. Template (Template), i.e. business logic and access the associated logic model retrieval of appropriate templates. Bridge model and template.

Django's architecture?

 

Let us glance Django whole picture:

 

urls.py inlet URL, corresponding to a function associated with the views.py (or generic class), access URL corresponding to a function.

 

views.py processing request issued by the user, from the corresponding urls.py over by the page rendering templates can display the content such as the login user name, user requested data, is output to the page.

 

models.py related to database operations, use this time to read data stored or, of course, when not using the database you can not use.

 

forms.py form, the user enters the data submitted in the browser, as well as verification of data generated input box and so on, of course, you can not use.

 

Function templates folder views.py in rendering templates in Html template, get dynamic content pages, can of course use caching to improve speed.

 

admin.py the background, you can use a small amount of code has a strong background.

 

settings.py Django setup, configuration files, such DEBUG switch, static files location.

 

The above py file does not understand it does not matter, will be detailed later. A picture is worth a thousand words, architecture picture of the working mechanism is as follows:

Django commercial sites everyone used

 

Sohu mailbox, shell network, watercress, love research, and easy of online cloud office, Yourong network, fast game play, ninety room, loans to help network, net interest odd, know almost, stylish and space, travel hee board: YxPad webpy, DNSPod international version, the kitchen, Betty's kitchen, Wopus questions and answers, plump net, scallop network, webmaster tools, easy of document management systems, personal rent, online document viewing - Easy degree cloud view, FIFA310 football data analysis expert, Sohu carry look like.

Django Prerequisites

 

Preconditions: pip, python, sublime, anaconda environment is installed.

 

Kisaki置 conditions:

 

Check whether the installation was successful:

 

xxx statistical analysis (decision) platform

 

System Environment: WIN10 64bit

Development Environment: sublime + Anaconda

Database: Mysql 5.6.17

Language: python3.5

Framework: django1.11 + Bootstrap

Visualization tools: Highchart | Echarts | plotly | Bokeh (using Echarts)

Django Basic Configuration Installation

 

1 Create a project, the project decision analysis: xmjc_analysis

 

Xmjc_analysis created in the root directory of E:

 

effect:

 

settings.py project settings file

urls.py total urls profile

wsgi.py deploy file servers

The directory structure __init__.py python package of necessary and relevant calls.

 

2 Create App called analysis

 

effect:

3 app settings.py new definition is added in the INSTALL_APPS

4 views, and web site to create the first page

 

(1) In our analysis this directory, the views.py open, modify the source code:

The first line is declared encoded as utf-8, because we use the Chinese in the code, if you do not declare on the error.

 

第二行引入HttpResponse,它是用来向网页返回内容的,就像Python中的 print 一样,只不过 HttpResponse 是把内容显示到网页上。

 

我们定义了一个index()函数,第一个参数必须是 request,与网页发来的请求有关,request 变量里面包含get或post的内容。

 

(2)我们打开 xmjc_analysis/xmjc_analysis/urls.py 这个文件, 修改其中的代码:

(3)本地运行服务器测试

 

注意在项目根目录xmjc_analysis运行结果如下:

(4)页面传参数,显示欢迎‘admin’字样

 

修改view.py源码:

通过get方式接受页面参数,当然也可以采用post,结合form实现。

 

5 配置简单数据库操作,默认sqlite,咱们指定mysql数据库

 

(1)在xmjc_analysis/settings.py文件下修改如下:

xmjc_analysis/__init__.py下修改:

 

(2)在analysis/models.py下设计数据库表,采用ORM方式

(3) 在analysis/admin.py中定义显示数据

 

(4)创建更改的文件,将生成的py文件应用到数据库

(5)创建超级管理员:用户名,test;密码密码:test123456

(6)登录后台查看信息

 

运行服务器:python manage.py runserver

可以看到后台信息,并对数据表进行增删改查操作,但是后台全部英文,可以改为中文显示?

 

后台管理设置为中文显示,xmjc_analysis/settings.py下修改代码

 

再去查看:

(7) Django 提供的 QuerySet API,shell玩转MySql

 

在xmjc_analysis下输入【 python manage.py shell】,然后查询数据表。

创建一条用户信息:

 

后台查看:

其他操作方式:

(8)批量向数据表导入数据

 

将name.txt导入数据库:

数据导入源码:

查看结果:

>> 至此,基本熟悉上手了。深入学习待续...

— End —

Guess you like

Origin www.cnblogs.com/Ryan-Yuan/p/11545005.html