-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdjango_note.txt
More file actions
144 lines (106 loc) · 4.09 KB
/
Copy pathdjango_note.txt
File metadata and controls
144 lines (106 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
- command line to start project:
+ "virtualenv venv"
+ "venv\scripts\activate"
+ "pip install mysqlclient"
+ "pip install django"
+ "pip install Ipython"
+ "django-admin startproject project_name" - create django project
+ "python manage.py startapp app_name" - create new django app
+ "pip freeze > requirements.txt" - add requirements
VIEWS AND URLS:
- view: function(request)
+ HttpResponse
+ Http404
+ HttpResponsePredict
- url: domain
- path(domain, view function, name path)
- include
- set base direction of template in setting
- request.POST is a dictionary of data
- request.FILES
- lesson 13: end part
TEMPLATES:
- render(requestHtml, html file, context) - context is dict
- variable template: a dicttionary to push variable in template
+ filter: modify variable in template: {{ variable }}
+ method of object in template just use if that method have no argument
+ tags: perform specific actions into template: {% actions %}
+ for loop
+ if else
+ url in template: {% url 'my_app:variable' %}
-> First add namespace for app: app_name = 'nameOfApp'
-> search path have name 'variable' in app 'my_app'
- template inheritance:
+ set template base in setting: os.path.join(BASE_DIR, 'templates')
+ block, extends
- custom 404 page: add 404.html file in main templates
STATIC:
- static: put img, css in template
- STATIC_URL: load every 'static' folder
- STATICFILES_DIRS: directories where look for static files that are not tied to a specific app.
- imgField:
+ "pip install Pillow" to process img
+ upload_to
+ default=
- MEDIA_ROOT: đường dẫn các file media trong project
- MEDIA_URL: tạo ra các đường dẫn URL cho file media.
- urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) - add các URL của file media vào urlpatterns
- when the form contains files or non-ASCII data: using (enctype='multipart/form-data')
- update staticfiles: "python manage.py collectstatic"
MODELS:
- model is table, field is column
- "python manage.py makemigrations app_name" - save changes of models
- "python manage.py migrate" - connect to database
- "python manage.py shell" - open evironment excute project like access database(insert, delete, ...)
- objectName.:
+ save() - save changes of object in database
+ delete() - delete this object in database
- modelName.objects:
+ .create() - insert data
+ .objects.bulk_create(list) - insert many data
+ .objects.all() - return a querySet of all objects
+ .get() - return just 1 object
+ .filter()
+ .filter(Q() & Q())
+ .filter(columnName__lockupName...) - more complex query
+ .order_by('columnName1', 'columnName2')
- lookup:
__exact: "ABC" vs "AbC" is different
- validators=[] - list of function, class validates the input data before it's processed or saved.
- signals: like trigger in mysql
DJANGO-ADMIN:
+ domain.com/admin
+ 'python manage.py createsuperuser' - create admin account
+ admin.site.register(nameModelClass) - add
+ ModelAdmin - add some frontend of admin page
DJANGO-FORM:
+ GET: request to get data
+ POST: request to send data
+ CSRF_token
+ create a forms.py in app
+ form is class inherite forms.Form
+ attribute is information in form
CLASS-BASED-VIEWS:
+ TemplateView
AUTHENTICATION:
+ authenticate(request, username=..., password=...) - authenticate User in django.contrib.auth
+ logout(request)
+ request.user.is_authenticated
+ messages
+ django decorator: @login_required(login_url='app_name:path_name') - if not authenticated, move to login_url
REST:
+ client-server
+ stateless: server doesn't store request data
+ cacheable: client can cache response data
+ uniform interface: client and server have same interface
+ layer system: client and server can be separated
+ code on demand (optional): server can send code to client
ELASTICSEARCH:
+ "python manage.py search_index --rebuild"
RUN:
pip install -r requirements.txt
run redis-server
run elasticsearch server
python manage.py search_index --rebuild (first time)
python -m celery -A src worker -l info -P gevent
python manage.py runserver