day 56 Django Introduction

HTTP protocol
Hypertext Transfer Protocol
1. Four characteristic
1) acting on the TCP / IP based application layer
2) based on a request response
3) the session token ... stateless Cookie
. 4) a long connection connectionless --- WebSocket (HTTP large patch protocol)

2. data format
# request format
request the first line (request method, protocol version ...)
request header (a lot of k: v key-value pairs)
\ r \ the n-
request body (the actual data sent post when the request only if it is no get request)

# response format
in response to the first line
in response to the first
\ r \ n
response body


3. the response status codes --- represent some meaning in particular digital
1XX: the server has successfully received the your data is being processed you can continue to submit additional data
2XX: server successful response (200 requests successfully)
3XX: redirection
4XX: request error (404 requests a resource does not exist 403 access Denied)
5XX: internal server error (500)

# Request method
get request
toward the data to other people
post requests
submitted data (eg: user login) to others

#url uniform resource locator


hand line and web frameworks

# Based wsgiref module

 1) Manual writing Socket
 2) http format data manually process

the module implements the above two manual process
according to the function of the different split into different files py
urls.py only put routing and view function correspondence relationship
views.py view discharge function (functions, classes)
after the split is completed if you want to add functionality, you only need the top two places in the hands and feet can be a

# dynamic and static pages
static pages
written died the same years the data is
dynamic web
data is real-time access the
EG:
1. Get the current time shows the rear end to the front end
2. the front end rear acquired display data in the database to

Question:
How to get to the back-end data transfer to the html page
data acquired is transferred to the rear end of the html page >>>: render template

# jinja2 module  
pip3 install jinja2

template syntax (grammar extremely close to the python backend)
<the p-> {{ }} User </ P>
<P> the user.name {{}} </ P>
<P> {{User [ 'pwd']}} </ P>
<P> {{user.get ( 'Hobby' )}} </ P>


{%} for user_dict in user_list%
<TR>
<TD> user_dict.id {{}} </ TD>
<TD> user_dict.name {{}} </ TD>
<TD> { user_dict.pwd}} {</ TD>
</ TR>
{%} endfor%

 


Three major python web framework
#Django:
large and comes with special features like a particularly large aircraft carriers
sometimes too heavy

#Flask
small but comes with features similar to the Rangers in particular are extremely rare
third-party modules particularly special, If the third-party modules flask can all add up to more than django
more dependent on third-party modules

#Tornado
non-blocking asynchronous
regressed to develop the game server


A: socket part
B: corresponding relationship between the routing and view function
C: template syntax

the django:
A with others wsgiref
B wrote it myself
C himself wrote
the Flask
a using someone else's werkzeug (based wsgiref)
B wrote it myself
C with others jinja2
Tornado
three full write your own

 

Use Django Notes
1. The name of the computer can not have Chinese
2. A pycharm window is a project
3. Project name inside try not to use Chinese

#django version of the problem
1.X 2.X market now used more or 1.X
is recommended that you use ~ 1.11.13 1.11.9

#django installation
PIP3 install django 1.11.11 ==

# django how to verify whether the installation was successful
command line direct knock django-admin

 

How to understand in the # django app

A django project is similar to a university, while the app is similar to the inside of the University
django is actually used for one application of
a stand-alone app is equivalent to a function

For example:
User Function
management function


django support as many app

 

 

How to use Django
# use the command line
to create a project django
django-admin startproject mysite
start django project
python manage.py runserver
create applications App
Python manage.py startapp app01

 

#pycharm use

When you start django project must make sure that only one port of a django project


# Note:
1. App you need to go to the newly created profile settings registered
pycharm will help you register a first application when you create a project written
2. Use the command line to create a django project will not automatically help you create templates folders
can only create their own
3.settings file you need to manually write the configuration TEMPLATES
os.path.join (base_dir, 'Templates')

#Django in the file directory
project name
the same name with the project folder
settings.py exposed to user's profile
urls.py routing and view function correspondence between the
application name
migrations in folders database migration records
admin.py django Admin
apps.py registration related
models.py model class
tests.py test file
views.py store view function (business logic)
Templates html files in folders
manage.py django file entry

 

Guess you like

Origin www.cnblogs.com/wwei4332/p/11700424.html
#56