【问题标题】:Flask-security delete user return an errorFlask-security 删除用户返回错误
【发布时间】:2021-03-09 07:51:27
【问题描述】:

我正在尝试在烧瓶应用程序的用户管理页面上添加删除按钮。

但是当我点击按钮时我已经有这个错误了:

sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.str' is not mapped

这是我的 adminusers.html 文件:

{% extends 'base.html' %}
{% block main %}
<main>
  <table class="table table-striped users">
    <thead>
      <tr>
        <th scope="col">#</th>
        <th scope="col">Name</th>
        <th scope="col">Email</th>
        <th scope="col">Roles</th>
        <th scope="col" class="text-center">Delete</th>
      </tr>
    </thead>
    <tbody>
    {% for user in users %}
      <tr>
        <th scope="row">{{ user.id }}</th>
        <td>{{ user.name }}</td>
        <td>{{ user.email }}</td>
        <td>
          {% for role in user.roles %}
            {{role.name}};
          {% endfor %}
        </td>
        <td style="text-align: center;"><a href="{{ url_for('delete_user', user=user) }}" style="color:red" ><i class="fas fa-times"></i></a></td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
</main>
{% endblock main %}

还有我的 app.py 文件:

@app.route('/adminusers')
def list_users():
    users= User.query.all()
    return render_template('adminusers.html', users=users)

@app.route('/delete_user/<user>')
def delete_user(user):
    user_datastore.delete_user(user=user)
    db.session.commit()
    return redirect(url_for('adminusers'))

我正在尝试使用“电子邮件”或“姓名”,但它已经返回错误

【问题讨论】:

  • delete_user takes a model 作为它的参数,你传入了一个字符串。

标签: flask flask-sqlalchemy flask-security


【解决方案1】:

感谢@pjcunningham,这样它就可以工作了:

@app.route('/delete_user/<id>')
def delete_user(id):
    user = user_datastore.get_user(id)
    user_datastore.delete_user(user)
    db.session.commit()
    return redirect(url_for('adminusers'))

【讨论】:

    猜你喜欢
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多