Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Post List View | Templates
Django: First Dive

bookPost 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 the Submit Task button to solve this task.

Tehtävä

Swipe to start coding

  1. Rewrite the post_list function using the render() function. The render function should:

    • Receive the request as the first argument.
    • Receive the post_list.html template name as the second argument.
    • Receive the context dictionary as the third argument.
  2. The context dictionary should contain the "post" key with the iterable QuerySet of posts.

Ratkaisu

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 5. Luku 3
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Suggested prompts:

Tiivistä tämä luku

Explain code

Explain why doesn't solve task

close

Awesome!

Completion rate improved to 3.45

bookPost List View

Pyyhkäise näyttääksesi valikon

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 the Submit Task button to solve this task.

Tehtävä

Swipe to start coding

  1. Rewrite the post_list function using the render() function. The render function should:

    • Receive the request as the first argument.
    • Receive the post_list.html template name as the second argument.
    • Receive the context dictionary as the third argument.
  2. The context dictionary should contain the "post" key with the iterable QuerySet of posts.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

close

Awesome!

Completion rate improved to 3.45
Osio 5. Luku 3
single

single

some-alt