1, app newly created must be registered to go to settings.py
Abbreviation: 'app01'
Complete: 'app01.apps.App01Config'
2, time to start Django project, it must ensure that a port number is only a Django project occupancy, otherwise, would be likely to cause the bug (after modifying the code to refresh the page has no effect)
3, users can access the resources, both in the url, only to open the url you can access relevant resources to
4, the back-end resources are generally required to manually specify whether exposed to the user
5, Django is supported by default automatically reset code it, so you just need to refresh the page a few times you can, but sometimes it's restart mechanism is relatively slow, it is generally manually restart Django
Restart mechanism: real-time monitoring file changes in the code, as long as there is a change, it will automatically reboot, your code may still not finished, this time will automatically error
6, form is the default form get request, carried in the way data is the url /? Name = & pwd = xxx xxx, http://127.0.0.1:8000/login/?username=zekai&password=123
After the post can be changed by a request method, the need to post requests to a middleware settings file commented
MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', # 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]
form form submission data destination have control action:
1. The case is not written, the current default network address submitted
2. You can also write suffix / index / (used)
3. You can also write the full path
Second, static files
Static files: an anterior been written document, we use just take over, then these files can be called called "static file," including a preceding frame bootstrap class, pictures, css files, js files
By default, all static files in a folder under static, static default folder subfolders created folder: css folder (the current site all the style file), js folder (the current site of all the js file), img folder (current website All image files), other (preceding skeleton code, third-party plug-in code)
Static configuration file:
= STATICFILES_DIRS [ os.path.join (base_dir, ' static ' ) ] # as long as you enter the path static folder can access specific files to STATIC_URL = ' / static / ' # this is not a static but rather the name of the folder Interface prefix "" " As long as the resource file path you want to access a static file must start with a static " "" # manually static file all the resources folder exposed to the user STATICFILES_DIRS = [ os.path.join (base_dir, ' static ' ), # the real folder path os.path.join (bASE_DIR, ' static1 ' ), # the real folder path os.path.join (bASE_DIR,'static2 ' ), # real folder path of the os.path.join (base_dir, ' static3 ' ) # real folder path ] # static file interface prefix "dynamic resolution" {% Load static% } <Link the rel = " this stylesheet " the href = " {% static 'on Bootstrap-3.3.7-dist / CSS / bootstrap.min.css'}% " > <Script the src = " {% static' on Bootstrap-3.3.7-dist / JS / on Bootstrap .min.js'}% " > </ Script> # using parser dynamically obtaining interface prefix
Three, request method
1, request.method way you can get the current request, with the request mode, you can divide GET requests and POST requests different write logic
2, processing of data, not just only wsgiref module, Django trailing end of a lot of data processing
GET request.GET data acquisition front-end get submitted (it is like a big dictionary) values request.GET.get ( ' username ' ) # Although the default value is a list, but just take a list of the last element # strongly recommended that you use the value in brackets in the form of # if you want directly to a list of all out (******) request.GET.getlist ( ' Hobby ' ) POST request.POST data acquisition front-end post submission (it is like a big dictionary) the value request.POST.get ( ' username ' ) # Although the default value is a list, but just take a list of the last element # strongly recommended that you use the form values in brackets # if you want directly to list all out (*** ***) request.POST.getlist ( ' Hobby ')
Fourth, the database
1, Django defaults are built-in sqlite database, if you want it to use a different database, you need to configure the settings configuration file
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'day51', 'HOST':'127.0.0.1', 'PORT':3306, 'USER':'root', 'PASSWORD':'123', 'CHARSET':'utf8'} }
2, but also to tell Django project in the name of the file or application name init init file, do not use the default connection mysqldb MySQL, but the use of pymysql
import pymysql pymysql.install_as_MySQLdb()
Five, orm
First need to write a model class in models.py under application
class User (models.Model): # the id field to the User table primary key fields in django orm you can not write to the master key django dictionary will default to your table to create a primary key field named id # id = models.AutoField ( primary_key = True) # Once you specify the primary key field that will not be django will help you automatically create a username = models.CharField (max_length = 32) # username VARCHAR (32) CharField max_length parameters must be specified password = models.IntegerField () # password int
You then need to execute the following two commands
# Database migration (synchronous) command python3 manage.py makemigrations # only in a small notebook (migrations folder) records database changes, and does not modify the database python3 manage.py the migrate # will modify the database record actually synchronized to the database # shorthand makemigrations the migrate
Note: As long as the models move with the database code line pipe, it is necessary to re-execute the above two commands indispensable
Six, deletions change search table field
By: When a table has been created out of the follow-up would also like to add fields, there are two ways
# 1, the default value for the new field to addr = models.CharField (= 32 MAX_LENGTH, default = ' China ' ) # 2, to the new field to be empty age = models.IntegerField (null = True)
Deleted (caution): Remove field, comments directly in the field models.py, and then re-execute two commands to
Note: After execution, delete all the data in the table all the corresponding fields, so under normal circumstances, is not used to delete the true sense
Change: the changes directly in the model class, and then save the modified
Seven, additions and deletions to change search data
# ORM action requires the name of the class Models from app01 Import Models # check data models.User.objects.all () # get all data models.User.onjects.get (username = username) # get directly to the objects, will complain RES = models.User.objects.filter (username = username) # get an object 'list', does not complain res.query # get the sql statement USER_OBJ = res.first () # get the first object # growth data 1.models.User.objects.create (username = username, password = password) 2.user_obj = models.User (username = username, password = password) user_obj.save () # is not recommended use # Deleted data models.User.objects.filter (Condition) .Delete () # change data in a way a:. Models.User.objects.filter (Condition) .Update () username = request.POST.get ( ' username ' ) password = request.POST.get ( ' passowrd ' ) models.User.objects.filter (ID = edit_id) .Update (username = username, password = password) # filter is a get list, filter operation in fact all bulk operations # If there are multiple data filter results list, it will modify all at once. Similarly modified for one loop 2 Second way:. Denit_obj.save () is not recommended edit_obj.username = username edit_obj.password =password edit_obj.save () # The second approach would start to finish all the fields all changes again, very low efficiency
Eight, summary data CRUD
1. orm show all of the preceding data to all () template syntax for loop 2. Add New button (new user's operation) href A label directly trigger back-end logic 3. Add Edit Delete button using the get request carries parameters characteristics, to keep up with the back url id value of the corresponding data request.GET.get () If you are editing, re-rendering a page, to the front end of the edit object passes if deleted, directly filter (). delete ()