Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I access data from a dictionary in a HTML template?

When I try to use data from a dictionary, I get the error:

'tuple' object has no attribute 'get'

This happens when I click on a link that should take me to the page of the entry. The entry is a Markdown file that I get from util.get_entry(title).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How do I access data from a dictionary in my HTML template (entry.html)? Do I need to convert the Markdown into HTML?

entry.html:

{% extends "encyclopedia/layout.html" %}

<!-- {% load static %} -->

{% block title %}
{{ name }}
{% endblock %}

{% block style %}{% endblock %}

{% block body %}
<div class="left">{{ entry }}</div>
<div class="right">
    <a href="{% url 'edit' %}">
        <button class="edit">Edit</button>
    </a>
</div>
{% endblock %}

views.py:

from . import util

def entry(request, name):
    if util.get_entry(name) is not None:
        return render(request, 'encyclopedia/entry.html'), {
            'entry': util.get_entry(name),
            'name': name
        }
    else:
        return render(request, "encyclopedia/404.html")

urls.py:

path('<str:name>', views.entry, name='entry'),

>Solution :

You are not passing the dict in parameters. Please use this

from . import util

def entry(request, name):
    if util.get_entry(name) is not None:
        context ={ 
            'entry': util.get_entry(name),
            'name': name

        }
        return render(request, 'encyclopedia/entry.html',context)
    else:
        return render(request, "encyclopedia/404.html")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading