1, Django a foundation

  Django basic 
basics:
1. What is a web application?
web is essentially a socket server, the user's browser is a socket client, based on b / s software development framework of c / s architecture application
browser Enter typing the URL sent a few things?
1. Browser the server sends a request over
2 receives the service request
3. the server returns a corresponding response
4. the browser receives the response page rendering display to the user according to specific rules to see
2, HTTP protocol provides mainly between the client and server communication format
3, what protocol is HTTP:
hypertext transfer protocol: specifies the format of the client and server message transmitted
four characteristics of http:
1, based on the request response
2, based on the role of TCP / IP over the application layer protocol
3. stateless (server can not save a user input state, a person can not remember to thousand times, are as strike)
4, no connection (primary response to a request, immediately after the disconnection, both after no longer have any relationship)
websocket the equivalent of a large patch of the HTTP protocol that supports long connection
request form:
request the first line: http version information, as well as customer request method, url and
request header: a lot of k, v key information (note below no less blank lines)

request body: post request data carried in
the response data format:
in response to the first line (http protocol version identifier, response status code)
in response header (lot k, v value pairs)

in response to the body (back to the browser data pages are usually response body HTML page
response status code:
a string of simple numbers to represent some complex state or message
1XX: the server has successfully received your data is being processed, you can also send additional data
2XX: request your server has been successfully sends the requested data to you
3XX: redirection
4XX: request error or no operating authority, or the content does not exist
5XX: internal server error
mode requests:
1, get request to obtain data to the server,
2, Port request, submit data over the server
URL: Uniform Resource Locator (URL is vernacular)

a: Web Framework
python web framework three major
1.Django:
Pros: Large while the full special function comes particularly, carriers similar
disadvantages: a bit bulky
2.Flask:
advantages: dapper, in particular less own function modules are all dependent on third-party components
particularly flask third frame assembly if the flask all the components add up to
completely cover over the entire Django
OK: comparative restricted to third-party developers
3.Tornado:
advantages: born asynchronous non-plug rent very fast frame can be withstood high concurrency can develop a game server

web framework can be divided three parts:
A: Socket
B: matching routing and view function
C: template grammar

Django:
A use of someone else's wsgiref (module file)
b write their own
c himself wrote
the Flask:
A use of someone else's Werkzeug
b write their own
c with someone else wrote jinja2
Tornado:
A, b, c are to write their own

ps: in the Notes prior to the introduction Django, that the use of Django Notes
1. the computer name can not have Chinese
2. a pycharm window is a project, not multiple projects in a window inside
3. project name can not play Chinese
  DJango process Figure:
  

 

 



Django versions: recommended 1.11.11 (is maintained)
After 18 years before the 2.0 version, LTS represents the maintenance version of
Download:
command-line direct download
pip3 install django which defaults to the latest version
pip3 install Django == 1.11.11
see if the download is successful:
Django-ADMIN
      

 

 

 


After successfully creating a project:
to create a project django way
Mode 1 (command line to create):
1. Create a project django
django-admin startproject project name
django-admin startproject mysite
project is equivalent to the following University College, here is just an empty shell,
so to create a project in their own application (app) that is subject own school, different for each application, its function is different
2, create an application (app) :( to switch to the project folder)
to switch to the project folder with: cd project name. For example: cd mysite
first method:
django startApp application name ADMIN-
django-admin startapp app01
second way:
Python manage.py startApp app01
. 3 django project start command
python manage.py runserver
The successful launch command-line will be a line (Starting Development AT http://127.0.0.1:8000/ Server)
PS: After a successful start, played in a django window, do not go from the other, did not change the situation in the port do not play at another port
if you want to play a window, the currently active window must stop, shut, and go from window to disable ctrl + z key to quit, vacate the port number to exit
pycharm create
ways 2 (pycharm creation)
FILE >>> new project select the second django need to pay attention to the name can not have Chinese, select a local interpreter, check Admin

 

 

 



Creating App
PyCharm command line to create
python3 manage.py startapp app01
Tools The following functions run manage task bar
to start a small green arrow
(************************************************************ *********)
Note: 1, django project using the command line created does not automatically create templates template folder
we need to manually create the file path and need to own settings.py file registration
      

 

 

 



2, the application must be created to register in settings.py file to take effect, otherwise it does not recognize the
d

 

 

 

jango document presents the main 
project file name:
           同名的项目文件夹:
settings.py 文件 django 暴露给用户的可配置文件
urls.py 文件 路由与视图函数对应的文件
wsgi.py 文件 是模块wsgiref的文件
manage.py文件 django 的入口文件
          应用文件(app):
migrations文件夹 数据库迁移记录文件
admin.py 文件 django后台管理
apps.py文件 应用注册相关
models.py 文件 orm模型类
tests.py 测试文件
views.py 视图函数文件
      

 

 

 


小白必会三板斧:######################
1, HttpResponse:返回字符串,你在里面写字符串,返回字符串相关的
HttpResponse('你好啊,我是你的第一个Django')
2,返回页面:
所有的页面html相关的都在templates里面写,在这个文件夹下创建HTML文件
render: 返回html页面 并且能够给该页面传值
3,redirect:重定向




强调:
1.用django一定要保证只有一个在运行状态 切记切记!!!!!!!
2.一定记得清浏览器的缓存

Guess you like

Origin www.cnblogs.com/Fzhiyuan/p/11515344.html