无法删除使用烧瓶制作的登录页面上的帐户
Not able to delete an account on a login page made using flask
我正在尝试使用 python flask 和 MySQL 制作一个简单的登录页面。网页本身是使用 html、bootstrap 4 和 css 制作的。我按照教程制作了登录页面,但现在我想添加一种删除帐户的方法。
这是您登录后可见的个人资料页面(显示您的用户名、密码和电子邮件)。数据库 table(称为帐户)有一个主键 ID。 /// 中的部分是我要添加的用于创建删除按钮的代码。 请帮我修复我的删除帐户按钮。
@app.route('/pythonlogin/profile', methods = ['GET'])
def profile():
# Check if user is loggedin
if 'loggedin' in session:
# We need all the account info for the user so we can display it on the profile page
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM accounts WHERE id = %s', (session['id'],))
account = cursor.fetchone()
# Show the profile page with account info
return render_template('profile.html', account=account)
# User is not loggedin redirect to login page
///if request.method == 'GET':
id = session['id']
mycursor = mysql.cursor()
sql = ('DELETE from accounts WHERE id = 4;')
# return redirect(url_for('logout)')
mycursor.execute(sql)
mysql.connection.commit()///
return redirect(url_for('login'))
以下是名为 profile.html 的 html 部分。
{% extends 'layout.html' %}
{% block title %}Profile{% endblock %}
{% block content %}
<h2>Profile Page</h2>
<div>
<p>Your account details are below:</p>
<table>
<tr>
<td>Username:</td>
<td>{{ account['username'] }}</td>
</tr>
<tr>
<td>Password:</td>
<td>{{ account['password'] }}</td>
</tr>
<tr>
<td>Email:</td>
<td>{{ account['email'] }}</td>
</tr>
<tr>
<form action="{{ url_for('logout') }}">
<td>
<button type="submit" class="btn btn-danger">Delete Account</button>
</td>
</form>
</tr>
</table>
</div>
{% endblock %}
在提供的代码中我可以看到一些错误(逻辑上的)
- 如果使用已登录,
return render_template('profile.html', account=account)
之后的代码将不会被执行
- 另一个问题是您正在创建 URL 来注销而不是个人资料页面
<form action="{{ url_for('logout') }}">
最好用 POST 方法做表格。
我正在尝试使用 python flask 和 MySQL 制作一个简单的登录页面。网页本身是使用 html、bootstrap 4 和 css 制作的。我按照教程制作了登录页面,但现在我想添加一种删除帐户的方法。
这是您登录后可见的个人资料页面(显示您的用户名、密码和电子邮件)。数据库 table(称为帐户)有一个主键 ID。 /// 中的部分是我要添加的用于创建删除按钮的代码。 请帮我修复我的删除帐户按钮。
@app.route('/pythonlogin/profile', methods = ['GET'])
def profile():
# Check if user is loggedin
if 'loggedin' in session:
# We need all the account info for the user so we can display it on the profile page
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM accounts WHERE id = %s', (session['id'],))
account = cursor.fetchone()
# Show the profile page with account info
return render_template('profile.html', account=account)
# User is not loggedin redirect to login page
///if request.method == 'GET':
id = session['id']
mycursor = mysql.cursor()
sql = ('DELETE from accounts WHERE id = 4;')
# return redirect(url_for('logout)')
mycursor.execute(sql)
mysql.connection.commit()///
return redirect(url_for('login'))
以下是名为 profile.html 的 html 部分。
{% extends 'layout.html' %}
{% block title %}Profile{% endblock %}
{% block content %}
<h2>Profile Page</h2>
<div>
<p>Your account details are below:</p>
<table>
<tr>
<td>Username:</td>
<td>{{ account['username'] }}</td>
</tr>
<tr>
<td>Password:</td>
<td>{{ account['password'] }}</td>
</tr>
<tr>
<td>Email:</td>
<td>{{ account['email'] }}</td>
</tr>
<tr>
<form action="{{ url_for('logout') }}">
<td>
<button type="submit" class="btn btn-danger">Delete Account</button>
</td>
</form>
</tr>
</table>
</div>
{% endblock %}
在提供的代码中我可以看到一些错误(逻辑上的)
- 如果使用已登录,
return render_template('profile.html', account=account)
之后的代码将不会被执行 - 另一个问题是您正在创建 URL 来注销而不是个人资料页面
<form action="{{ url_for('logout') }}">
最好用 POST 方法做表格。