Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Views and Templates — Rendering the Article List

Views and Templates — Rendering the Article List

In this chapter you will learn to use Django views to fetch data from the database and work with templates to render the article card list on the blog homepage.


The role of the view

The view is Django's core logic layer, responsible for:

Django views have two writing styles: function-based views (FBV) and class-based views (CBV).

In the first eight chapters we prioritize FBV—it is more intuitive and suitable for beginners to understand the request handling flow.


Function-based view basics

A Django view function receives request parameters and returns HttpResponse object.

Example

File path: blog/views.py

from

django.

shortcuts

import

render

from

.

models

import

Post

def

index

(

request

)

:

   

"""Blog homepage: display all articles"""

   

Query all articles from the database (in descending time order)

    posts

=

Post.

objects

.

all

(

)

.

order_by

(

'-created_at'

)

   

The three parameters of render:

   

1. request — the request object

   

2. Template path — blog/index.html

   

3. context dictionary — data passed to the template

    context

=

{

       

'posts'

: posts

,

       

'title'

:

'RUNOOB Blog - Home'

   

}

   

return

render

(

request

,

'blog/index.html'

,

context

)

render() is the most commonly used shortcut function in Django; it combines: load template → inject data → generate HTML → return response.


Django template syntax

Django template syntax is very similar to Vue3 template syntax, but there are some key differences.

Purpose Django template Vue3 template
Output variables {{ variable }} {{ variable }}
Loop {% for item in list %}...{% endfor %} v-for="item in list"
Conditional {% if cond %}...{% endif %} v-if="cond"
URL generation {% url 'name' %} router-link to="name"
Inherit layout {% extends 'base.html' %} Component nesting

Key difference: Django templates use {% %} to wrap control tags, while Vue3 uses v- directives. Django templates render on theServer Sideserver side — HTML is sent to the browser only after it is generated; Vue3 renders on thebrowser side— the browser assembles HTML only after receiving JS.


Template inheritance: base.html

Multiple pages share the same navigation bar and footer; use template inheritance to avoid duplication.

The parent template defines "blocks" and the child template fills in specific content.

Create the base.html parent template

Example

   

   

   

<p>{% block title %}RUNOOB Blog{% endblock %}</p>

   

    {% block extra_head %}{% endblock %}

   

   

   

   

        {% block content %}

        {% endblock %}

   

   

   

       

© 2024 RUNOOB Blog. Powered by Django.

   

Create the index.html child template

Example

{% extends 'blog/base.html' %}

{% block title %}{{ title }}{% endblock %}

{% block content %}

Latest Articles

{% if posts %}

   

        {% for post in posts %}

       

           

               

{{ post.category.name }}

               

{{ post.title }}

               

{{ post.summary|truncatechars:80 }}

               

{{ post.created_at|date:"Y-m-d" }}

           

       

        {% endfor %}

   

{% else %}

   

No articles yet; stay tuned.

{% endif %}

{% endblock %}

Several knowledge points used in the template:

Syntax Meaning
{% extends 'blog/base.html' %} Inherit the parent template; must be written on the first line
{% block content %}...{% endblock %} Fill the content block defined in the parent template
{{ post.category.name }} Access attributes of related objects via foreign keys
{{ post.summary\|truncatechars:80 }} Filter: truncate the first 80 characters
{{ post.created_at\|date:"Y-m-d" }} Filter: format the date

In Django templates, Filter filters are represented by the pipe character | , following a variable and used to format the output. Common filters:date、truncatechars、length、default。


Add article list styles

Append the following CSS in the style tag of base.html:

Example

/* Article card grid layout */

.article-grid

{

   

display

:

grid

;

    grid-template-columns

:

repeat

(

auto-fill

,

minmax

(

280px

,

1fr

)

)

;

    gap

:

24px

;

}

.article-card

{

   

background

:

#fff

;

   

border-radius

:

12px

;

   

overflow

:

hidden

;

   

box-shadow

:

0

2px

12px

rgba

(

0

,

0

,

0

,

0.08

)

;

    transition

:

transform 0.2s

,

box-shadow

0.2s

;

}

.article-card

:hover

{

    transform

:

translateY

(

-4px

)

;

   

box-shadow

:

0

8px

24px

rgba

(

0

,

0

,

0

,

0.12

)

;

}

.card-content

{

   

padding

:

20px

;

}

.card-category

{

   

display

:

inline-block

;

   

padding

:

2px

10px

;

   

background

:

#e8f5e9

;

   

color

:

#2e7d32

;

   

border-radius

:

12px

;

   

font-size

:

12px

;

   

margin-bottom

:

8px

;

}

.article-card

h3

{

   

font-size

:

18px

;

   

margin-bottom

:

8px

;

   

color

:

#222

;

}

.article-card

p

{

   

font-size

:

14px

;

   

color

:

#666

;

   

line-height

:

1.6

;

   

margin-bottom

:

12px

;

}

.card-date

{

   

font-size

:

12px

;

   

color

:

#999

;

}

.empty-tip

{

   

text-align

:

center

;

   

color

:

#999

;

   

padding

:

60px

0

;

   

font-size

:

16px

;

}


Hands-on: Verify the homepage effect

Confirm the following are done:

Start the server and visit the homepage; you should see the article card list dynamically rendered from database data.


Chapter Summary

In this chapter you mastered the core cooperation of Django views and templates: views.py queries data from the database and passes it into context, render() renders the template, the base.html parent template defines layout + blocks, child templates use extends to inherit and fill content, and Django template syntax ({{ }}, {% %}, filters).

Now the blog homepage is database-driven, no longer hardcoded.