質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

3453閲覧

ボタンを押してデータの書き換えをしたい

kamakiri

総合スコア13

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2017/05/05 11:05

https://teratail.com/questions/73935
これの続きです。

評価されてない画像データを1つだけ表示して、ボタンで画像の種類を分類したいです。

python

1class Image(db.Model): 2 __tablename__ = 'images' 3 id = db.Column(db.Integer, primary_key=True) 4 name = db.Column(db.String(100), default='', nullable=False) 5 good = db.Column(db.Integer) 6 normal = db.Column(db.Integer) 7 bad = db.Column(db.Integer)

html

1{% extends "layout.html" %} 2<div class=image> 3{% block body %} 4 5 {% if session.user_id %} 6 {{ message }} 7 <img src={{ image.name }}> 8 9 #どうやって、何の要素を渡せばいいのだ・・・ 10 <form action="{{ url_for('classify_images', image_id=image.id) }}" method=post class=classify-images> 11 <button type=submit class=good>GOOD</button> 12 <button type=submit class=normal>NORMAL</button> 13 <button type=submit class=bad>BAD</button> 14 </form> 15 {% endif %} 16 17{% endblock %} 18</div>

python

1@app.route('/image/', methods=['GET']) 2@login_required 3def show_image(): 4 images = Image.query.all() 5 for image in images: 6 #評価されてない画像を出す、これでいいのか? 7 if image.good is None: 8 return render_template('classify_images.html', image=image) 9 else: 10 flash('All images are classified') 11 return render_template('classify_images.html', message="mou de-ta naidesu") 12 13 14@app.route('/classify', methods=['GET','POST']) 15@login_required 16def classify_images(image_id): 17 image = Image.query.get(image_id) 18 ''' 19 ここら辺がグダッてる、よくわからん。GOODのボタンが押されたらgoodに1を他のには0を、 20 NORMALのボタンが押されたらnormalに1を他のには0をみたいな感じでやりたい 21 こういう感じでやりたいです・・・ 22 ''' 23 if request.form == ['good']: 24 image.good = 1 25 image.normal = 0 26 image.bad = 0 27 db.session.add(image) 28 db.session.commit() 29 flash('New classification was successfully posted') 30 return redirect(url_for('show_image'))

よろしくお願いします・・・・

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

http://stackoverflow.com/questions/8552675/form-sending-error-flask
これ参考にしました

html

1{% extends "layout.html" %} 2<div class=image> 3 {% block body %} 4 5 {% if session.user_id %} 6 {{ message }} 7 <img src={{ image.name }}> 8 {{ image.id }} 9 {{ image.name }} 10 {{ image.good }} 11 {{ image.normal }} 12 {{ image.bad }} 13 <form action="{{ url_for('classify_images', image_id=image.id) }}" method=post class=classify-images> 14 <button type=submit name=classify value=good>GOOD</button> 15 <button type=submit name=classify value=normal>NORMAL</button> 16 <button type=submit name=classify value=bad>BAD</button> 17 </form> 18 {% endif %} 19 20 {% endblock %} 21</div>

python

1@app.route('/image/<int:image_id>/classify/', methods=['GET','POST']) 2@login_required 3def classify_images(image_id): 4 image = Image.query.get(image_id) 5 image.good = 0 6 image.normal = 0 7 image.bad = 0 8 if request.form['classify'] == 'good': 9 image.good = 1 10 elif request.form['classify'] == 'normal': 11 image.normal = 1 12 elif request.form['classify'] == 'bad': 13 image.bad = 1 14 db.session.add(image) 15 db.session.commit() 16 flash('New classification was successfully posted') 17 return redirect(url_for('show_image'))

解決!

投稿2017/05/05 14:12

編集2017/05/05 14:15
kamakiri

総合スコア13

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

html

1 <form action="{{ url_for('classify_images', image_id=image.id) }}" method=post class=classify-images> 2 <button type=submit value=good>GOOD</button> 3 <button type=submit value=normal>NORMAL</button> 4 <button type=submit value=bad>BAD</button> 5 </form>

python

1@app.route('/classify', methods=['GET', 'POST']) 2def classify_images(image_id): 3 image = Image.query.get(image_id) 4 if request.form['good']: 5 image.good = 1 6 image.normal = 0 7 image.bad = 0 8 if request.form['normal']: 9 image.good = 0 10 image.normal = 1 11 image.bad = 0 12 if request.form['bad']: 13 image.good = 0 14 image.normal = 0 15 image.bad = 1 16 db.session.add(image) 17 db.session.commit() 18 flash('New classification was successfully posted') 19 return redirect(url_for('show_image')) 20

こんな感じか・・・?

python

1@app.route('/classify', methods=['GET','POST']) 2@login_required 3def classify_images(image_id): 4 image = Image.query.get(image_id) 5 image.good = 0 6 image.normal = 0 7 image.bad = 0 8 if request.form['good']: 9 image.good = 1 10 if request.form['normal']: 11 image.normal = 1 12 if request.form['bad']: 13 image.bad = 1 14 db.session.add(image) 15 db.session.commit() 16 flash('New classification was successfully posted') 17 return redirect(url_for('show_image'))

こうかな・・・?

↑をこうした

python

1@app.route('/image/<int:image_id>/classify', methods=['GET','POST']) 2@login_required 3def classify_images(image_id): 4 image = Image.query.get(image_id) 5 image.good = 0 6 image.normal = 0 7 image.bad = 0 8 if request.form['good']: 9 image.good = 1 10 if request.form['normal']: 11 image.normal = 1 12 if request.form['bad']: 13 image.bad = 1 14 db.session.add(image) 15 db.session.commit() 16 flash('New classification was successfully posted') 17 return redirect(url_for('show_image'))

そしたら、

BAD Request
The browser (or proxy) sent a request that this server could not understand.

こうなった

この時点でhtmlのmethodがmethoになってたっぽい・・・
細かい所気をつけよう

投稿2017/05/05 11:58

編集2017/05/05 14:14
kamakiri

総合スコア13

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kamakiri

2017/05/05 11:59

if request.form['good']: が if request.form == ['good']: になってた
kamakiri

2017/05/05 12:24

TypeError: classify_images() takes exactly 1 argument (0 given) ん〜・・・・
kamakiri

2017/05/05 12:36 編集

File "/Users/orenonamae/image_classify/flask_tutorial/tutorial/flaskr/views.py", line 17, in decorated_view return f(*args, **kwargs) TypeError: classify_images() takes exactly 1 argument (0 given) '''python def login_required(f): @wraps(f) def decorated_view(*args, **kwargs): if g.user is None: return redirect(url_for('login', next=request.path)) return f(*args, **kwargs) return decorated_view ''' これ・・・?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問