Django base 02

You may request a complete process:

  0. start the server, waiting for the client (user's browser) to connect
  1. Establish a connection, the browser sends a request in the browser address bar enter the URL, and the server
  2. The server receives the request message, parses the request message, the correspondence relationship between the path and the function, the function to be executed to find
  3. execute the function, open the HTML file, the replacement string, to give a final HTML content to be returned
  4. the message format of claim HTTP protocol, the HTML content to a reply the user's browser (response transmission)
  5. after the browser receives a response message, in accordance with the rules of rendering HTML page.
  6. Close connector

Django installation:

  pip3 install django==1.11.11

  pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ django==1.11.11
  1. Django project started:

  1. Start the command line
    in the root directory of the project (that is, there manage.py of that directory), run:

      python3 manage.py runserver IP:端口 #--> 在指定的IP和端口启动
      python3 manage.py runserver 端口 #--> 在指定的端口启动
      python3 manage.py runserver #--> 默认在本机的8000端口启动

  2. PyCharm start
    a small green triangle, you can directly start the Django project (provided that the small triangle on the left is your Django project name)

  1. Configuration files related to the project name /settings.py
      1. Templates (HTML storage configuration file) <- I tell Django where to find the HTML file

  2. static files (css / js / images)
    # static file directory alias
    STATIC_URL = '/ static /'

    # All static files (css / js / images) are placed me under your configuration folder
    STATICFILES_DIRS = [
      os.path.join (base_dir, "static"),
    ]

  1. Comment out setting.py in the row with the csrf (about 45 to 47 lines)

Submit the form data form back end needs attention

  1. not form from, obtain user input for all tags should be placed inside the form, and must have a name attribute
  2. action control where to submit property, method generally arranged POST
  3. must submit button type = submit, It can not be other types

  1. GET requests and POST requests
      : GET request
      1. The browser requests a page
      when a keyword search engines 2.
http://www.baidu.com/s?wf=素还真

POST request:
  1. browser to submit data to the server, such as login / registration
  django get POST form values

  email = request.POST.get('email',None)
  pwd = request.POST.get('pwd',None)
  1. In Django APP:
      What is the APP and APP Why??

  project -> Project (Tsinghua School)

  APP -> Application (College of Economics and Management / Academy of Fine Arts)

To help us in a big Django project management to achieve different business functions.

  1. Create APP command
  . Command, in the root directory of your Django project inputs:

  python3 manage.py startapp app名字
  1. SNAKE
import pymysql
pymysql.connect(
...
...
)
  1. Different programmers to write SQL uneven level
  2. Efficiency is also uneven

python syntax - automatic translation -> SQL statements

jQuery DOM
$("#d1") --自动翻译--> document.getElementById("d1")

ORM:
Advantages:

  1. Simple, do not write your own SQL statements
  2. Development of high efficiency
    Disadvantages:
  3. You remember this special syntax
  4. With respect to some of the great God SQL statements, certainly the efficiency gap

ORM correspondence relationship of:
Class ---> Data table
objects ---> rows
Properties ---> field

ORM can do thing:

  1. Operating Data Table -> Create Table / delete tables / Modify table
    operation models.py inside the class

  2. Operation data line -> data CRUD

You can not create a database, create the database yourself

Use Django's ORM detailed steps:

  1. DIY Create a database
    create database database name;
  2. Set configuration database connections in Django project (tell Django which database connection)

    Database-related configuration

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # 连接的数据库类型
'HOST': '127.0.0.1', # 连接数据库的地址
'PORT': 3306, # 端口
'NAME': "day61", # 数据库名称
'USER': 'root', # 用户
'PASSWORD': '123456' # 密码
}
}
  1. Tell Django connection with pymysql MySQL database instead of the default MySQLDB
    project / the init .py file, write the following two sentences:
import pymysql
# 告诉Django用pymysql来代替默认的MySQLdb
pymysql.install_as_MySQLdb()
  1. The definition of a class in the following models.py app file, this class must inherit models.Model
class 类名(models.Model):
...
  1. Execute two commands
python3 manage.py makemigrations
python3 manage.py migrate

Increase ORM and single-table query:

  1. Inquire
models.UserInfo.objects.all()
  1. increase
models.UserInfo.objects.create(name="张三")

Guess you like

Origin www.cnblogs.com/anyux/p/11921995.html