Reputation: 977
I have elements I get from a DB that I put in a list of lists in Python and send to a template but I get this message:
jinja2.exceptions.UndefinedError
jinja2.exceptions.UndefinedError: list object has no element [1000000001, 'TestTitle', 'TestDescription', 'static/img/uploads/lereacteur.png', datetime.datetime(2020, 5, 10, 16, 8, 46, 826822)]
[1000000001, 'TestTitle', 'TestDescription', 'static/img/uploads/lereacteur.png', datetime.datetime(2020, 5, 10, 16, 8, 46, 826822)] is the first line of the list that I want to send to the template.
The route is :
@app.route('/projects')
def projects():
project_matrix = listOfProjects()
print(project_matrix)
return render_template("projects.html", projects=project_matrix)
This is the template:
{% extends 'template1.html' %}
{% block Title %} Projects {% endblock %}
{% block bodyTitle %} Projects {% endblock %}
{% block subTitle %} Test {% endblock %}
{% block body %}
<div class="projects">
{% for project in projects %}
<div class="project">
<div class="projectTitle">
{{ projects[project][1] }}
</div>
<div class="projectHolding">
<div class="projectDescription">
{{ projects[project][2] }}
</div>
<div class="projectImg">
<img
src="{{ url_for('static', filename = 'img/uploads/'+'{{projects[project][3]}}') }}"
style="min-width: 100px; max-height: 300px;"
alt="tether's Market Cap"
/>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock %}
This is what shows up in the terminal when I load the page:
[[1000000001, 'TestTitle', 'TestDescription', 'static/img/uploads/lereacteur.png', datetime.datetime(2020, 5, 10, 16, 8, 46, 826822)], [1000000002, 'testSecondTitle', 'testSecondDescription', 'static/img/uploads/logo-licence-miashs2.png', datetime.datetime(2020, 5, 11, 2, 4, 52, 200953)]]
127.0.0.1 - - [12/May/2020 02:15:52] "GET /projects HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/hugo/Desktop/website/app.py", line 185, in projects
return render_template("projects.html", projects=project_matrix)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/templating.py", line 140, in render_template
ctx.app,
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/templating.py", line 120, in _render
rv = template.render(context)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/jinja2/environment.py", line 1090, in render
self.environment.handle_exception()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/jinja2/environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/jinja2/_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "/Users/hugo/Desktop/website/templates/projects.html", line 1, in top-level template code
{% extends 'template1.html' %}
File "/Users/hugo/Desktop/website/templates/template1.html", line 39, in top-level template code
{% block body %} {% endblock %}
File "/Users/hugo/Desktop/website/templates/projects.html", line 22, in block "body"
{{ projects[project][1] }}
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/jinja2/environment.py", line 452, in getitem
return obj[argument]
jinja2.exceptions.UndefinedError: list object has no element [1000000001, 'TestTitle', 'TestDescription', 'static/img/uploads/lereacteur.png', datetime.datetime(2020, 5, 10, 16, 8, 46, 826822)]
127.0.0.1 - - [12/May/2020 02:15:52] "GET /projects?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [12/May/2020 02:15:52] "GET /projects?__debugger__=yes&cmd=resource&f=jquery.js HTTP/1.1" 200 -
127.0.0.1 - - [12/May/2020 02:15:52] "GET /projects?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [12/May/2020 02:15:52] "GET /projects?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [12/May/2020 02:15:52] "GET /projects?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [12/May/2020 02:15:52] "GET /projects?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
The first line tells me that the 'print(project_matrix)' sends a valid list of elements to the web page so I don't understand why the error tells me list object has no element.
Do you have any idea of where the problem comes from ?
Upvotes: 0
Views: 15104
Reputation: 1972
You're trying to call a list index, with the list itself.
You already have the list object when you enter the for
loop {% for project in projects %}
. So this line:
{{ projects[project][1] }}
Should be
{{ project[1] }}
Similarly for:
{{ projects[project][2] }}
{{ project[2] }}
and
src="{{ url_for('static', filename = 'img/uploads/'+'{{projects[project][3]}}') }}"
src="{{ url_for('static', filename = 'img/uploads/'+'{{project[3]}}') }}"
Upvotes: 1