0

I am trying to apply a for loop to the following html (in a Django project) such that the 'Name' and the 'Comments' field are caused to repeat on the html view.

When I insert the templating code, that is:

{% for c in comments %}
{% endfor %}

on either side of the content i want to repeat, it simply makes the name and comments disappear altogether and does not have the desired result.

The relevant parts of the file are below:

index.html (the main html page)

{% load static %}
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="{% static 'guestbook/styles.css' %}">
</head>
<body>

<h1>The world's guestbook</h1>
<p><a href="{% url 'sign' %}">Sign </a>the guestbook</p>
{% for c in comments %}
<h2>Name</h2>


<p>This the message that the user leaves.</p>

{% endfor %}
</body>
</html>

views.py (in the guestbook app)

from django.shortcuts import render
from .models import Comment

# Create your views here.

def index(request):
    comments = Comment.objects.order_by('-date_added')
    context ={'comments': comments}
    #name=Name.objects.order_by('-date_added')

    return render(request,'guestbook/index.html')


def sign(request):
    return render(request,'guestbook/sign.html')

models.py file

from django.db import models
from django.utils import timezone

# Create your models here.

class Comment(models.Model):
    name=models.CharField(max_length=20)
    comment=models.TextField()
    date_added=models.DateTimeField(default=timezone.now)
    def __str__(self):
        return self.name    

I am working off a tutorial in which this is the recommended code and the desired result is as expected - I notice my html template does not have div tags and wonder if that could be an issue? If so, how can it be resolved?

enter image description here

Compoot
  • 2,227
  • 6
  • 31
  • 63

1 Answers1

1

You need to pass that context:

def index(request):
    comments = Comment.objects.order_by('-date_added')
    context ={'comments': comments}
    return render(request,'guestbook/index.html', context=context)
                                                  ^^^^^^^^^^^^^^^

From documentation of render:

Context: A dictionary of values to add to the template context. By default, this is an empty dictionary. If a value in the dictionary is callable, the view will call it just before rendering the template.

Meaning, the values inside dictionary which is being used with known argument context of render function, these values will be sent to the template. Then you can access those values through {{ key }} of the dictionary(which is sent as context) in html template, or your case {{ comments }}. More information can be found regarding context in this SO Answer.

ruddra
  • 50,746
  • 7
  • 78
  • 101
  • Is there a good tutorial or link that helps a complete beginner understand the best practices and easiest way to use templates (e.g. something simple like making the customised text input boxes with their css become functional, with the {{form}} command. – Compoot Apr 09 '19 at 11:28
  • Not sure what to suggest, but i think you can follow django's official tutorial or django girl's, or YouTube videos. – ruddra Apr 09 '19 at 19:48