質問編集履歴

7

コードの修正

2021/01/01 03:20

投稿

haruchan__
haruchan__

スコア7

test CHANGED
File without changes
test CHANGED
@@ -52,10 +52,6 @@
52
52
 
53
53
  ```work13.py
54
54
 
55
- work13.py
56
-
57
-
58
-
59
55
  import mysql.connector as mydb
60
56
 
61
57
  import pandas as pd
@@ -84,7 +80,7 @@
84
80
 
85
81
 
86
82
 
87
- sql = "select shimei from shain"
83
+ sql = "select * from shain"
88
84
 
89
85
 
90
86
 
@@ -118,39 +114,53 @@
118
114
 
119
115
  def search():
120
116
 
117
+ if request.method == 'GET':
118
+
119
+ #posts = Post.query.all()
120
+
121
+ return render_template('search.html')
122
+
123
+
124
+
125
+ else:
126
+
121
- search_name = request.form.get('keyword')
127
+ search_name = request.form.get('keyword')
122
-
128
+
123
- n = 0
129
+ n = 0
124
-
130
+
125
- result = df_shain_list[df_shain_list['shimei'].str.contains(search_name)] #search_nameを含んだデータを抽出
131
+ result = df_shain_list[df_shain_list['shimei'].str.contains(search_name)] #search_nameを含んだデータを抽出
126
-
132
+
127
- result_shimei = result['shimei']
133
+ result_shimei = result['shimei']
128
-
134
+
129
- for index, row in result.iterrows():
135
+ for index, row in result.iterrows():
130
-
136
+
131
- result_shimei = row['shimei']
137
+ result_shimei = row['shimei']
132
-
138
+
133
- n = n + 1
139
+ n = n + 1
134
-
140
+
135
- if search_name == '': #何も入力されていない場合
141
+ if search_name == '': #何も入力されていない場合
136
-
142
+
137
- result_word = (f'全社員の人数は{n}人です。')
143
+ result_word = (f'全社員の人数は{n}人です。')
138
-
144
+
139
- elif n > 0: #該当者がいる場合
145
+ elif n > 0: #該当者がいる場合
140
-
146
+
141
- result_word = (f'名前に「{search_name}」が含まれるのは{n}人です。')
147
+ result_word = (f'名前に「{search_name}」が含まれるのは{n}人です。')
142
-
148
+
143
- elif n == 0: #該当者がいない場合
149
+ elif n == 0: #該当者がいない場合
144
-
150
+
145
- result_word = (f'名前に「{search_name}」が含まれる人はいませんでした。')
151
+ result_word = (f'名前に「{search_name}」が含まれる人はいませんでした。')
152
+
153
+
154
+
155
+ print(result_word)
146
156
 
147
157
 
148
158
 
149
- #due = datetime.strptime(due, '%Y-%m-%d')
159
+ #due = datetime.strptime(due, '%Y-%m-%d')
150
-
160
+
151
- #new_post = Post(title=title, detail=detail, due=due)
161
+ #new_post = Post(title=title, detail=detail, due=due)
152
-
162
+
153
- return render_template('result.html', result_word = search_result)
163
+ return render_template('result.html', result_word = result_word)
154
164
 
155
165
 
156
166
 

6

コードの修正

2021/01/01 03:20

投稿

haruchan__
haruchan__

スコア7

test CHANGED
File without changes
test CHANGED
@@ -118,7 +118,39 @@
118
118
 
119
119
  def search():
120
120
 
121
+ search_name = request.form.get('keyword')
122
+
123
+ n = 0
124
+
125
+ result = df_shain_list[df_shain_list['shimei'].str.contains(search_name)] #search_nameを含んだデータを抽出
126
+
127
+ result_shimei = result['shimei']
128
+
129
+ for index, row in result.iterrows():
130
+
131
+ result_shimei = row['shimei']
132
+
133
+ n = n + 1
134
+
135
+ if search_name == '': #何も入力されていない場合
136
+
137
+ result_word = (f'全社員の人数は{n}人です。')
138
+
139
+ elif n > 0: #該当者がいる場合
140
+
141
+ result_word = (f'名前に「{search_name}」が含まれるのは{n}人です。')
142
+
143
+ elif n == 0: #該当者がいない場合
144
+
145
+ result_word = (f'名前に「{search_name}」が含まれる人はいませんでした。')
146
+
147
+
148
+
149
+ #due = datetime.strptime(due, '%Y-%m-%d')
150
+
151
+ #new_post = Post(title=title, detail=detail, due=due)
152
+
121
- return render_template('search.html')
153
+ return render_template('result.html', result_word = search_result)
122
154
 
123
155
 
124
156
 
@@ -126,41 +158,7 @@
126
158
 
127
159
  def result():
128
160
 
129
- if request.method == 'post':
130
-
131
- search_name = request.form.get('keyword')
132
-
133
- n = 0
134
-
135
- result = df_shain_list[df_shain_list['shimei'].str.contains(search_name)] #search_nameを含んだデータを抽出
136
-
137
- result_shimei = result['shimei']
138
-
139
- for index, row in result.iterrows():
140
-
141
- result_shimei = row['shimei']
142
-
143
- n = n + 1
144
-
145
- if search_name == '': #何も入力されていない場合
146
-
147
- result_word = (f'全社員の人数は{n}人です。')
148
-
149
- elif n > 0: #該当者がいる場合
150
-
151
- result_word = (f'名前に「{search_name}」が含まれるのは{n}人です。')
152
-
153
- elif n == 0: #該当者がいない場合
154
-
155
- result_word = (f'名前に「{search_name}」が含まれる人はいませんでした。')
156
-
157
-
158
-
159
- #due = datetime.strptime(due, '%Y-%m-%d')
160
-
161
- #new_post = Post(title=title, detail=detail, due=due)
162
-
163
- return render_template('result.html', result_word = search_result)
161
+ return render_template('result.html')
164
162
 
165
163
 
166
164
 

5

コードの修正

2021/01/01 03:18

投稿

haruchan__
haruchan__

スコア7

test CHANGED
File without changes
test CHANGED
@@ -96,14 +96,34 @@
96
96
 
97
97
  app = Flask(__name__)
98
98
 
99
-
99
+ """
100
+
101
+ app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://{user}:{password}@{host}/{db_name}?charset=utf8'.format(**{
102
+
103
+ 'user': 'root',
104
+
105
+ 'password': 'root',
106
+
107
+ 'host': '3306',
108
+
109
+ 'db_name': 'shain'
110
+
111
+ })
112
+
113
+ """
100
114
 
101
115
  #db = SQLAlchemy(app)
102
116
 
103
-
104
-
105
117
  @app.route('/', methods=['GET', 'POST'])
106
118
 
119
+ def search():
120
+
121
+ return render_template('search.html')
122
+
123
+
124
+
125
+ @app.route('/result', methods=['GET', 'POST'])
126
+
107
127
  def result():
108
128
 
109
129
  if request.method == 'post':

4

コードの修正

2021/01/01 03:17

投稿

haruchan__
haruchan__

スコア7

test CHANGED
File without changes
test CHANGED
@@ -102,7 +102,7 @@
102
102
 
103
103
 
104
104
 
105
- @app.route('/result', methods=['GET', 'POST'])
105
+ @app.route('/', methods=['GET', 'POST'])
106
106
 
107
107
  def result():
108
108
 

3

コードの修正

2021/01/01 03:13

投稿

haruchan__
haruchan__

スコア7

test CHANGED
File without changes
test CHANGED
@@ -100,45 +100,7 @@
100
100
 
101
101
  #db = SQLAlchemy(app)
102
102
 
103
- @app.route('/', methods=['GET', 'POST'])
103
+
104
-
105
- def search():
106
-
107
- search_name = request.form.get('keyword')
108
-
109
- n = 0
110
-
111
- result = df_shain_list[df_shain_list['shimei'].str.contains(search_name)] #search_nameを含んだデータを抽出
112
-
113
- result_shimei = result['shimei']
114
-
115
- for index, row in result.iterrows():
116
-
117
- result_shimei = row['shimei']
118
-
119
- n = n + 1
120
-
121
- if search_name == '': #何も入力されていない場合
122
-
123
- result_word = (f'全社員の人数は{n}人です。')
124
-
125
- elif n > 0: #該当者がいる場合
126
-
127
- result_word = (f'名前に「{search_name}」が含まれるのは{n}人です。')
128
-
129
- elif n == 0: #該当者がいない場合
130
-
131
- result_word = (f'名前に「{search_name}」が含まれる人はいませんでした。')
132
-
133
-
134
-
135
- #due = datetime.strptime(due, '%Y-%m-%d')
136
-
137
- #new_post = Post(title=title, detail=detail, due=due)
138
-
139
- return render_template('result.html', result_word = search_result)
140
-
141
-
142
104
 
143
105
  @app.route('/result', methods=['GET', 'POST'])
144
106
 

2

コードの修正

2021/01/01 02:26

投稿

haruchan__
haruchan__

スコア7

test CHANGED
File without changes
test CHANGED
@@ -192,6 +192,8 @@
192
192
 
193
193
  ```エラーの原因かと思われる場所
