Зміст курсу
Django: First Dive
Django: First Dive
Retrieve Data
To retrieve data from the database, you need to use the Manager.
This manager is available automatically if your IDE has a Django support. In another case, you need to define one more class attribute in your model.
Our Post
class should look like this:
There are many different ways to retrieve data from the database.
We will consider the main two.
Retrieve all records
To retrieve data from the table (Model class), you need to use the all()
manager method.
In this case, we receive the iterable QuerySet.
In Django, a QuerySet
is a collection of database objects (model instances). It represents the results of a query and allows you to retrieve, filter, update, and delete data from your database tables using Python code. It provides methods and properties that help you interact with the data, such as retrieving objects, applying filters, sorting, and performing aggregations. QuerySet
offers a powerful and convenient way to work with your database in Django applications.
Let's iterate on the received Post
QuerySet and display information on the HTML page.
The provided code defines a View function named post_list
that handles an HTTP request. Here's a breakdown of what the code does:
- Inside the function, it retrieves all
Post
objects from the database usingPost.objects.all()
. This creates aQuerySet
that contains all thePost
instances. - An empty string variable named
html
is initialized. This variable will store the HTML content generated dynamically based on the retrievedPost
objects. - A
for
loop is used to iterate over eachpost
in theposts
QuerySet. For eachpost
, the code appends HTML content to thehtml
string variable. It creates an<h1>
heading with the post'stitle
, a<p>
paragraph with the post'stext
, and another<p>
paragraph displaying the post'sid
.
In summary, this code fetches all the Post
objects from the database and generates an HTML string that includes the title, text, and ID of each post. The resulting HTML is then sent as the response to the user's request.
Let's connect this View to the URL pattern inside the urls.py
file.
Now, our page is available by the http://127.0.0.1:8000/posts/
URL address.
Retrieve one object
To retrieve just one record from the database, you can use the get()
manager method.
A View function should look like this:
The main problem is a pk
argument. To receive this argument, we need to connect a specific URL pattern:
The pattern is defined as "posts/<int:pk>/"
, which represents a URL pattern.
<int:pk>
is a path converter that captures an integer value from the URL and assigns it to a variable named pk
. This variable can then be used to identify a specific post.
The taken pk
value via URL address is passed to the view function as the pk
argument, and we can use this argument for the get()
method.
Note
- You can receive other data types via URL address.
- Any name can replace the
pk
argument name, but the name should be the same as the View function argument name.
Let's look at the result in the browser.
Дякуємо за ваш відгук!