質問編集履歴

2

記載内容の改善

2023/02/21 07:16

投稿

toru122
toru122

スコア3

test CHANGED
File without changes
test CHANGED
@@ -44,7 +44,7 @@
44
44
  def api():
45
45
  REQUESTS_URL = 'https://app.rakuten.co.jp/services/api/Recipe/CategoryRanking/20170426'
46
46
 
47
- APP_ID = '1081377137595442173'
47
+ APP_ID = ''
48
48
 
49
49
  # 簡単豚肉料理に絞った範囲
50
50
  item_parameters = {

1

記載内容の改善

2023/02/21 07:16

投稿

toru122
toru122

スコア3

test CHANGED
File without changes
test CHANGED
@@ -6,12 +6,15 @@
6
6
  楽天レシピカテゴリ別ランキングAPIというものを使いウェブアプリを作ろうとしています。ボタンを押すとランダムに今日のご飯メニューを提示してくれるといったものを想像しています。(今回は豚肉料理に絞りたいです。)
7
7
  flaskでできるかなと思い考えて行っていたのですが、ボタンを押した後画面を転移させ、apiをたたき画面に反映させるのかがわからずにいます。htmlのformタグを使いpythonのファイルと紐づけさせたらいいのではと思っているのですが、書き方がわからず困っています。ちがうのでしょうか。randomモジュールもどこにどのように書けばよいのかわかりません。どなたか教えていただけないでしょうか。
8
8
 
9
+ 2・21 編集後
10
+
9
11
 
10
12
 
11
13
  ### 該当のソースコード views.py
12
14
 
13
15
  ```ここに言語名を入力
14
16
  from flask import Flask, request, render_template
17
+ import main
15
18
 
16
19
  # アプリ生成
17
20
  app = Flask(__name__)
@@ -21,11 +24,12 @@
21
24
  def form():
22
25
  return render_template('form.html')
23
26
 
24
- @app.route("/summon.html", methods=['GET', 'POST'])
27
+ @app.route("/summon", methods=['GET', 'POST'])
25
28
  def summon():
26
29
  if request.method == 'POST':
30
+ api = main.api()
27
- return render_template('summon.html')
31
+ return render_template('summon.html', data=api)
28
-
32
+
29
33
  if __name__ == "__main__":
30
34
  app.run(debug=True)
31
35
 
@@ -35,45 +39,30 @@
35
39
 
36
40
  ```ここに言語名を入力
37
41
  import requests
38
- import random
39
42
 
40
43
  # urlの作成
44
+ def api():
41
- REQUESTS_URL = 'https://app.rakuten.co.jp/services/api/Recipe/CategoryRanking/20170426'
45
+ REQUESTS_URL = 'https://app.rakuten.co.jp/services/api/Recipe/CategoryRanking/20170426'
42
46
 
43
- APP_ID = ''
47
+ APP_ID = '1081377137595442173'
44
48
 
45
- # 簡単豚肉料理に絞った範囲
49
+ # 簡単豚肉料理に絞った範囲
46
- item_parameters = {
50
+ item_parameters = {
47
- 'applicationId': APP_ID,
51
+ 'applicationId': APP_ID,
48
- 'format':'json',
52
+ 'format':'json',
49
- 'formatVersion': 2,
53
+ 'formatVersion': 2,
50
- 'categoryId': '36-494',
54
+ 'categoryId': '36-494',
51
- 'elements': 'recipeTitle,recipeUrl,mediumImageUrl,recipeIndication'
55
+ 'elements': 'recipeTitle,recipeUrl,mediumImageUrl,recipeIndication'
52
- }
56
+ }
53
57
 
54
- # jsonデータの取得
58
+ # jsonデータの取得
59
+
55
60
  res = requests.get(REQUESTS_URL, item_parameters)
56
61
  result = res.json()
62
+ return result
57
63
 
58
64
  ```
59
65
 
60
- ### 該当のソースコード base.html
61
-
62
- ```ここに言語名を入力
63
- <!DOCTYPE html>
64
- <html lang="ja">
65
- <head>
66
- <meta charset="UTF-8">
67
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
68
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
69
- <title>アプリ</title>
70
- </head>
71
- <body>
72
- {% block content %}
73
- {% endblock %}
74
- </body>
75
- </html>
76
- ```
77
66
 
78
67
  ### 該当のソースコード form.html
79
68
 
@@ -84,19 +73,15 @@
84
73
  <meta charset="UTF-8">
85
74
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
86
75
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
87
- <title>ランダムレシピ</title>
76
+ <title></title>
88
77
  </head>
89
78
  <body>
90
- <h1>豚肉料理</h1>
79
+ <h1></h1>
91
80
  <br>
92
- {% block content %}
81
+
93
82
  <form action="/summon.html" method='POST'>
94
83
  <h3>押してください</h3>
95
84
  <button type="submit">開始</button>
96
- </form>
97
- {% endblock %}
98
- </body>
99
- </html>
100
85
  ```
101
86
 
102
87
  ### 該当のソースコード summon.html
@@ -111,11 +96,25 @@
111
96
  <title>Document</title>
112
97
  </head>
113
98
  <body>
99
+
114
100
  <h2>今日の豚肉料理は</h2>
115
-
101
+ {{ data }}
102
+
116
103
  </body>
117
104
  </html>
118
105
  ```
106
+
107
+ になります。
108
+ 現状では
109
+ ボタンをクリックするとsummon.htmlに遷移したのち
110
+ Not Found
111
+ The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.と表示されます。
112
+
113
+ views.py に main.py をimportするとこのように出ます。
114
+ importしなければ通常に遷移します。
115
+
116
+ 初歩的な部分で詰まっているのかもしれませんが、ご指摘いただけると幸いです。
117
+ よろしくお願いいたします。
119
118
 
120
119
 
121
120
 
@@ -123,3 +122,6 @@
123
122
 
124
123
 
125
124
 
125
+
126
+
127
+