我有一个似乎有效的查询,不会抛出错误。 调用值时,我得到'lgi'是未定义的错误?
def search_staff():
sql_staff = """SELECT s.forename, s.surname, s.brc, s.oou_id, s.staff_id, s.status, lgi.staff_id
FROM staff s
JOIN local_grant_investigators as lgi ON s.staff_id = lgi.staff_id
GROUP BY s.forename, s.surname, s.brc, s.oou_id, s.staff_id, s.status, lgi.staff_id;
"""
r_s = list(conn.execute(text(sql_staff)))
{% if staff %}
<table>
<thead>
<th>Surname</th>
<th>Forename</th>
<th>Status</th>
<th>Cluster</th>
<th>TEST Cluster ID</th>
<th></th>
<th></th>
</thead>
<tbody>
{% for s in staff %}
<tr>
<td> {{ s.surname }} </td>
<td> {{ s.forename }} </td>
<td> {{ s.status }} </td>
<td> {{ s.staff_id }} </td>
<!-- <td> -->
{% if s.staff_id %}
<td>
{{ lgi.staff_id }}
</td>
{% endif %}
<!-- </td> -->
<td>
</td>
<td><a data-toggle="modal" href="#editstaff" class="staffedit" id="{{s.staff_id}}|{{s.forename}}|{{s.surname}}"> Edit </a></td>
<!-- <td><a data-toggle="modal" href="#removestaff" class="staffremove" id="{{s.staff_id}}|{{s.forename}}|{{s.surname}}"> Remove </a></td> -->
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
Traceback (most recent call last):
File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Program Files\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Program Files\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Program Files\Python36\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Program Files\Python36\lib\site-packages\flask_login\utils.py", line 228, in decorated_view
return func(*args, **kwargs)
File "C:\inetpub\wwwroot\FlaskClive\theapp\routes.py", line 1003, in search_staff
return render_template('search_staff.html', staff=r_s)
File "C:\Program Files\Python36\lib\site-packages\flask\templating.py", line 134, in render_template
context, ctx.app)
File "C:\Program Files\Python36\lib\site-packages\flask\templating.py", line 116, in _render
rv = template.render(context)
File "C:\Program Files\Python36\lib\site-packages\jinja2\asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "C:\Program Files\Python36\lib\site-packages\jinja2\environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "C:\Program Files\Python36\lib\site-packages\jinja2\environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Program Files\Python36\lib\site-packages\jinja2\_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "C:\inetpub\wwwroot\FlaskClive\theapp\templates\search_staff.html", line 1, in top-level template code
{% extends "base.html" %}
File "C:\inetpub\wwwroot\FlaskClive\theapp\templates\base.html", line 102, in top-level template code
{% block content %}{% endblock %}
File "C:\inetpub\wwwroot\FlaskClive\theapp\templates\search_staff.html", line 67, in block "content"
{{ lgi.staff_id }}
File "C:\Program Files\Python36\lib\site-packages\jinja2\environment.py", line 430, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'lgi' is undefined
答案 0 :(得分:0)
将您的SQL查询更改为:
sql_staff = """SELECT s.forename, s.surname, s.brc, s.oou_id, s.staff_id, s.status, lgi.staff_id as lgi_staff_id
FROM staff s
JOIN local_grant_investigators as lgi ON s.staff_id = lgi.staff_id
GROUP BY s.forename, s.surname, s.brc, s.oou_id, s.staff_id, s.status, lgi.staff_id;
"""
然后在你的模板中执行以下操作:
{% for s in staff %}
<tr>
<td> {{ s.surname }} </td>
<td> {{ s.forename }} </td>
<td> {{ s.status }} </td>
<td> {{ s.staff_id }} </td>
<!-- <td> -->
{% if s.staff_id %}
<td>
{{ s.lgi_staff_id }}
</td>