我有一个页面显示Employees Masterlist
,我想做的就是单击Employee No.
时将查看该员工的时间表。
由于我的研究,我尝试使用它。
在我的routes.py
@app.route("/employee/<int:employee_id>")
def employee(employee_id):
employee = Employee.query.get_or_404(employee_id)
return render_template('emp_timesheet.html', employee=employee)
这是我的masterlist.html
{% extends 'header.html' %}
{% block content %}
<table width="100%" class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Employee Number</th>
<th>Name</th>
<th>Contract</th>
<th>Position</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
{% for employee in list_employee %}
<tr class="odd gradeX">
<td><a href="{{ url_for('employee', employee_id=employee.id) }}">{{ employee.employee_number }}</a></td>
<td>{{ employee.last_name }}, {{ employee.first_name }}</td>
<td>{{ employee.contract }}</td>
<td class="center">{{ employee.title }}</td>
<td class="center">{{ employee.salary }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}
但是我遇到了这个错误:
File "/home/jxmtsi/Desktop/flask_training/rep/templates/header.html", line 293, in top-level template code
{% block content %}{% endblock %}
File "/home/jxmtsi/Desktop/flask_training/rep/templates/masterlist.html", line 27, in block "content"
<td><a href="{{ url_for('employee', employee_id=employee.id) }}">{{ employee.employee_number }}</a></td>
File "/home/jxmtsi/.local/lib/python3.6/site-packages/jinja2/environment.py", line 430, in getattr
return getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'employee' is undefined
答案 0 :(得分:0)
我认为,问题出在您的Python代码中,更确切地说是您的应用程序与SQL库之间的通信中的问题。或者,也许您在SQL数据库中没有有关请求的employee_id的数据。
检查,方法employee
中的变量employee (employee_id)
是否正在从您的数据库接收数据。例如,尝试print(employee.last_name)
:
@app.route("/employee/<int:employee_id>")
def employee(employee_id):
employee = Employee.query.get_or_404(employee_id)
print(employee.last_name) # or print other value from table (Model) Employee
# print(employee.id)
return render_template('emp_timesheet.html', employee=employee)