To know before Django

 

What is a web application?

  More simply, is to visit a Web site through a browser, the site retrieval of data from the background, and then display the appropriate interface to a procedure such user.

 

What is the HTTP protocol?

  Hypertext Transfer Protocol: specifies the format of the client and the server message transmission

 

  Four characteristics:

    1, TCP / IP protocol-based action in the application layer protocol

    2, based on the request response (primary response corresponding to a request)

    3, no state (not hold client, every treat you as strike)

    4, no link (HTTP request will be accepted immediately after a disconnect, but not disconnected TCP)

Extended: for there is no link to reverse a websoceke technology, which is used to maintain constant links.

 

  Request data format:

    The first line request

    Request header (a bunch of k, v key-value pairs)

          (Here is a space, not representing no)

    Request body (post data carried in the request) (the GET request carrying parameters in the URL after a question mark spaced from, the first data is insecure, and the second is the size of the transmission is also limited)

  In response to the data format:

    The first line of response

    Response header (pile k, v key-value pairs)

            (Space)

    Response body (post data carried in the request)

 

  Response status code:

    1xx server has successfully received your data is being processed, you can continue to submit additional data

    2xx request succeeds the server has to send you the requested data to you

    3xx Redirection

    4xx requested resource does not exist

    5xx Server Error

 

  Static and dynamic pages

    Static pages:

      Data on the page is written dead, the same years.

    dynamic webpages:

      Data on the page is retrieved from the back-end dynamic

      For example, the back end to get the current time

      Back-end database to obtain the data is then passed to the front page

 

  Template Rendering

    Generated by backend data directly to the front page used (and the front page may be flexibly operate the data) template syntax >>>

    Rendering template template syntax need to rely on third-party modules

    pip install jinja2

 

    Jinja2 support front-end template syntax directly using syntax similar to Python's operating data

    <P> {{user_dic}} </ p> remember two braces

    <p>{{ user_dic.name }}</p>
    <p>{{ user_dic['password'] }}</p>
    <p>{{ user_dic.get('name') }}</p>

    

    Values ​​generally, it can be traversed, then added to the front-end interface list

    {% For user in user_dict%} is here taken to form the dictionary list which sets [{}, {}, {}, {}]
      <TR>
        <TD> the user.id {{}} </ TD>
        < TD> the user.name {{}} </ TD>
        <TD> user.password {{}} </ TD>
      </ TR>
    {%} endfor%

 

web frame flowchart Lite

  

What is a web framework?

Currently the three main Python web framework

  Django: large and comes with a lot of functional modules, equivalent to the web framework world aircraft carrier (Cons: too big to result in more content of the document)

   Flask: dapper, less built-in function modules, most of them rely on a third-party modules, but because streamline lead content easy to understand

  Tornado: asynchronous non-blocking treatment is mainly used in the case of high IO multiplexing is generally used more game backend

 

functional web frame divided into three parts

a: socket (service)

b: Routing and view function  

c: template rendering

 

Django:

  a:用别人的  wsgiref

  b:自己写的

  c:自己写的

Flask:

  a:用别人的  werkzeug

  b:自己写的

  c:用别人的  jinja2

Tornado:

  a,b,c 都是自己写的

 

注意,在运行Django的时候:

  1、计算机的名称不能有中文

  2、一个pycharm窗口就是一个项目,不要多个项目放在一个窗口里面

  3、项目名不能起中文

 

 Django简介

  一般下载1.xx.xx版本

   命令行下载:pip3 install Django == 1.11.11

  验证是否下载成功

  Django - admin

 

创建Django项目的方式

  方式一(命令行创建):

    创建Django项目

      Django-admin startproject  

    创建app应用

      Python3  manage.py  startapp  app01

    启动Django项目

      python3  manage.py  runserver

    注意:用命令行创建Django默认不会自动创建templates(用来存放所有的HTML文件的地方)文件夹

          需要自己手动创建(在settings中查看该文件夹是否加入了环境变量中,没有要记得手动添加)

 

   方式二(pycharm创建):

     file>new project 选择Django 注意:1、名字不能有中文   2、选择本地解释器  3、勾选后台管理

    创建app

      pycharm命令行创建

        python3  manage.py  startapp  app01

      Tools  下面 run manage task 功能栏  省略了python3  manage.py 直接输入startapp即可创建

     强调:

      1、用django 一定要保证只有一个在运行状态

      2、记得清理浏览器的缓存

 

app(应用)的概念

    一个django项目就像是一所大学

    而app就是大学里面的学院

注意:创建新的app之后,要在配置文件中注册才能生效

 INSTALLED_APPS = [

  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'app01.apps.App01Config'    # 可以用全称
  'app01'            # 也可以简写

 ]

 django各个文件的作用

 应用名

    migrations  数据库迁移记录相关数据

    admin.py     django 后台管理相关

    models.py      模型表相关

    views.py    视图函数相关

项目名

    settings.py   配置文件

    urls.py    路由与视图函数的映射关系

templates

    项目用到的所有HTML文件

 manage.py

    django 入口文件

 

 django 入门必会三个技术点

from  django.shortcuts  import render,HttpResponse,redirect

 

HttpResponse  返回字符串

render       返回一个html页面

    两种给前端页面传值的方式

      def reg(request):
        user_dict = {'name':'jason','password':'123'}
        return render(request,'reg.html',{'user_dict':user_dict})

      def reg(request):
        user_dict = {'name':'jason','password':'123'}
        return render(request,'reg.html',locals())    (推荐使用)

 redirect    重定向(跳转到其他指定URL或页面等)

 

django识别到代码变化之后会自动刷新,但是有时候反应速度比较慢

可以手动重启,也可以手动刷新浏览器

 

Guess you like

Origin www.cnblogs.com/SlookUp/p/10981231.html