194
194
 
195
+ エラーの原因かと思われる場所
196
+
195
197
  result = df_shain_list[df_shain_list['shimei'].str.contains(search_name)] #search_nameを含んだデータを抽出
196
198
 
197
199
  ```

1

環境の追記

2021/01/01 02:18

投稿

haruchan__
haruchan__

スコア7

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,7 @@
1
1
  pythonとmysqlを使用して簡単なデータベースの中を検索できるwebアプリケーションを作成使用としています。そのなかで、下記のようなエラーが発生してhtmlが表示されなくなり困っています。
2
2
 
3
+
4
+
3
5
  例 htmlにて検索した文字列を入力
4
6
 
5
7
           ↓
@@ -12,8 +14,20 @@
12
14
 
13
15
 
14
16
 
17
+ 環境
18
+
19
+ 使用言語:python 3.9.1
20
+
21
+ データベース:mysql 5.6.x
22
+
23
+ フレームワーク:flask
24
+
25
+
26
+
15
27
  ```エラーコード
16
28
 
29
+ エラーコード
30
+
17
31
  TypeError: first argument must be string or compiled pattern
18
32
 
19
33
  ```
@@ -38,6 +52,10 @@
38
52
 
39
53
  ```work13.py
40
54
 
55
+ work13.py
56
+
57
+
58
+
41
59
  import mysql.connector as mydb
42
60
 
43
61
  import pandas as pd
@@ -182,6 +200,10 @@
182
200
 
183
201
  ```resul.html
184
202
 
203
+ resul.html
204
+
205
+
206
+
185
207
  {% extends 'base.html' %}
186
208
 
187
209
 
@@ -198,6 +220,10 @@
198
220
 
199
221
  ```search.html
200
222
 
223
+ search.html
224
+
225
+
226
+
201
227
  {% extends 'base.html' %}
202
228
 
203
229
 
@@ -214,6 +240,10 @@
214
240
 
215
241
  ```base.html
216
242
 
243
+ base.html
244
+
245
+
246
+
217
247
  <!DOCTYPE html>
218
248
 
219
249
  <html lang="ja">