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

質問編集履歴

5

secret_keyの簡略化

2021/10/30 02:15

投稿

EiAma
EiAma

スコア15

title CHANGED
File without changes
body CHANGED
@@ -32,7 +32,7 @@
32
32
  from wtforms import StringField, PasswordField, SubmitField, BooleanField, SelectField, TextAreaField, IntegerField
33
33
 
34
34
  app = Flask(__name__)
35
- app.config['SECRET_KEY'] = '345678907654324560909345'
35
+ app.config['SECRET_KEY'] = '1234567890'
36
36
 
37
37
 
38
38
  class Submit(FlaskForm):
@@ -78,7 +78,7 @@
78
78
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
79
79
 
80
80
  <!-- Bootstrap CSS -->
81
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
81
+ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" crossorigin="anonymous">
82
82
 
83
83
  {% if title %}
84
84
  <title>{{ title }}</title>
@@ -169,7 +169,7 @@
169
169
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
170
170
 
171
171
  <!-- Bootstrap CSS -->
172
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
172
+ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" crossorigin="anonymous">
173
173
 
174
174
  {% if title %}
175
175
  <title>{{ title }}</title>

4

テストコードの添付

2021/10/30 02:15

投稿

EiAma
EiAma

スコア15

title CHANGED
File without changes
body CHANGED
@@ -15,63 +15,188 @@
15
15
 
16
16
  "3."のデータベースにデータをあげることはできますが、データが入力されたものと一致していない。
17
17
 
18
- ### 該当のソースコード
18
+ ### コード
19
19
 
20
+ ファイル構造
21
+ .
22
+ ├── app.py
23
+ └── templates
24
+ ---├── evaluate.html
25
+ ---└── evaluated.html
26
+
27
+ **app.py**
20
28
  ```python
29
+ from flask import Flask, render_template, url_for, redirect
30
+ from wtforms.validators import DataRequired
31
+ from flask_wtf import FlaskForm
32
+ from wtforms import StringField, PasswordField, SubmitField, BooleanField, SelectField, TextAreaField, IntegerField
21
33
 
34
+ app = Flask(__name__)
35
+ app.config['SECRET_KEY'] = '345678907654324560909345'
36
+
37
+
38
+ class Submit(FlaskForm):
39
+ year = IntegerField('評価年', validators=[DataRequired()], default = 2020)
40
+ submit = SubmitField('登録')
41
+
22
42
  class Evaluation(FlaskForm):
23
43
  value = StringField('評価値', validators=[DataRequired()])
24
44
  message = TextAreaField('コメント', render_kw={'rows': 4})
25
45
 
46
+ @app.route('/', methods=['GET','POST'])
47
+ def home():
26
48
 
27
- for a in range(len(list_of_function_name)):
28
- list_of_form.append(Test())
49
+ form = Submit()
29
50
 
51
+ list_of_form = []
52
+ list_of_function_name = ['Column1', 'Column2', 'Column3']
53
+
54
+ for a in range(len(list_of_function_name)):
30
- # list_of_function_name はデータベースから取って来た評価項目一覧です。
55
+ list_of_form.append(Evaluation())
56
+ print(list_of_form)
57
+
31
- list_of_form_dictionary = dict(zip(list_of_function_name, list_of_form))
58
+ list_of_form_dictionary = dict(zip(list_of_function_name, list_of_form))
59
+
60
+ if form.validate_on_submit():
61
+ for a in range(len(list_of_form)):
62
+ print(print(list_of_form[a].value.data))
63
+ print(print(list_of_form[a].message.data))
64
+
65
+ return render_template('evaluated.html', title = '評価値', list_of_form = list_of_form )
66
+
67
+ return render_template('evaluate.html', title = 'テスト', form = form, evaluation_list = list_of_form_dictionary)
68
+
32
69
  ```
33
70
 
