-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDjango_Quick_Revision_Notes_Part1
More file actions
175 lines (99 loc) · 4.66 KB
/
Django_Quick_Revision_Notes_Part1
File metadata and controls
175 lines (99 loc) · 4.66 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Django Notes:
## Some Django Commands Definition:
First of all,
python manage.py makemigrations appname
will create (migration_number).py files in your app migrations folders. These lines of code are nothing but statements which help in creating actual fields in your respective database similar to SQL statements.
In order to execute the migration which was created using the previous command, we will run the following command,
python manage.py migrate
On migrate your new model fields will be reflected in database if there are no errors
## Note:
1. When you add/change model methods, then you don't need to run ./manage makemigrations and ./manage.py migrate.
But whenever you edit your model fields (adding a new one, changing an existing one or altering any of the arguments it takes) then you should always run migrations.
2. We use {% %} and {{ }} to put python code and python variable html code in django respectively and this is known as jinja formatting.
For loop and if statements end with {% endfor %} and {% endif %} in jinja formatting.
3. Steps to add model fields of a class in django admin panel:
a). Go to the app contaning that model.
b). Go to its admin.py file
c). Import the model
d). write
from django.contrib import admin
admin.site.register(model_name)
4. If you want that a class return a string on calling instead of class type then use dunder (double underscore) str built in function of python.
for eg.
class album(models.Model):
def __str__(self):
return self.artist
5. Regex to extract serial number (integer value) from url:
r'^(?P<album_id>[0-9]+)/$'
Here, ^ (carrot sign) and $ represents starting and ending of regex.
6. Shortcut way to rise 404 Error:
from django.shortcuts import get_object_or_404
album = get_object_or_404(Album , pk = album_id)
It will throw a message of Not found error on screen if album doesn't exists inside database.
## Django Imports:
from django.db import models
from django.http import Http404
from django.http import HttpResponse
from django.shortcuts import render
from django.contrib import admin
## Django Database Query Shell Commands:
- from app.models import album, song (Name of model)
- Command to see all objects inside album
album.objects.all()
- Way to create a new object inside album:
1.
a = album(artist="Weeknd",album_title="Star_boy", songn me="star_boy")
a.save()
2.
album.objects.create(artist ="Weeknd", album_title
="Star_boy", song = "star_boy")
- To filter an object using an attribute.
album.objects.filter(id = 1)
Here id or pk is the default primary use by django, It's like
serial no
Another eg:
album.objects.filter(artist__startswith='Week')
- Way to get an object from database on a specific attribute:
album.objects.get(artist="Weekend")
- Some other useful commands:
album1 = album.objects.get(pk=1)
album1.song_set.all()
This will print all song class objects
under album1 object.
Note that class name should be in lowercase (song) always.
album1.song_set.create(song_title = 'I love bacon',file_type='mp3')
This will create a song class object
under album1 object.
album1.song_set.count()
This will count the number of song class objects under
album1 object.
## Django Views
- Rendering
Way to render a template on a request:
1.Define a function in views and on url request, call an view
function.
2.Ex. of a view function
def index(request):
context = {'all_albums': all_albums}
return render(request,
'path_to_template('music/index.html'), context)
Here, context is a dictionary which replaces the words in
jinja code with outside values or data
## Using Dynamic Url
Pass the url by name instead of url pattern. This will do the future
proofing. For eg, If you want to change the url patterns then you
have to modified it only at one place instead of passing it to every
point.
Eg. of Dynamic url passing:
{% url 'detail' album.id %}
where detail is the url name and album.id is replacing the v
ariable with value
Where urlpatterns is:
url(r'^(?P<album_id>[0-9]+)/$', views.detail,
name='detail')
But, if more than one url pattern of different app has same name then we have to use namespace standard
we pass an statement in urls.py file of the app as follows:
app_name = 'music'
Now, modified dynamic url passing is as follows:
{% url 'music:detail' album.id %}
This will tell django that it has to see url name 'detail' inside music app.