質問するログイン新規登録

質問編集履歴

1

vue追加

2020/10/29 01:24

投稿

yuujiMotoki
yuujiMotoki

スコア90

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,21 @@
1
+ #更新しました
2
+  
3
+ VUE側を書きました。
4
+ - HTMLのファイルフォームからPOSTして、FlaskFormで処理したい
5
+ - JS側のPOSTには、AJAX使いたくない(可読性がない)
6
+ - 将来的にも、FLASK側はライブラリで処理したい
7
+
8
+ POSTの中身をみても、``data:None``で受け取れていない
9
+
10
+ VUE側でのバインディングが良く分からない
11
+
12
+ ```
13
+ (Pdb) vars(number_form.file)
14
+ {'meta': <wtforms.form.Meta object at 0x000001EA7762A0D0>, 'default': None, 'description': '', 'render_kw': None, 'filters': (), 'flags': <wtforms.fields.Flags: {}>, 'name': 'file', 'short_name': 'file', 'type': 'FileField', 'validators': (), 'id': 'file', 'label': Label('file', 'File'), 'process_errors': [], 'object_data': None, 'data': None, 'raw_data': []}
15
+ ```
16
+
17
+
18
+
1
19
  #内容
2
20
   フォームで3つのファイルをPOST送信するコードを書いています。
3
21
   バックエンドはFlaskで書いています。
@@ -22,47 +40,77 @@
22
40
 
23
41
  ```javascript
24
42
 
25
- {% extends "layout.html" %}
43
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>
26
- {% block body %}
27
44
 
45
+ <!-- vue.js form -->
46
+ <div id="app">
28
47
  <div id="content">
29
48
  <div class="container-fluid">
30
- <h1>データ1の読込</h1>
31
- <p>
49
+ <template
32
- <form class="form form-horizontal" method="POST" action="{{url_for('data_import')}}" enctype="multipart/form-data">
33
- {{ form1.hidden_tag() }}
34
- {{ form1.file }}
35
- <input type="submit" >
50
+ title="ファイル1の読込"
51
+ form = {{form1}}     <!-- Flask側から form1 をレンダリング -->
52
+ >
36
- </form>
53
+ </template>
54
+ <template
55
+ title="ファイル2の読込"
56
+ form = {{form2}}     <!-- Flask側から form2 をレンダリング -->
57
+ >
58
+ </template>
59
+ <template
60
+ title="ファイル3の読込"   <!-- Flask側から form3 をレンダリング -->
61
+ form = {{form3}}
62
+ >
63
+ </template>
37
- </p>
64
+ </div>
38
- <hr>
39
- <h1>データ2の読込</h1>
40
- <p>
41
- <form class="form form-horizontal" method="POST" action="{{url_for('data_import')}}" enctype="multipart/form-data">
42
- {{ form2.hidden_tag() }}
43
- {{ form2.file }}
44
- <input type="submit" >
45
- </form>
46
- </p>
65
+ </div>
47
- <hr>
66
+ </div>
48
67
 
68
+ ```
69
+ ```javascript
70
+
71
+ {% raw %}
72
+ <script type="text/x-template" id="template">
49
- <h1>データ3の読込</h1>
73
+ <h1>{{title}}</h1>
50
- <p>
51
74
  <form class="form form-horizontal" method="POST" action="{{url_for('data_import')}}" enctype="multipart/form-data">
75
+ <input type="file" name="filename">
52
- {{ form3.hidden_tag() }}
76
+ {{ this.form.hidden_tag }}
53
- {{ form3.file }}
54
77
  <input type="submit" >
55
78
  </form>
56
- </p>
57
79
  <hr>
58
- </div>
80
+ </script>
59
- </div>
81
+ {% endraw %}
60
82
 
61
- {% endblock %}
83
+ <script>
62
84
 
85
+ Vue.component('template', {
86
+ template: '#template',
87
+ props: {
88
+ title : String,
89
+ form : Object,
90
+ },
91
+ data: function(){
92
+ return {
93
+ title : "",
94
+ hidden_tag:""
95
+ };
96
+ }
97
+ }
98
+ )
99
+
100
+ // Vue
101
+ new Vue({
102
+ el: '#app'
103
+ });
104
+
105
+ </script>
63
106
  ```
107
+ ```python
64
108
 
109
+ from flask_wtf import FlaskForm
110
+
111
+ class UploadForm(FlaskForm):
65
- ```python
112
+ file = FileField()
113
+
66
114
  #データインポート処理
67
115
  @app.route('/import',methods=["GET", "POST"])
68
116
  def data_import():