71
+ **evaluate.html**
34
72
  ```HTML
35
- {% for key, evaluation_form in list_of_form_dictionary.items() %}
36
- <h3>{{ key }}</h3>
37
- <div class="form-group">
38
- {{ evaluation_form }}
39
- {{ evaluation_form.value.label(class="form-control-label") }}
40
- {% if evaluation_form.value.errors %}
41
- {{ evaluation_form.value(class="form-control form-control-lg is-invalid") }}
42
- <div class="invalid-feedback">
43
- {% for error in evaluation_form.value.errors %}
44
- <span>エラー</span>
45
- {% endfor %}
73
+ <!DOCTYPE html>
74
+ <html>
75
+ <head>
76
+ <!-- Required meta tags -->
77
+ <meta charset="utf-8">
78
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
79
+
80
+ <!-- Bootstrap CSS -->
81
+ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
82
+
83
+ {% if title %}
84
+ <title>{{ title }}</title>
85
+ {% else %}
86
+ <title>HOME</title>
87
+ {% endif %}
88
+ </head>
89
+ <body>
90
+ <header class="site-header">
91
+ </header>
92
+ <main role="main" class="container">
93
+ <div class="row">
94
+ <div class="col-md-12">
95
+ <div class="container">
96
+ <form method="POST" action="">
97
+ {{ form.hidden_tag() }}
98
+ <fieldset class="form-group">
99
+ <legend class="border-bottom mb-4">評価</legend>
100
+ <div class="form-group">
101
+ {{ form.year.label(class="form-control-label") }}
102
+ {% if form.year.errors %}
103
+ {{ form.year(class="form-control form-control-lg is-invalid") }}
104
+ <div class="invalid-feedback">
105
+ {% for error in form.year.errors %}
106
+ <span>エラー</span>
107
+ {% endfor %}
108
+ </div>
109
+ {% else %}
110
+ {{ form.year(class="form-control form-control-lg") }}
111
+ {% endif %}
112
+ </div>
113
+
114
+ {% for name, evaluation_form in evaluation_list.items() %}
115
+ <h3>{{ name[5:] }}</h3>
116
+ <div class="form-group">
117
+ {{ evaluation_form }}
118
+ {{ evaluation_form.value.label(class="form-control-label") }}
119
+ {% if evaluation_form.value.errors %}
120
+ {{ evaluation_form.value(class="form-control form-control-lg is-invalid") }}
121
+ <div class="invalid-feedback">
122
+ {% for error in evaluation_form.value.errors %}
123
+ <span>エラー</span>
124
+ {% endfor %}
125
+ </div>
126
+ {% else %}
127
+ {{ evaluation_form.value(class="form-control form-control-lg") }}
128
+ {% endif %}
46
129
  </div>
130
+ <div class="form-group">
131
+ {{ evaluation_form.message.label(class="form-control-label") }}
132
+ {% if evaluation_form.message.errors %}
133
+ {{ evaluation_form.message(class="form-control form-control-lg is-invalid") }}
134
+ <div class="invalid-feedback">
135
+ {% for error in evaluation_form.message.errors %}
136
+ <span>エラー</span>
137
+ {% endfor %}
138
+ </div>
47
- {% else %}
139
+ {% else %}
48
- {{ evaluation_form.value(class="form-control form-control-lg") }}
140
+ {{ evaluation_form.message(class="form-control form-control-lg") }}
49
- {% endif %}
141
+ {% endif %}
50
- </div>
142
+ </div>
143
+ {% endfor %}
144
+
145
+
146
+ </fieldset>
147
+
51
148
  <div class="form-group">
52
- {{ evaluation_form.message.label(class="form-control-label") }}
53
- {% if evaluation_form.message.errors %}
54
- {{ evaluation_form.message(class="form-control form-control-lg is-invalid") }}
55
- <div class="invalid-feedback">
56
- {% for error in evaluation_form.message.errors %}
57
- <span>エラー</span>
58
- {% endfor %}
59
- </div>
60
- {% else %}
61
- {{ evaluation_form.message(class="form-control form-control-lg") }}
149
+ {{ form.submit(class="btn btn-outline-info") }}
62
- {% endif %}
63
150
  </div>
64
- {% endfor %}
151
+ </form>
152
+ </div>
153
+ </div>
154
+ </div>
155
+ </main>
156
+ </body>
157
+ </html>
158
+
65
159
  ```
