1. Django Framework Details
1. View views.py
1. FBV (function base view) functions-based view
Throws a question:
Can a function decorator be used directly to decorate a method in a class?
2. CBV (class base view) FBV (function base view)
3. Considerations for uploading Files:
1. If there is an upload file, the uploaded file object should be taken from Request.Files in views.py
2. If there is an upload file, the form form in the HTML file must be added enctype= "Multipart/form-data"
File_obj = Request. Files.get ("Touxiang") # Get Upload file Object
file_name = file_obj.name # get the file name
With open (file_name, "WB") as F: # Create a new file with the same name locally
For File_obj.chunks (): # Read data from 1.1 points in uploaded file object
F.write (line) # Write to the new file
4. Jsonresponse
A method that is specifically designed to return JSON-formatted data in a Django package
From django.http import Jsonresponse
Jsonresponse (dictionary)
Jsonresponse (list, safe=false)
2. Template language
1. Template language content that has been learned in the present
1. {{variable}}
2. {% logical operation%}
1. For loop
{% for I in list%}
{{i}}
{% ENDFOR%}
Forloop.counter
Forloop.counter0
Forloop.last
{% EMPTY%}
2. If judgment
{% if condition%}
Things to do when conditions are established
{% Else%}
Conditions are not set up to do the thing
{% ENDIF%}
3. Logical Judgment
1. In judging
2. = = Judgment
1. Template language Variables related
1. Value corresponding to the key of the dictionary
{{Dic.key}}
2. List by index value
{{List.1}}
3. Properties and methods of objects
{{Obj.name}}
{{Obj.dream}}--Methods do not add parentheses
2. Filters (do some extra work on the variable)
1. Built-in filter
2. Custom Filter
1. Create a new Python package under the app, which must be named Templatetags
2. Create a new Python file in the package above, define the function, and register to the Django template language
From Django Import Template
# generate an instance for registering a custom filter method
Register = template. Library ()
@register. Filter (name= "SB")
def add_sb (value):
Return "{} SB". Format (value)
3. Using the Custom filter method
{% load py filename%}
{{VALUE|SB}}
3. Tags
4. Master and Inheritance
1. Define the common parts that the master and many other pages will use we can extract them and put them in a separate HTML file.
2. Replace the corresponding content in the master by defining a different block waiting for the sub-page
3. Inherit the defined master by {% extends ' base.html '%} in the sub-page
4. Implement custom page content in sub-pages via block
5. Page-css and PAGE-JS two blocks are typically defined in the master for sub-pages
5. Components
Put the functionally independent HTML code in a separate file as a component for other pages to use
{% include ' nav.html '%}
Python-day57 Django Introduction