嘿,我是 Flask 的新手,我正在尝试制作一个登录和注册系统,我也想显示注册时上传的个人资料照片。我可以存储照片但无法显示,至少我认为是这样。 这是代码。
应用程序.py
@app.route('/register', methods=['GET', 'POST'])
def register():
msg = ''
if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'email' in request.form and 'photo' in request.form:
username = request.form['username']
password = request.form['password']
email = request.form['email']
photo = request.form['photo']
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM accounts WHERE username = % s', (username, ))
account = cursor.fetchone()
if account:
msg = 'Account already exists !'
elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
msg = 'Invalid email address !'
elif not re.match(r'[A-Za-z0-9]+', username):
msg = 'name must contain only characters and numbers !'
注册.html
<form action="{{ url_for('register')}}" method="post" autocomplete="off">
<div class="msg">{{ msg }}</div>
<input type="text" name="username" placeholder="Enter Your Username" class="textbox" id="username" required></br></br>
<input type="password" name="password" placeholder="Enter Your Password" class="textbox" id="password" required></br></br>
<input type="email" name="email" placeholder="Enter Your Email ID" class="textbox" id="email" required></br></br>
<input type="file" name="photo" placeholder="File location" class="textbox" id="photo" required></br></br>
<input type="submit" class="btn" value="Register">
</form>
显示.html
<table class="worddark"></br></br></br></br>
<tr>
<td>Username:</td>
<td>{{ account['username'] }}</td>
</tr>
<tr>
<td>Password:</td>
<td>{{ account['password'] }}</td>
</tr>
<tr>
<td>Email ID:</td>
<td>{{ account['email'] }}</td>
</tr>
<tr>
<td>Profile:</td>
<td>{{ account['photo'] }}</td>
</tr>
我正在尝试获取在个人资料上显示图像的解决方案
您需要使用 html
<img>
标签并将路线放置在 src
属性内。
如果您仅通过名称保存图像
example.jpeg
,则需要在 src 属性中指定整个路线<img src="/your/path/example.jpeg">
。