Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Sending Data to Templates | Forms and Dynamic URLs
course content

Course Content

Django: Build Your First Website

Sending Data to TemplatesSending Data to Templates

Description

AppFolder \ veiws.py
  1. Note.objects.all():
    • Note is the model you defined for records in the database;
    • objects is a Django query manager that allows interaction with the database;
    • all() is a method of the manager, returning all objects of that model from the database.
  2. .order_by('-id'):
    • order_by('-id') is a method that sorts the results by the id field in descending order (from highest to lowest). The -id specifies sorting in reverse order based on the id field.

So, this line fetches all objects from the Note model in the database and sorts them by the id field in descending order. The result is stored in the variable notes and can be used, for example, to pass to a template for displaying a list of records.

{'notes': notes}: This is a Python dictionary that passes data to be used in the template. In the template, you can access this data using the key "notes".

AppFolde \ templates \ notes.html
html

index.html

css

index.css

js

index.js

  1. {% for note in notes %}:
    • This is a Django template tag that starts a loop. It iterates over each item (note) in the notes list.
  2. HTML Markup Inside the Loop:
    • <h3>{{ note.title }}</h3>: Displays the title of the current note;
    • <p>{{ note.content }}</p>: Displays the content of the current note;
    • <p>{{ note.created }}</p>: Displays the creation date of the current note with a tertiary text style.
  3. {% endfor %}:
    • Ends the loop.

This template is designed to present a list of notes. For each note, it displays the title, content, creation date, and provides a button link to edit the note. The actual display will depend on the styles and structure of the rest of your HTML and CSS.

Everything was clear?

Section 6. Chapter 3
course content

Course Content

Django: Build Your First Website

Sending Data to TemplatesSending Data to Templates

Description

AppFolder \ veiws.py
  1. Note.objects.all():
    • Note is the model you defined for records in the database;
    • objects is a Django query manager that allows interaction with the database;
    • all() is a method of the manager, returning all objects of that model from the database.
  2. .order_by('-id'):
    • order_by('-id') is a method that sorts the results by the id field in descending order (from highest to lowest). The -id specifies sorting in reverse order based on the id field.

So, this line fetches all objects from the Note model in the database and sorts them by the id field in descending order. The result is stored in the variable notes and can be used, for example, to pass to a template for displaying a list of records.

{'notes': notes}: This is a Python dictionary that passes data to be used in the template. In the template, you can access this data using the key "notes".

AppFolde \ templates \ notes.html
html

index.html

css

index.css

js

index.js

  1. {% for note in notes %}:
    • This is a Django template tag that starts a loop. It iterates over each item (note) in the notes list.
  2. HTML Markup Inside the Loop:
    • <h3>{{ note.title }}</h3>: Displays the title of the current note;
    • <p>{{ note.content }}</p>: Displays the content of the current note;
    • <p>{{ note.created }}</p>: Displays the creation date of the current note with a tertiary text style.
  3. {% endfor %}:
    • Ends the loop.

This template is designed to present a list of notes. For each note, it displays the title, content, creation date, and provides a button link to edit the note. The actual display will depend on the styles and structure of the rest of your HTML and CSS.

Everything was clear?

Section 6. Chapter 3
some-alt