How to get the key and values from a dictionary to display them on a Django page?

I want to build a page that has each author with their quote. I have tried, but everything i tried has failed. The function below is what causes me the issues.

quotes = {
    "Arthur Ashe": "Start where you are, Use what you have, Do what you can.",
    "Steve Jobs": "Don’t watch the clock; do what it does. Keep going.",
    "Sam Levenson": "Don’t watch the clock; do what it does. Keep going.",
    " Robert Collier": "Success is the sum of small efforts, repeated day in and day out.",
    "Nelson Mandela": "It always seems impossible until it’s done.",
    "Mahatma Gandhi": "The future depends on what you do today.",
    "Zig Ziglar": "You don’t have to be great to start, but you have to start to be great.",
    "Dave": "Discipline is doing what needs to be done, even if you don’t want to do it.",
    "Suzy Kassem": "Doubt kills more dreams than failure ever will.",
    "Pablo Picasso": "Action is the foundational key to all success."    
}
     
def mypage(request):
    messages = [quotes[item] for item in quotes]
    authors = [item for item in quotes]
    return render(request, "quotes/mypage.html", {"authors": authors, "quotes":messages})

You can pass the entire dictionary:

def mypage(request):
    messages = [quotes[item] for item in quotes]
    authors = [item for item in quotes]
    return render(request, 'quotes/mypage.html', {'quotes': quotes})

and then in the template, enumerate over the .items() of the quotes, so:

{% for author, quote in quotes.items % }
{{author}} said: "{{ quote }}"
{% endfor % }
Вернуться на верх