質問編集履歴

3

追加プログラム

2022/06/16 12:11

投稿

tiikk
tiikk

スコア12

test CHANGED
File without changes
test CHANGED
@@ -84,4 +84,41 @@
84
84
  </body>
85
85
  </html>
86
86
  ```
87
+ ```cgi
88
+ #! /home/ユーザーid/anaconda3/bin/python
89
+ from sys import path
90
+ import os
91
+ from wsgiref.handlers import CGIHandler
92
+ from flask_sample import app
93
+ from werkzeug.middleware.proxy_fix import ProxyFix
87
94
 
95
+
96
+ path.insert(0, '/サイト名/portfolio/')
97
+
98
+
99
+ class ProxyFix(object):
100
+ def __init__(self, app):
101
+ self.app = app
102
+
103
+ def __call__(self, environ, start_response):
104
+ # ※要書き換え
105
+ environ['SERVER_NAME'] = "サイト名"
106
+ environ['SERVER_PORT'] = "80"
107
+ environ['REQUEST_METHOD'] = "GET"
108
+ environ['SCRIPT_NAME'] = ""
109
+ if 'PATH_INFO' not in environ:
110
+ environ['PATH_INFO'] = ''
111
+ environ['QUERY_STRING'] = ""
112
+ environ['SERVER_PROTOCOL'] = "HTTP/1.1"
113
+ return self.app(environ, start_response)
114
+
115
+
116
+ if __name__ == '__main__':
117
+ app.wsgi_app = ProxyFix(app.wsgi_app)
118
+ CGIHandler().run(app)
119
+ ```
120
+ ```htaccess
121
+ RewriteEngine On
122
+ RewriteCond %{REQUEST_FILENAME} !-f
123
+ RewriteRule ^(.*)$ /portfolio/index.cgi/$1 [QSA,L]
124
+ ```

2

情報追加

2022/06/16 12:02

投稿

tiikk
tiikk

スコア12

test CHANGED
File without changes
test CHANGED
@@ -3,7 +3,34 @@
3
3
  postでhtmlから送信がうまく出来ていないということでしょうか?
4
4
  pythonのほうではエラー等でていませんでした。
5
5
  教えてください。お願い致します。
6
+ @app.route('/insert')の場合
7
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-06-16/485d49e7-5d88-4f22-829f-e64c3d69f234.png)
6
8
  ```python
9
+ from flask import Flask, render_template, request, redirect, url_for
10
+ from flask_sqlalchemy import SQLAlchemy
11
+
12
+ app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://{user}:{password}@{host}/{db_name}?charset=utf8'.format(**{
13
+ 'user': '',
14
+ 'password': '',
15
+ 'host': '',
16
+ 'db_name': ''
17
+ })
18
+ app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
19
+ app.config['SQLALCHEMY_ECHO'] = False
20
+ # dbの初期化
21
+ db = SQLAlchemy(app)
22
+ class Shohin(db.Model):
23
+ __tablename__ = 'Shohin'
24
+ id = db.Column(db.Integer, primary_key=True)
25
+ name = db.Column(db.Text)
26
+ price = db.Column(db.Integer)
27
+
28
+
29
+ @app.route('/result', methods=['GET'])
30
+ def result():
31
+ datas = Shohin.query.all()
32
+ return render_template("index3.html", lists=datas)
33
+
7
34
  @app.route('/insert', methods=["POST"])
8
35
  def insert():
9
36
  name_txt = request.form['name']
@@ -14,9 +41,34 @@
14
41
  db.session.add(shohin)
15
42
  db.session.commit()
16
43
  return redirect("/portfolio/result")
44
+
45
+
46
+ if __name__ == "__main__":
47
+ app.run(host='0.0.0.0', debug=True)
17
48
  ```
18
49
  ```html
50
+ <body>
51
+ <div>
52
+ <table>
53
+ <thead>
54
+ <tr>
55
+ <th>ID</th>
56
+ <th>商品名</th>
57
+ <th>値段</th>
58
+ </tr>
59
+ </thead>
60
+ <tbody>
61
+ {% for item in lists %}
62
+ <tr>
63
+ <th>ID</th>
64
+ <th>{{item.name}}</th>
65
+ <th>{{item.price}}</th>
66
+ </tr>
67
+ {% endfor %}
68
+ </tbody>
69
+ </table>
70
+ </div>
19
- <form action='insert' method="POST">
71
+ <form action='insert' method="POST">
20
72
  <p><新規登録></p>
21
73
  <label>
22
74
  商品名
@@ -27,6 +79,9 @@
27
79
  <input type="text" name="price">
28
80
  </label>
29
81
  <button type="submit">登録</button>
30
- </form>
82
+ </form>
83
+
84
+ </body>
85
+ </html>
31
86
  ```
32
87
 

1

違った内容変更

2022/06/16 11:24

投稿

tiikk
tiikk

スコア12

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- flaskで作成しています。methods=["POST"]の状態だとうまく動かずにNot foundと出ますがGETにするとうまく動きます。POSTと指定するのが正解だと思うのですがなぜPOSTだと動かないのでしょうか?
1
+ flaskで作成しています。methods=["POST"]の状態だとうまく動かずにNot foundと出ますが何も指定しないとうまく動きます。POSTと指定するのが正解だと思うのですがなぜPOSTだと動かないのでしょうか?
2
2
  curlを使ってPOSTで送信してみるとhtmlが返ってきて正常に動いていると思います。
3
3
  postでhtmlから送信がうまく出来ていないということでしょうか?
4
4
  pythonのほうではエラー等でていませんでした。