質問編集履歴
4
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -80,8 +80,6 @@
|
|
80
80
|
|
81
81
|
app = Flask(__name__)
|
82
82
|
|
83
|
-
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
|
84
|
-
|
85
83
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://username@localhost:5432/sample'
|
86
84
|
|
87
85
|
db = SQLAlchemy(app)
|
3
プログラムを最新版に修正しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -70,15 +70,19 @@
|
|
70
70
|
|
71
71
|
```python
|
72
72
|
|
73
|
-
from flask import Flask, render_template
|
73
|
+
from flask import Flask, flash, render_template, request, redirect, url_for, abort, jsonify
|
74
74
|
|
75
75
|
from flask_sqlalchemy import SQLAlchemy
|
76
76
|
|
77
|
+
import sys
|
78
|
+
|
77
79
|
|
78
80
|
|
79
81
|
app = Flask(__name__)
|
80
82
|
|
83
|
+
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
|
84
|
+
|
81
|
-
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://username@localhost:5432/
|
85
|
+
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres://username@localhost:5432/sample'
|
82
86
|
|
83
87
|
db = SQLAlchemy(app)
|
84
88
|
|
@@ -86,19 +90,23 @@
|
|
86
90
|
|
87
91
|
class Todo(db.Model):
|
88
92
|
|
89
|
-
__tablename__ = 't
|
93
|
+
__tablename__ = 'todos'
|
90
|
-
|
94
|
+
|
95
|
+
|
96
|
+
|
91
|
-
id = db.Column(db.Integer, primary_key=True)
|
97
|
+
id = db.Column(db.Integer, primary_key=True)
|
92
|
-
|
98
|
+
|
93
|
-
username = db.Column(db.String(), nullable=False)
|
99
|
+
username = db.Column(db.String(), nullable=False)
|
94
|
-
|
100
|
+
|
95
|
-
texts = db.Column(db.String()
|
101
|
+
texts = db.Column(db.String(120))
|
96
|
-
|
97
|
-
|
98
|
-
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
|
106
|
+
|
99
|
-
def __repr__(self):
|
107
|
+
def __repr__(self):
|
100
|
-
|
108
|
+
|
101
|
-
return f'<Todo {self.id} {self.username} {self.texts}>'
|
109
|
+
return f'<Todo {self.id} {self.username} {self.texts}>'
|
102
110
|
|
103
111
|
|
104
112
|
|
@@ -108,55 +116,77 @@
|
|
108
116
|
|
109
117
|
#ユーザ名・テキストが入力されていたら、データベースに格納し、HTMLページに表示
|
110
118
|
|
111
|
-
@app.route('/t
|
119
|
+
@app.route('/todos/create', methods=['POST'])
|
112
120
|
|
113
121
|
def create_todo():
|
114
122
|
|
123
|
+
error = False
|
124
|
+
|
125
|
+
body = {}
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
try:
|
130
|
+
|
115
|
-
username = request.form
|
131
|
+
username = request.form['username']
|
116
|
-
|
132
|
+
|
117
|
-
texts = request.form
|
133
|
+
texts = request.form['texts']
|
118
|
-
|
134
|
+
|
119
|
-
todo = Todo(texts=texts)
|
135
|
+
todo = Todo(username=username, texts=texts)
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
136
|
+
|
137
|
+
|
138
|
+
|
125
|
-
if username and texts:
|
139
|
+
if username and texts:
|
140
|
+
|
126
|
-
|
141
|
+
db.session.add(todo)
|
142
|
+
|
143
|
+
db.session.commit()
|
144
|
+
|
145
|
+
body['username'] = todo.username
|
146
|
+
|
147
|
+
body['texts'] = todo.texts
|
148
|
+
|
127
|
-
flash("成功", "success")
|
149
|
+
flash("成功", "success")
|
128
|
-
|
129
|
-
|
150
|
+
|
130
|
-
|
131
|
-
|
151
|
+
|
132
|
-
|
133
|
-
|
152
|
+
|
134
|
-
|
135
|
-
'username':todo.username,
|
136
|
-
|
137
|
-
'texts': todo.texts
|
138
|
-
|
139
|
-
})
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
#ユーザー名、テキストが入力されていなかった時
|
153
|
+
#ユーザー名、テキストが入力されていなかった時
|
144
|
-
|
154
|
+
|
145
|
-
elif username == "":
|
155
|
+
elif username == "":
|
146
|
-
|
156
|
+
|
147
|
-
flash("名前を入力してください", "failed")
|
157
|
+
flash("名前を入力してください", "failed")
|
148
|
-
|
158
|
+
|
149
|
-
b.session.rollback()
|
159
|
+
b.session.rollback()
|
150
|
-
|
151
|
-
|
152
|
-
|
160
|
+
|
161
|
+
|
162
|
+
|
153
|
-
elif texts == "":
|
163
|
+
elif texts == "":
|
154
|
-
|
164
|
+
|
155
|
-
flash("テキストを入力してくfださい", "failed")
|
165
|
+
flash("テキストを入力してくfださい", "failed")
|
166
|
+
|
167
|
+
db.session.rollback()
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
except:
|
172
|
+
|
173
|
+
error = True
|
156
174
|
|
157
175
|
db.session.rollback()
|
158
176
|
|
159
|
-
|
177
|
+
print(sys.exc_info())
|
178
|
+
|
179
|
+
finally:
|
180
|
+
|
181
|
+
db.session.close()
|
182
|
+
|
183
|
+
if error:
|
184
|
+
|
185
|
+
abort (400)
|
186
|
+
|
187
|
+
else:
|
188
|
+
|
189
|
+
return jsonify(body)
|
160
190
|
|
161
191
|
|
162
192
|
|
@@ -164,7 +194,7 @@
|
|
164
194
|
|
165
195
|
def index():
|
166
196
|
|
167
|
-
return render_template('index.html', data=Todo.query.all())
|
197
|
+
return render_template('index.html', data=Todo.query.all())
|
168
198
|
|
169
199
|
```
|
170
200
|
|
@@ -174,123 +204,117 @@
|
|
174
204
|
|
175
205
|
<html>
|
176
206
|
|
177
|
-
|
207
|
+
<head>
|
178
|
-
|
208
|
+
|
179
|
-
|
209
|
+
<title>Todo App</title>
|
180
|
-
|
210
|
+
|
181
|
-
|
211
|
+
<style>
|
182
|
-
|
212
|
+
|
183
|
-
|
213
|
+
.hidden{
|
184
|
-
|
214
|
+
|
185
|
-
|
215
|
+
display: none;
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
</style>
|
220
|
+
|
221
|
+
</head>
|
222
|
+
|
223
|
+
<body>
|
224
|
+
|
225
|
+
<form method="post" action="/todos/create">
|
226
|
+
|
227
|
+
<h4>username</h4>
|
228
|
+
|
229
|
+
<input type= "text" name="username" />
|
230
|
+
|
231
|
+
<h4>texts</h4>
|
232
|
+
|
233
|
+
<input type= "text" name="texts" />
|
234
|
+
|
235
|
+
<input type= "submit" value="Create" />
|
236
|
+
|
237
|
+
</form>
|
238
|
+
|
239
|
+
<div id= "error" class="hidden">Something went wrong!</div>
|
240
|
+
|
241
|
+
<ul>
|
242
|
+
|
243
|
+
{% for d in data %}
|
244
|
+
|
245
|
+
<li>{{d.username}}</li>
|
246
|
+
|
247
|
+
<li>{{d.texts}}</li>
|
248
|
+
|
249
|
+
{% endfor %}
|
250
|
+
|
251
|
+
</ul>
|
252
|
+
|
253
|
+
<script>
|
254
|
+
|
255
|
+
const unameInput = document.getElementById('username');
|
256
|
+
|
257
|
+
const txInput = document.getElementById('texts');
|
258
|
+
|
259
|
+
document.getElementById('form').onsubmit = function(e) {
|
260
|
+
|
261
|
+
e.preventDefault();
|
262
|
+
|
263
|
+
const name = unameInput.value;
|
264
|
+
|
265
|
+
const texts = txInput.value;
|
266
|
+
|
267
|
+
descInput.value = '';
|
268
|
+
|
269
|
+
fetch('/todos/create', {
|
270
|
+
|
271
|
+
method: 'POST',
|
272
|
+
|
273
|
+
body: JSON.stringify({
|
274
|
+
|
275
|
+
'username': username,
|
276
|
+
|
277
|
+
'texts': texts,
|
278
|
+
|
279
|
+
}),
|
280
|
+
|
281
|
+
headers: {
|
282
|
+
|
283
|
+
'Content-Type': 'application/json',
|
284
|
+
|
285
|
+
}
|
286
|
+
|
287
|
+
})
|
288
|
+
|
289
|
+
.then(response => response.json())
|
290
|
+
|
291
|
+
.then(jsonResponse => {
|
292
|
+
|
293
|
+
console.log('response', jsonResponse);
|
294
|
+
|
295
|
+
li = document.createElement('li');
|
296
|
+
|
297
|
+
li.innerText = name;
|
298
|
+
|
299
|
+
li.innerText = city;
|
300
|
+
|
301
|
+
document.getElementById('todos').appendChild(li);
|
302
|
+
|
303
|
+
document.getElementById('error').className = 'hidden';
|
304
|
+
|
305
|
+
})
|
306
|
+
|
307
|
+
.catch(function() {
|
308
|
+
|
309
|
+
document.getElementById('error').className = '';
|
310
|
+
|
311
|
+
})
|
186
312
|
|
187
313
|
}
|
188
314
|
|
189
|
-
</style>
|
190
|
-
|
191
|
-
</head>
|
192
|
-
|
193
|
-
<body>
|
194
|
-
|
195
|
-
<div id="error" class="hidden">Something went wrong!</div>
|
196
|
-
|
197
|
-
<form id="form" method="post" action="/texts/create">
|
198
|
-
|
199
|
-
<input type="text" id="username" name="username">
|
200
|
-
|
201
|
-
<input type="text" id="texts" name="texts" />
|
202
|
-
|
203
|
-
<input type="submit" value="Create" />
|
204
|
-
|
205
|
-
</form>
|
206
|
-
|
207
|
-
<ul id="username">
|
208
|
-
|
209
|
-
{% for d in data %}
|
210
|
-
|
211
|
-
<li>{{d.username}}</li>
|
212
|
-
|
213
|
-
{% endfor %}
|
214
|
-
|
215
|
-
</ul>
|
216
|
-
|
217
|
-
<ul id="texts">
|
218
|
-
|
219
|
-
{% for d in data %}
|
220
|
-
|
221
|
-
<li>{{ d.texts }}</li>
|
222
|
-
|
223
|
-
{% endfor %}
|
224
|
-
|
225
|
-
</ul>
|
226
|
-
|
227
|
-
<script>
|
228
|
-
|
229
|
-
const nmInput = document.getElementById('username')
|
230
|
-
|
231
|
-
const descInput = document.getElementById('texts');
|
232
|
-
|
233
|
-
document.getElementById('form').onsubmit = function(e) {
|
234
|
-
|
235
|
-
e.preventDefault();
|
236
|
-
|
237
|
-
const nm = nameInput.value;
|
238
|
-
|
239
|
-
const desc = descInput.value;
|
240
|
-
|
241
|
-
descInput.value = '';
|
242
|
-
|
243
|
-
fetch('/texts/create', {
|
244
|
-
|
245
|
-
method: 'POST',
|
246
|
-
|
247
|
-
body: JSON.stringify({
|
248
|
-
|
249
|
-
'username':nm,
|
250
|
-
|
251
|
-
'texts': desc,
|
252
|
-
|
253
|
-
}),
|
254
|
-
|
255
|
-
headers: {
|
256
|
-
|
257
|
-
'Content-Type': 'application/json',
|
258
|
-
|
259
|
-
}
|
260
|
-
|
261
|
-
})
|
262
|
-
|
263
|
-
.then(response => response.json())
|
264
|
-
|
265
|
-
.then(jsonResponse => {
|
266
|
-
|
267
|
-
console.log('response', jsonResponse);
|
268
|
-
|
269
|
-
li = document.createElement('li');
|
270
|
-
|
271
|
-
li.innerText = nm;
|
272
|
-
|
273
|
-
li.innerText = desc;
|
274
|
-
|
275
|
-
document.getElementById('usernames').appendChild(li);
|
276
|
-
|
277
|
-
document.getElementById('texts').appendChild(li);
|
278
|
-
|
279
|
-
document.getElementById('error').className = 'hidden';
|
280
|
-
|
281
|
-
})
|
282
|
-
|
283
|
-
.catch(function() {
|
284
|
-
|
285
|
-
document.getElementById('error').className = '';
|
286
|
-
|
287
|
-
})
|
288
|
-
|
289
|
-
}
|
290
|
-
|
291
315
|
</script>
|
292
316
|
|
293
|
-
|
317
|
+
</body>
|
294
318
|
|
295
319
|
</html>
|
296
320
|
|
2
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,6 +10,8 @@
|
|
10
10
|
|
11
11
|
フォームが空欄の時はフラッシュメッセージを使って警告をしてデータベースには格納せず、フォームが空欄でない場合はSQLAlchemyを使ってデータベースに入力されたデータを格納しています。
|
12
12
|
|
13
|
+
|
14
|
+
|
13
15
|
### 発生している問題・エラーメッセージ
|
14
16
|
|
15
17
|
現在以下のコマンドでPythonプログラムを実行すると、HTML画面は問題なく表示されるのですが、入力ボタンを押してもデータがデータベースに格納されず、ウェブブラウザにも表示されない状況です。
|
@@ -118,26 +120,28 @@
|
|
118
120
|
|
119
121
|
|
120
122
|
|
123
|
+
|
124
|
+
|
125
|
+
if username and texts:
|
126
|
+
|
127
|
+
flash("成功", "success")
|
128
|
+
|
129
|
+
db.session.add(todo)
|
130
|
+
|
131
|
+
db.session.commit()
|
132
|
+
|
133
|
+
return jsonify({
|
134
|
+
|
135
|
+
'username':todo.username,
|
136
|
+
|
137
|
+
'texts': todo.texts
|
138
|
+
|
139
|
+
})
|
140
|
+
|
141
|
+
|
142
|
+
|
121
143
|
#ユーザー名、テキストが入力されていなかった時
|
122
144
|
|
123
|
-
if username and texts:
|
124
|
-
|
125
|
-
flash("成功", "success")
|
126
|
-
|
127
|
-
db.session.add(todo)
|
128
|
-
|
129
|
-
db.session.commit()
|
130
|
-
|
131
|
-
return jsonify({
|
132
|
-
|
133
|
-
'username':todo.username,
|
134
|
-
|
135
|
-
'texts': todo.texts
|
136
|
-
|
137
|
-
})
|
138
|
-
|
139
|
-
|
140
|
-
|
141
145
|
elif username == "":
|
142
146
|
|
143
147
|
flash("名前を入力してください", "failed")
|
1
typo
test
CHANGED
File without changes
|
test
CHANGED
@@ -64,7 +64,7 @@
|
|
64
64
|
|
65
65
|
|
66
66
|
|
67
|
-
pp.py
|
67
|
+
app.py
|
68
68
|
|
69
69
|
```python
|
70
70
|
|