1👍
✅
You need to pass a QuerySet
of message
objects as context to the render(…)
function [Django-doc], thus:
from django.shortcuts import render, HttpResponse
from .models import message
def index(request):
messages = message.objects.all()
return render(request, 'Home/index.html', {'message': messages})
Usually model names are written in PascalCase, not snake_case. I therefore advise to rename your class to:
from django.db import models
class Message(models.Model):
your_message = models.CharField(max_length=50)
def __str__(self):
return self.your_message
In the view you then query to that model, and pass it to messages
to the template, not :message
from django.shortcuts import render, HttpResponse
from .models import Message
def index(request):
messages = Message.objects.all()
return render(request, 'Home/index.html', {'messages': messages})
Then we render this by iterating over the messages
variables:
{% for message in messages %}
<h2>{{ message.your_message }}</h2>
{% endfor %}
Source:stackexchange.com