Post List View
Now, let's create the post_list.html file inside the template folder.
For now, the HTML file should be the following structure:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
</head>
<body>
{{ posts }}
</body>
</html>
We implemented the post_list view function:
def post_list(request):
posts = Post.objects.all()
html = ""
for post in posts:
html += f"<h1>{post.title}</h1>"
html += f"<p>{post.text}</p>"
html += f"<p>Post ID: {post.id}</p>"
return HttpResponse(HTML)
Note
The presented code for the task is part of the framework and cannot work separately, so errors will be received when you try to
Run Code. Use theSubmit Taskbutton to solve this task.
Swipe to start coding
-
Rewrite the
post_listfunction using therender()function. The render function should:- Receive the
requestas the first argument. - Receive the
post_list.htmltemplate name as the second argument. - Receive the
contextdictionary as the third argument.
- Receive the
-
The
contextdictionary should contain the"post"key with the iterable QuerySet of posts.
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Sammanfatta detta kapitel
Explain code
Explain why doesn't solve task
Awesome!
Completion rate improved to 3.45
Post List View
Svep för att visa menyn
Now, let's create the post_list.html file inside the template folder.
For now, the HTML file should be the following structure:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
</head>
<body>
{{ posts }}
</body>
</html>
We implemented the post_list view function:
def post_list(request):
posts = Post.objects.all()
html = ""
for post in posts:
html += f"<h1>{post.title}</h1>"
html += f"<p>{post.text}</p>"
html += f"<p>Post ID: {post.id}</p>"
return HttpResponse(HTML)
Note
The presented code for the task is part of the framework and cannot work separately, so errors will be received when you try to
Run Code. Use theSubmit Taskbutton to solve this task.
Swipe to start coding
-
Rewrite the
post_listfunction using therender()function. The render function should:- Receive the
requestas the first argument. - Receive the
post_list.htmltemplate name as the second argument. - Receive the
contextdictionary as the third argument.
- Receive the
-
The
contextdictionary should contain the"post"key with the iterable QuerySet of posts.
Lösning
Tack för dina kommentarer!
single