此问题在此处已有答案:
Sending data from HTML form to a Python script in Flask(2个答案)
11个月前关闭。
我试图使一个表单,上传数据和图片的SQL数据库的HTML表单是工作正常,但当我按提交它说,该页是不工作,并没有插入任何数据的表可以u plz帮助,并检查我的代码有什么问题
下面是我的python代码#main.py
from flask import Flask, flash, request, redirect, url_for, render_template
import urllib.request
import os
from werkzeug.utils import secure_filename
from Dbaconnection import dbaconnect
app = Flask(__name__)
app.secret_key = "cairocoders-ednalan"
UPLOAD_FOLDER = 'static/uploads/'
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def home():
return render_template('index.html')
@app.route('/webproject', methods=['GET','POST'])
def upload_image():
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No image selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
data=request.form
db = dbaconnect();
dbcursor = db.cursor()
print(data['file'])
dbcursor.execute("INSERT INTO webproject(name,Address,Photo) VALUES(%s,%s,%s)",(data['Name'],data['Address'],data['Photo'],))
db.commit()
flash('Image successfully uploaded and displayed below')
return render_template('index.html', filename=filename)
else:
flash('Allowed image types are - png, jpg, jpeg, gif')
return redirect(request.url)
@app.route('/display/<filename>')
def display_image(filename):
return redirect(url_for('static', filename='uploads/' + filename), code=301)
if __name__ == "__main__":
app.run(debug=True)
这里是HTML表单代码#index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Database upload form</title>
<!-- Font Icon -->
<link rel="stylesheet" href=" {{url_for('static',filename='fonts/material-icon/css/material-design-iconic-font.min.css')}}">
<!-- Main css -->
<link rel="stylesheet" href="{{url_for('static',filename='css/style.css')}}">
</head>
<body>
<div class="main">
<div class="container">
<div class="signup-content">
<img src="{{ url_for('display_image', filename=filename) }}">
<form method="GET","POST" id="signup-form" class="signup-form" action = '/webproject' enctype="multipart/form-data"/>
<h2>Assignment</h2>
<p class="desc">Enter The Student Data To store</span></p>
<div class="form-group">
<input type="text" class="form-input" name="Name" id="name" placeholder="Your Name" />
</div>
<div class="form-group">
<input type="text" class="form-input" name="Address" id="address" placeholder="Your Address" />
</div>
<div class="form-group">
<div class="costom-file">
<label class="custom-file-lable" for="image">file input</label>
<input type="file" class="custom-file-input" id="Photo" name="Photo" VALUE="upload a file" />
</div>
<div class="form-group">
<input type="submit" name="upload on db" id="submit" class="form-submit submit" value="Submit" />
</div>
</form>
</div>
</div>
</div>
<!-- JS -->
<script href="{{url_for('static',filename='vendor/jquery/jquery.min.js')}}"></script>
<script href="{{url_for('static',filename='js/main.js')}}"></script>
</body>
<!-- This templates was made by Colorlib (https://colorlib.com) -->
</html>
1条答案
按热度按时间whlutmcx1#
1.您不能在html
form
标签中放置多个method
。您只能在get
或post
中选择一个1.用于从html表单获取
file
输入的后端代码无效在html表单中,您说:
但在后端代码中:
如前所述in this article