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
Django: Build Your First Website
course content

Conteúdo do Curso

Django: Build Your First Website

Django: Build Your First Website

1. Get Started
2. The First Application
3. Models
4. CRUD Operations
5. Templates
6. Forms and Dynamic URLs

bookSending Data to Templates

Description

  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".

html

index

css

index

js

index

copy
  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.

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 6. Capítulo 3
some-alt