2️⃣ 🌐 Core Django Concepts
Estimated reading: 3 minutes 46 views

📊 Django Display Data – Render Database Content in Templates (2025 Guide)

🧲 Introduction – Why Learn to Display Data in Django?

After creating models and storing data, the next step is displaying that data on your website. Django allows you to pass data from the view to the template using context dictionaries—making it easy to render dynamic content.

🎯 In this guide, you’ll learn:

  • How to retrieve data from models
  • How to pass data to templates
  • How to display lists, tables, and conditional content
  • Tips for formatting and organizing data in HTML

🛠️ Step 1: Create a Django Model

Example model in models.py:

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    published = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

Make migrations and apply:

$ python manage.py makemigrations
$ python manage.py migrate

🧪 Step 2: Create Sample Data

You can use the Django shell:

$ python manage.py shell
>>> from blog.models import Post
>>> Post.objects.create(title="First Post", content="Welcome to Django!")

🧱 Step 3: Fetch Data in View

In views.py:

from django.shortcuts import render
from .models import Post

def post_list(request):
    posts = Post.objects.all()
    return render(request, 'blog/post_list.html', {'posts': posts})

🖼️ Step 4: Display Data in Template

templates/blog/post_list.html:

<!DOCTYPE html>
<html>
<head><title>Posts</title></head>
<body>
  <h1>Blog Posts</h1>
  {% if posts %}
    <ul>
      {% for post in posts %}
        <li>
          <h2>{{ post.title }}</h2>
          <p>{{ post.content }}</p>
          <small>Published on {{ post.published|date:"F j, Y" }}</small>
        </li>
      {% endfor %}
    </ul>
  {% else %}
    <p>No posts available.</p>
  {% endif %}
</body>
</html>

🌐 Step 5: Map URL to the View

urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.post_list, name='post-list'),
]

✅ Now visit http://localhost:8000/ to see your database data rendered on the page.


💡 Bonus: Format & Enhance Display

  • Use template filters like |date, |truncatechars, |linebreaks
  • Add pagination for large datasets
  • Apply CSS or Bootstrap classes for layout

✅ Best Practices for Displaying Data

  • Always sanitize input (Django auto-escapes output)
  • Use {% for %} loops for lists
  • Handle empty queries with {% if posts %}...{% else %}
  • Prefer context variables over hardcoded HTML

📌 Summary – Recap & Next Steps

🔍 Key Takeaways:

  • Use Django ORM to retrieve data with Model.objects.all()
  • Use render() to pass data to templates via context
  • Display data with {{ variable }} inside loops and conditionals

⚙️ Real-World Relevance:
Displaying dynamic data is essential for blogs, dashboards, stores, news portals, and any content-driven web app.


❓ Frequently Asked Questions (FAQ)

❓ How do I display all records from a model?

✅ Use:

Model.objects.all()

❓ Can I filter data before displaying?

✅ Yes, example:

Model.objects.filter(is_published=True)

❓ What if the database is empty?

✅ Use {% if data %}...{% else %} to show fallback content.


❓ How do I show just part of the content?

✅ Use filters:

{{ post.content|truncatewords:20 }}

❓ Can I style the displayed data?

✅ Yes. You can wrap the output in HTML/CSS, use Bootstrap, or add inline styles.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Django Display Data

Or Copy Link

CONTENTS
Scroll to Top