반응형
플라스크 - 잘못된 요청 브라우저(또는 프록시)가 이 서버가 이해할 수 없는 요청을 보냈습니다.
파일 업로드 및 플라스크를 사용하여 MongoDB에 데이터 입력 작업을 하려고 하는데 양식을 작성하고 이미지를 업로드할 때 다음 오류가 발생했습니다.
잘못된 요청 - 브라우저(또는 프록시)가 이 서버가 인식할 수 없는 요청을 보냈습니다.
내 HTML 코드
<form class="form-check form-control" method="post" enctype="multipart/form-data" action="{{ url_for('index') }}">
<label>Full Name*</label></td>
<input name="u_name" type="text" class="text-info my-input" required="required" />
<label>Email*</label>
<input name="u_email" type="email" class="text-info my-input" required="required" />
<label>Password*</label>
<input name="u_pass" type="password" class="text-info my-input" required="required" />
<label>Your Image*</label>
<input name="u_img" type="file" class="text-info" required="required" /></td>
<input name="btn_submit" type="submit" class="btn-info" />
</form>
내 파이썬 코드:
from flask import Flask, render_template, request, url_for
from flask_pymongo import PyMongo
import os
app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'flask_assignment'
app.config['MONGO_URI'] = 'mongodb://<user>:<pass>@<host>:<port>/<database>'
mongo = PyMongo(app)
app_root = os.path.dirname(os.path.abspath(__file__))
@app.route('/', methods=['GET', 'POST'])
def index():
target = os.path.join(app_root, 'static/img/')
if not os.path.isdir(target):
os.mkdir(target)
if request.method == 'POST':
name = request.form['u_name']
password = request.form['u_pass']
email = request.form['u_email']
file_name = ''
for file in request.form['u_img']:
file_name = file.filename
destination = '/'.join([target, file_name])
file.save(destination)
mongo.db.employee_entry.insert({'name': name, 'password': password, 'email': email, 'img_name': file_name})
return render_template('index.html')
else:
return render_template('index.html')
app.run(debug=True)
오류는 다음과 같이 발생합니다.BadRequestKeyError
존재하지 않는 키에 대한 액세스 때문에request.form
.
ipdb> request.form['u_img']
*** BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.
업로드된 파일은 다음과 같이 입력됩니다.request.files
그리고 아닌request.form
사전.또한 값이 아래에 입력되므로 루프를 잃어야 합니다.u_img
의 예입니다.FileStorage
그리고 주목할 만합니다.
@app.route('/', methods=['GET', 'POST'])
def index():
target = os.path.join(app_root, 'static/img/')
if not os.path.isdir(target):
os.makedirs(target)
if request.method == 'POST':
...
file = request.files['u_img']
file_name = file.filename or ''
destination = '/'.join([target, file_name])
file.save(destination)
...
return render_template('index.html')
언급URL : https://stackoverflow.com/questions/48780324/flask-bad-request-the-browser-or-proxy-sent-a-request-that-this-server-could
반응형
'sourcecode' 카테고리의 다른 글
코드(c#)에서 div를 숨기는 방법 (0) | 2023.07.12 |
---|---|
HttpResponseMessage 헤더에 Content-Type 헤더를 설정할 수 없습니까? (0) | 2023.07.12 |
ansysql INSERT INTO 생성 (0) | 2023.07.12 |
SPARS COLUMN을 사용해야 하는 이유와 시기(SQL SERVER 2008) (0) | 2023.07.12 |
Firebase child_added only get child added (0) | 2023.07.12 |