DEV Community

Cover image for Retail Website built with Django P4 (2020-02-10)
Peiwen Li
Peiwen Li

Posted on

Retail Website built with Django P4 (2020-02-10)

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 %}
Enter fullscreen mode Exit fullscreen mode

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. a category, will automatically delete all of the items under that category
  • on_delete=models.PROTECT: deleteing a foreign object, i.e. a category, will automatically prompt 'Can Not Delete' if there's items under that category, And the items will be listed
  • on_delete=models.SET_NULL: deleteing a foreign object, i.e. a category, will set this foreign key to Null in all of the items originally under that category.

Google Font

  1. Go to Google Fonts: https://fonts.google.com/
  2. add the fonts for your website
  3. interact with the pop up at bottom right corner
  4. copy url
  5. go to your css file
  6. paste: @import url(...)
  7. 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()

    1. you don't have to call it with () to gain access to the result
    2. 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]
    

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)