回答編集履歴
1
flash表示を追加
answer
CHANGED
@@ -5,4 +5,59 @@
|
|
5
5
|
content_lengthが0のデータが来ているようです。
|
6
6
|
|
7
7
|
|
8
|
-
(少なくともChromeで実行した場合は)そのif文はTrueになることがないと思います。
|
8
|
+
(少なくともChromeで実行した場合は)そのif文はTrueになることがないと思います。
|
9
|
+
|
10
|
+
|
11
|
+
### 2020/02/18 12:40追加
|
12
|
+
|
13
|
+
```html
|
14
|
+
<!-- templates/index.html -->
|
15
|
+
<!DOCTYPE html>
|
16
|
+
<html lang="en">
|
17
|
+
<head>
|
18
|
+
<meta charset="UTF-8">
|
19
|
+
<title>Upload new File</title>
|
20
|
+
</head>
|
21
|
+
<body>
|
22
|
+
{% with messages = get_flashed_messages() %}
|
23
|
+
{% if messages %}
|
24
|
+
{% for message in messages %}
|
25
|
+
<p>{{ message }}</p>
|
26
|
+
{% endfor %}
|
27
|
+
{% endif %}
|
28
|
+
{% endwith %}
|
29
|
+
<h1>Upload new File</h1>
|
30
|
+
<form method=post enctype=multipart/form-data>
|
31
|
+
<input type=file name=file>
|
32
|
+
<input type=submit value=Upload>
|
33
|
+
</form>
|
34
|
+
</body>
|
35
|
+
</html>
|
36
|
+
```
|
37
|
+
|
38
|
+
```python
|
39
|
+
# app.py
|
40
|
+
# render_templateを読み込み
|
41
|
+
from flask import Flask, flash, request, redirect, url_for, render_template
|
42
|
+
|
43
|
+
# 中略
|
44
|
+
@app.route('/', methods=['GET', 'POST'])
|
45
|
+
def upload_file():
|
46
|
+
if request.method == 'POST':
|
47
|
+
# check if the post request has the file part
|
48
|
+
if 'file' not in request.files:
|
49
|
+
flash('No file part')
|
50
|
+
return render_template('index.html')
|
51
|
+
file = request.files['file']
|
52
|
+
# if user does not select file, browser also
|
53
|
+
# submit an empty part without filename
|
54
|
+
if file.filename == '':
|
55
|
+
flash('No selected file')
|
56
|
+
return render_template('index.html')
|
57
|
+
if file and allowed_file(file.filename):
|
58
|
+
filename = secure_filename(file.filename)
|
59
|
+
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
60
|
+
return redirect(url_for('uploaded_file',
|
61
|
+
filename=filename))
|
62
|
+
return render_template('index.html')
|
63
|
+
```
|