First, the design mode
1.1 MVC
-
-
View (V) is the interface you see, is the presentation models, in addition to providing an interface to collect user input.
-
The controller (C) controlling the flow of information between the model and the view. Information from the database, by transmitting the information to view the program logic. Also collected by the view information from the user, the view changes, by modifying the model data.
MTV 1.2 (Django)
-
-
T represents a "template", the presentation layer . It includes performance-related decision: how to display something on the page or other document types.
-
V represents the "View", the business logic layer . Logic includes access model and select the appropriate template: a bridge between models and templates.
Second, calls Django environment in a Python script
import os if __name__ == '__main__': os.environ.setdefault("DJANGO_SETTINGS_MODULE", "untitled15.settings") import django django.setup() # 测试代码
Three, Django terminal print SQL statements
# settings中配置 LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console':{ 'level':'DEBUG', 'class':'logging.StreamHandler', }, }, 'loggers': { 'django.db.backends': { 'handlers': ['console'], 'propagate': True, 'level':'DEBUG', }, } }
Four, Django request lifecycle
Fifth, the difference Django2.x version 1.x version
django2.x of re_path and 1.x of the same url
path basic rules
- Angle brackets (
<>
) to capture value from the url. - Capture values may comprise a type converter (converter type), such as using
<int:name>
capture an integer variable. Ruoguo no reformer, will match any string, including of course/
the character. - No need to add a leading slash.
path converter
Django's default converter supports the following five:
- str, in addition to the matching path separator (
/
non-empty string) outside, which is the default. - int, matching positive integer, including 0.
- Slug, matching the string of letters, numbers and bars, the underscore.
- uuid, matching formatted uuid, as 075194d3-6885-417e-a8a8-6c931e272f00.
- path, matches any non-empty string, comprising a path separator (/) (not?)
Registering Custom converter
For some complex or require multiplexing, you can define your own converter. Converter is a class or interface, it requires three points:
regex
Class attributes, string type
to_python(self, value)
Method, value is the class attributeregex
is matched string, returns a specific value of the variable Python, for delivery to the corresponding Django view function.to_url(self, value)
Methods, andto_python
contrary, value is the value of a specific variable Python, which returns a string, commonly used reverse url reference.
chestnut:
class FourDigitYearConverter: regex = '[0-9]{4}' def to_python(self, value): return int(value) def to_url(self, value): return '%04d' % value
Use register_converter
to register to the URL configuration:
from django.urls import register_converter, path from . import converters, views register_converter(converters.FourDigitYearConverter, 'yyyy') urlpatterns = [ path('articles/2003/', views.special_case_2003), path('articles/<yyyy:year>/', views.year_archive), ... ]
Six simple file upload
The front end of a bit of attention
1.method need to specify the POST
2.enctype format needs to be changed formdata
Note that temporarily back end
1. Comment out csrfmiddleware intermediate profile
2. Obtain the file data uploaded by the user post by request.FILES
print(request.FILES) print(type(request.FILES.get('file_name'))) file_name=request.FILES.get('file_name').name from django.core.files.uploadedfile import InMemoryUploadedFile with open(file_name,'wb')as f: for i in request.FILES.get('file_name').chunks(): f.write(i)
Seven, choice
Defined in the model table: choice = ((0, 'M'), (1, 'M'), (2, 'Unknown'))
In the field uses: gender = models.IntegerField (choices = choice)
Layer corresponding to the view taken in the text: gender = author.get_gender_display ()