day 53: Creating the configuration file django static files, add or delete data, change search

How to create is not to fill django app name when you create a file:

1, the first knock at the command line:

python3 manage.py startapp app name

 

 

2, then go register settings app:

In the INSTALLED_APPS

Then add functionality:

That is, views and urls

Importing files urls in view

Static file:

html files are all placed in the default templates folder
for front-end has been written document we just use it to take over these files can be called called "Static file"
static file can be:
Bootstrap front-end framework of a class has been written of the
picture
css
js
static files by default all on static folder (created manually)
static default folder created by default subfolder
css folder of the current site all the style file
js file js file all current Web
img file All image files current site
other (front-end framework code third-party plug-in code ...)

 Note that when the starting point django:

Ddjango project started when we must make sure that a port number is only a django project in occupied
otherwise would be likely to cause bug

Users can access all resources in the url

Only open the url related resources you can access to (******)
so your front-end framework for a class of resources need to manually exposed to the front end.
Back-end resources generally need to manually specify whether exposed to the user

 

 

 Static files manually exposed to the user:

= STATICFILES_DIRS [
the os.path.join (base_dir, 'static')
]
as long as you enter the path static folder specific to the file can access.
STATIC_URL = '/ static /' # the name of the folder is not a static but rather an interface prefix
"" "If you want to access a resource file path to a static file must start with a static" ""
# static folder manually all exposed to the user resources
STATICFILES_DIRS = [
os.path.join (base_dir, 'static'), the true path of the folder #
os.path.join (bASE_DIR, 'static1') , # real folder path
os.path.join (bASE_DIR, 'static2'), # the real folder path
os.path.join (bASE_DIR, 'static3') # real folder path

Go to the lowest setting in the configuration:

Static file interface prefix dynamic analysis:

After setting the interface prefix can be arbitrarily set, but you can get files can be dynamically:

 

 

 

Browser's cache function:

 

 

 

django auto-refresh mechanism:

The default is to support automatic restart django code so you only need to refresh the page several times you can
, but sometimes it's restart mechanism is relatively slow
mechanism: real-time monitoring file changes as long as the code changes will automatically restart
possible that your code has not yet finished this time will automatically error

Sign in the front end of the basic settings registration page:

The default is to get a request form form
data-carrying url way to say hello followed by the data
http://127.0.0.1:8000/login/?username=zekai&password=123

can change post method request
after request needs to go into post settings file Comment out a middleware

 

 

 

 

form form data submitted by the action destination
default to 1. The current address is not written filed
2 can also be written suffix / index / (used this project)
3. The full path can also be written

Back ends should behave differently depending on the request type:

Login DEF (Request):
# view function for a different request should have different processing logic
# IF request.method == 'the GET':
  # Print ( 'received')
  # Print (request.method) can acquire distal # request manner and the whole string to uppercase
  # Print (type (request.method))
  # return the render (request, 'the login.html')
  # elif request.method == 'the POST':
    # # get user input accordingly Analyzing logic
    # return HttpResponse ( "get brother")
IF request.method == 'the POST':
  return the HttpResponse ( 'coming baby')
return the render (Request, 'the login.html')

 

 

 Distinguish between simple wording:

 

 

 RESQUEST using simple methods:

Front-end data acquisition
request.method acquisition request method of

processing of the data is not just only wsgiref django backend module also done 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 form values in brackets

# if you want directly to list all out (******)
request.GET.getlist ( 'Hobby')
POST
request.POST data acquisition front-end post submission (it is like a big dictionary)
values
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')

 

django default using built-in sqlite database
if you want it to other database settings need to be configured in the configuration file
1.settings file configuration
DATABASES = {
'default': {
'ENGINE': 'django.db.backends. MySQL ',
' NAME ':' day51 ',
' the HOST ':' 127.0.0.1 ',
' PORT ': 3306,
' the USER ':' the root ',
' PASSWORD ':' 123 ',
' the CHARSET ':' UTF8 '
}
}


2.'ll tell init file in the project name or the name of the application init file django Do not use the default connection mysql mysqldb
but the use of pymysql
Import pymysql
pymysql.install_as_MySQLdb ()

ORM Django
the ORM object relational mapping
table class
a record target
attribute value of the object field corresponding to the


first model classes models.py need to write the application
class User (models.Model):
# id field is set to the User table primary key field 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 so it will not automatically django will help you create a
username = models.CharField (max_length = 32) # username varchar (32) CharField max_length parameters must be specified
password = models.IntegerField () # password int
************* ************ need to perform database migration (synchronous) command ******************************
python3 manage.py makemigrations # only in a small notebook (migrations folder) and modify the database records are not directly manipulate data
python3 manage.py migrate # modify the database record actually synchronized to the database
Note: As long as the models move in with database related code must be re-implementation of the above two lives So indispensable (******)

django command to create a table in the database:

 

 

 

Field of deletions change search
increases
when the table has been created out of a subsequent want to add fields, there are two ways
1. The new field to set a default value
addr = models.CharField (max_length = 32, default = 'China ') # default value of this field defaults

2. Give the new field to be empty
age = models.IntegerField (null = True) # This field allows null


deletion (caution)
and re-execute two command to delete the comment field in the field directly models.py you can
Note: to delete all the data in the table fields corresponding to all after the execution
and under normal circumstances is basically will not be used to delete the true sense

orm operation requires the use of the name of the class of models of

data check
from app01 Import models

models.User.objects.all () # direct holding all the data

models.User.objects.get (username = username) # is not recommended, because when a query condition does not exist, the page will complain

res = models.User.objects.filter (username = username)


res.query: used to view objects native sql statement:

user_obj = res.first()

 

 

 

 

数据的增
1.
models.User.objects.create(username=username,password=password)

2.
user_obj = models.User(username=username,password=password)
user_obj.save()


models.User.objects.filter(条件).delete()


models.User.objects.filter(条件).update()

 

 

 

Guess you like

Origin www.cnblogs.com/yangjiaoshou/p/11567048.html