回答編集履歴
2
add
answer
CHANGED
|
@@ -25,13 +25,7 @@
|
|
|
25
25
|
{% endfor %}
|
|
26
26
|
|
|
27
27
|
<script>
|
|
28
|
-
var dict =
|
|
29
|
-
{
|
|
30
|
-
|
|
28
|
+
var dict = { "title1:"db1","title2":"db2", "title3":"db3","title4":"db4" }
|
|
31
|
-
"データ2":"PAL",
|
|
32
|
-
"データ3":"NUM",
|
|
33
|
-
"データ4":"BUN"
|
|
34
|
-
}
|
|
35
29
|
|
|
36
30
|
Object.keys(dict).map(function(key,index){
|
|
37
31
|
document.getElementById('t'+index).textContent = key;
|
|
@@ -40,7 +34,7 @@
|
|
|
40
34
|
</script>
|
|
41
35
|
```
|
|
42
36
|
```python
|
|
43
|
-
model_table = {"
|
|
37
|
+
model_table = {"db1",database1,"db2":database2,"db3":database3,"db4":database4}
|
|
44
38
|
|
|
45
39
|
#データインポート処理
|
|
46
40
|
@app.route('/import')
|
1
解決
answer
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
#コンセプト
|
|
2
|
-
FORMのactionタグに追加したURL/IDでFLASKへ飛ばそうとした。
|
|
3
|
-
JSにはソースの可読性を高めるため、タイトルとURL末尾のエンドを書いた
|
|
4
|
-
HTMLのIDタグは
|
|
5
|
-
Flask側はJINJA2のレンダリング時にloop.index0を使い、
|
|
6
|
-
JS側からはMAPのINDEXから生成したもので、紐付けている。
|
|
7
|
-
|
|
1
|
+
最終的にはVue.Jsは使わないことにしました。
|
|
8
2
|
|
|
3
|
+
#解決した方法
|
|
4
|
+
HTMLフォームとFLASKのFORM-MODEL処理について、下記の辞書を使い紐付けを行った。
|
|
5
|
+
|
|
6
|
+
JS
|
|
7
|
+
dict = { "title1:"db1","title2":"db2", "title3":"db3","title4":"db4" }
|
|
8
|
+
|
|
9
|
+
python
|
|
10
|
+
model_table = {"db1",database1,"db2":database2,"db3":database3,"db4":database4}
|
|
11
|
+
|
|
12
|
+
全体的なループカウンタは、Flask側からJINJA2でループを回して、レンダリング時にloop.index0を使いIDタグに命名している。
|
|
13
|
+
DOM完成後にJSでdictのループを回して(MAPのINDEXから生成)してhtmlを書き換えている。
|
|
14
|
+
|
|
9
15
|
```html
|
|
10
16
|
|
|
11
17
|
{% for i in forms %}
|
|
@@ -33,7 +39,43 @@
|
|
|
33
39
|
});
|
|
34
40
|
</script>
|
|
35
41
|
```
|
|
42
|
+
```python
|
|
43
|
+
model_table = {"MAP":database1,"PAL":database2,"BUN":database3,"NUM":database4}
|
|
36
44
|
|
|
45
|
+
#データインポート処理
|
|
46
|
+
@app.route('/import')
|
|
47
|
+
@app.route('/import/<model_name>',methods=["GET", "POST"])
|
|
48
|
+
def data_import(model_name):
|
|
49
|
+
if request.method == 'POST':
|
|
50
|
+
if 'file' not in request.files:
|
|
51
|
+
flash('No file part')
|
|
52
|
+
return redirect(request.url)
|
|
53
|
+
file = request.files['file']
|
|
54
|
+
if file.filename == '':
|
|
55
|
+
flash(name + "ファイルを選択してください。", "failed")
|
|
56
|
+
return redirect(request.url)
|
|
57
|
+
if file and allowed_file(file.filename):
|
|
58
|
+
filename = secure_filename(file.filename)
|
|
59
|
+
filename =os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
|
60
|
+
file.save(filename)
|
|
61
|
+
model = model_table[model_name]
|
|
62
|
+
objects = model.data_import(filename)
|
|
63
|
+
|
|
64
|
+
#new_report = Report(report_name=filename, report_welder_wps_association_id=report_id) #create a database entry with exact filename
|
|
65
|
+
#db.session.add(new_report)
|
|
66
|
+
#db.session.commit()
|
|
67
|
+
|
|
68
|
+
return render_template(
|
|
69
|
+
'table_viewer.html',
|
|
70
|
+
forms = range(4)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
return render_template(
|
|
74
|
+
'import.html',
|
|
75
|
+
forms = [1,2,3,4]
|
|
76
|
+
)
|
|
77
|
+
```
|
|
78
|
+
|
|
37
79
|
#あとがき
|
|
38
80
|
何故ここまで複雑化したのかというと、FLASK側の処理をURLエンドポイントで分岐したいことと、
|
|
39
81
|
JS側をコード変更が容易になるようにしたいため。
|