Creating templates in Django

 Creating templates in Django


A template is an HTML file that defines the structure and layout of a web page in Django. We can create templates inside our app's templates directory. Here's an example template for displaying a list of blog posts:


html


{% extends 'base.html' %}


{% block content %}

    <h1>Blog Posts</h1>

    {% for post in posts %}

        <div class="post">

            <h2>{{ post.title }}</h2>

            <p>{{ post.content }}</p>

            <small>{{ post.created_at }}</small>

        </div>

    {% endfor %}

{% endblock %}


In this example, we define a template that extends a base template called base.html. We use the for loop to iterate over all Post objects passed to the template and display their title, `content

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...