Damion Silver
Damion Silver

Reputation: 611

how to get something from my database to appear on my django html project

I am trying to get a quote from my database to appear on my html webpage using django. IT will not appear

here goes my index.html file **the code on this website put the quotes around my answer. in my program its no quotes on it. Other than that , the code is exactly how it appears in my file.:

{%extends 'homepage/layout.html' %}

{% block content %}

{% for quote in quotes %}

<h2>{{quote.body}}</h2>

{% endfor %}

{% endblock %}

Here goes my models.py file:

from django.db import models
class Quote(models.Model):
def __str__(self):
return self.body

Here goes my views.py file:

from django.shortcuts import render
from .models import Quote
def index(request):
quotes = Quote.objects.all()[:1],
context = dict()
context['quotes'] = quotes
return render(request, 'homepage/index.html', context)

When i run this, I get no errors. Its's just the quote that is stored in the database does not display on the screen .

BUT When i run this in my index.html file :

{%extends 'homepage/layout.html' %}
{% block content %}
{% for quote in quotes %}
<h2>{{quote}}</h2>
{% endfor %}
{% endblock %}

it displays the quote as a query set object. So I do know the quote can be referenced and my project is connecting to the database. Im just not understanding why the way im doing it is not working.

the quote is ""To uncover the truth, we must ask ourselves before we ask anything else, is it true?—True to the motives, the impulses, the principles that shape the life of actual men and women?""

Any help please?

Upvotes: 0

Views: 125

Answers (1)

MattRowbum
MattRowbum

Reputation: 2192

You will need to remove the comma from the end of the following line in your views.py file:

quotes = Quote.objects.all()[:1],

Upvotes: 1

Related Questions