在Python中检索查询参数

时间:2018-08-09 22:55:24

标签: python python-3.x flask python-decorators

使用Python及其Flask框架时,我在特定页面上有一个按钮链接,该链接传递一个id数值(使用Jinja2模板引擎):

 <table class="table table-striped table-sm">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Last Name</th>
                <th>Email</th>
                <th>Phone</th>
                <th>Role</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
              {% for employee in the_employees %}
                <tr>
                    <td>{{employee.id}}</td>
                    <td>{{employee.name}}</td>
                    <td>{{employee.last_name}}</td>
                    <td>{{employee.email}}</td>
                    <td>{{employee.phone}}</td>
                    <td>{{employee.role}}</td>
                    <td>
                      <a class="btn btn-success" role="button" href="/empdetails/{{employee.id}}">Details</a>
                      <a class="btn btn-info active" role="button" href="/scheduleinfo/{{employee.id}}" style="margin-left:5px;">Schedule</a>
                    </td>

                </tr>
              {% endfor %}
        </tbody>

这是由Python的Flask装饰器路由处理的,该路由从员工列表中获取该ID:

employees = [{'id':1, 'name': "Jonathan", 'last_name':"Wilkinson", "email":"jwilki@zbigg.com", 'phone':"98753215646", 'role':"Owner"},
            {'id':2, 'name': 'Mary', 'last_name': 'Sue', 'email': 'jjeelasj221@zbigg.net',  'phone': "932342384758", 'role': 'Manager'},
            {'id':3,'name': "Joanna", 'last_name': "Kenderson", 'email':'kjjsdakjae@kkzbigg.com', 'phone': '9876478398478', 'role': 'Employee'}
            ]

@app.route('/empdetails/<emp_id>')
def emp_details(emp_id) -> str:
    the_id = emp_id

但是,尽管id值清楚显示在地址栏中,但该值仍会被忽略。在通过之前是否有特定要求? 还是有一个库可以简化这种工作?

1 个答案:

答案 0 :(得分:1)

您必须通过键访问数据值或创建具有所需属性的对象。这个答案说明了两者。

具有用于启用属性调用的类:

class Employee:
  def __init__(self, _data):
    self.__dict__ = _data

现在,在投放表格html的路由中:

@app.route('/get_employees', methods=['GET'])
def get_employees():
  employees = [{'id':1, 'name': "Jonathan", 'last_name':"Wilkinson", "email":"jwilki@zbigg.com", 'phone':"98753215646", 'role':"Owner"},
        {'id':2, 'name': 'Mary', 'last_name': 'Sue', 'email': 'jjeelasj221@zbigg.net',  'phone': "932342384758", 'role': 'Manager'},
        {'id':3,'name': "Joanna", 'last_name': "Kenderson", 'email':'kjjsdakjae@kkzbigg.com', 'phone': '9876478398478', 'role': 'Employee'}]
  return flask.render_template('table_data.html', the_employees = [Employee(i) for i in employees])

或者,没有类,html表文件将变为:

<td>{{employee['id']}}</td>
<td>{{employee['name']}}</td>
<td>{{employee['last_name']}}</td>
<td>{{employee['email']}}</td>
<td>{{employee['phone']}}</td>
<td>{{employee['role']}}</td>
<td>
  <a class="btn btn-success" role="button" href="/empdetails/{{employee['id']}}">Details</a>
  <a class="btn btn-info active" role="button" href="/scheduleinfo/{{employee['id']}}" style="margin-left:5px;">Schedule</a>
</td>