Django:
template {{forloop.counter0}}
{% for image in item.itemimage_set.all %}
<li data-target="#item{{item.pk}}Carousel" data-slide-to="{{forloop.counter0}}"></li>
{% endfor %}
forloop.counter0
is 0-indexed
forloop.counter
is 1-indexed
more variables in forloop can be found here:
- forloop.revcounter
- forloop.revcounter0
- forloop.first
- forloop.last
- forloop.parentloop
ForeignKey.on_delete
ForeignKey
takes in on_delete
argument to deal with the situation where the ForeignKey
object is being deleted. The value of on_delete
argument represents this behaviour. There are 3 options (In the examples below, Item
model has Category
as a ForeignKey
):
-
on_delete=models.CASCADE
: deleting a foreign object, i.e. acategory
, will automatically delete all of theitem
s under that category -
on_delete=models.PROTECT
: deleteing a foreign object, i.e. acategory
, will automatically prompt 'Can Not Delete' if there'sitem
s under that category, And theitem
s will be listed -
on_delete=models.SET_NULL
: deleteing a foreign object, i.e. acategory
, will set this foreign key toNull
in all of theitem
s originally under that category.
Google Font
- Go to Google Fonts: https://fonts.google.com/
- add the fonts for your website
- interact with the pop up at bottom right corner
- copy url
- go to your css file
- paste: @import url(...)
- apply the font into css element as usual (font-family: 'Forum', cursive;). you can also copy this on the popup
Python
why @property decorator? SO question ref
-
the function within the class can be accessed like a class' property instead of a function()
- you don't have to call it with
()
to gain access to the result - you can pass parameter into it and process existing class values
class Person(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) birth_date = models.DateField() @property def full_name(self): "Returns the person's full name." return '%s %s' % (self.first_name, self.last_name) @full_name.setter def full_name(self, value): names = value.split(' ') self.first_name = names[0] self.last_name = names[1]
- you don't have to call it with
VS Code
Shortcuts - Find and replace:
- Find: command + F
- Add next occurrence: command + D
- Add ALL occurrences: option + Enter
- Replacement: command + option + F
- Replace: command + Enter
Top comments (0)