66
160
 
67
- ```python
161
+ **evaluated.html**
68
162
 
163
+ ```HTML
69
- # 例えばlen(list_of_function_name)が3であった時の出力テスト
164
+ <!DOCTYPE html>
165
+ <html>
166
+ <head>
70
- print(list_of_form[0].value.data)
167
+ <!-- Required meta tags -->
71
- print(list_of_form[1].value.data)
168
+ <meta charset="utf-8">
72
- print(list_of_form[2].value.data)
169
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
73
170
 
74
- # なお、異なる値を入力していっても、全てlist_of_form[0].value.dataで出力されるものと同じものが以降のprint文でも出力されてしまう。
171
+ <!-- Bootstrap CSS -->
172
+ <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
173
+
174
+ {% if title %}
175
+ <title>{{ title }}</title>
176
+ {% else %}
177
+ <title>HOME</title>
178
+ {% endif %}
179
+ </head>
180
+ <body>
181
+ <header class="site-header">
182
+ </header>
183
+ <main role="main" class="container">
184
+ <div class="row">
185
+ <div class="col-md-12">
186
+ <div class="container">
187
+ {{ list_of_form }}
188
+ {% for written in list_of_form %}
189
+ <h3>{{ written.value.data }}</h3>
190
+ <h4>{{ written.message.data }}</h4>
191
+ <br>
192
+ {% endfor %}
193
+ </div>
194
+ </div>
195
+ </div>
196
+ </main>
197
+ </body>
198
+ </html>
199
+
75
200
  ```
76
201
 
77
202
  ### 試したこと
@@ -87,4 +212,5 @@
87
212
 
88
213
  Flask==1.1.1
89
214
  Flask-WTF==0.14.2
215
+ WTForms==2.2.1
90
216
  Python 3.7.4

3

表記の訂正

2020/01/09 05:29

投稿

EiAma
EiAma

スコア15

title CHANGED
File without changes
body CHANGED
@@ -9,13 +9,12 @@
9
9
 
10
10
  submitフォームは別のFlaskFormにて定義しています。
11
11
 
12
- ================================================
13
12
  1. FlaskFormの定義
14
13
  2. render_template時にFormを渡す
15
14
  3. validate関数にエラーがない場合はデータベースにあげる
16
- ================================================
17
- 3. のデータベースにデータをあげることはできますが、データが入力されたものと一致していない。
18
15
 
16
+ "3."のデータベースにデータをあげることはできますが、データが入力されたものと一致していない。
17
+
19
18
  ### 該当のソースコード
20
19
 
21
20
  ```python

2

プログラムの流れの記述と疑問箇所の追加

2020/01/08 16:18

投稿

EiAma
EiAma

スコア15

title CHANGED
File without changes
body CHANGED
@@ -9,6 +9,13 @@
9
9
 
10
10
  submitフォームは別のFlaskFormにて定義しています。
11
11
 
12
+ ================================================
13
+ 1. FlaskFormの定義
14
+ 2. render_template時にFormを渡す
15
+ 3. validate関数にエラーがない場合はデータベースにあげる
16
+ ================================================
17
+ 3. のデータベースにデータをあげることはできますが、データが入力されたものと一致していない。
18
+
12
19
  ### 該当のソースコード
13
20
 
14
21
  ```python

1

誤字

2020/01/08 16:17

投稿

EiAma
EiAma

スコア15

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,5 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- ここに質問の内容を詳しく書いてください。
4
3
  Flaskを用いて評価フォームを準備し、入力されたデータをデータベースにあげるという一連の関数を作成中です。
5
4
  同じ型のFlaskFormをfor文で作成し、リストとしてまとめた物を別のリストとzip関数で辞書型にまとめ、render_template時に渡し、HTML上のテンプレートタグにて展開することができました。
6
5