我正在使用Flask构建一个字典应用程序,用户可以在其中添加新词,下面的代码是app.py文件,我在POST请求中遇到问题,我在终端上收到的错误如下:
line 45, in add_word
word = request.get_json['word']
TypeError: 'method' object is not subscriptable
下面的代码是app.py文件
from flask import Flask, render_template, url_for, request
from flaskext.mysql import MySQL
import datetime
import pymysql.cursors
import json
app = Flask(__name__)
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_DB'] = 'dictionary'
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
mysql = MySQL(app, cursorclass=pymysql.cursors.DictCursor)
**@app.route('/', methods=['GET', 'POST'])**
def index():
user_response = ''
if request.method == 'POST':
*user_input = request.form['word']*
conn = mysql.get_db()
cur = conn.cursor()
cur.execute('select meaning from word where word=%s', (user_input) )
rv = cur.fetchall()
if (len(rv) > 0):
user_response = rv[0]['meaning']
else:
user_response = 'The word can not be found in this dictionary, please try again with another word'
return render_template('index.html', user_response = user_response)
@app.route('/dashboard')
def dashboard():
conn = mysql.get_db()
cur = conn.cursor()
cur.execute('select * from word')
rv = cur.fetchall()
cur.close()
return render_template('dashboard.html', words=rv)
**@app.route('/word', methods=['POST'])
def add_word():
req = request.get_json()
word = request.get_json['word']
meaning = request.get_json['meaning']
conn = mysql.get_db()
cur = conn.cursor()
cur.execute('insert into word(word, meaning) VALUES (%s, %s)', (word, meaning))
conn.commit()
cur.close()
return json.dumps('success')
if __name__ == "__main__":
app.run(debug=True)
这是我的仪表板页面或路线:
{% extends 'base.html' %}
{% block title %}
<title>Dictionary App - Dashboard</title>
{% endblock %}
{% block dash_active %}
class="active"
{% endblock %}
{% block content %}
<div class="row">
<h2>Word Index</h2>
<div class="col-md-2 sidenav">
<a href="#" class="side-active" id="word-index">All Words</a>
<a href="#" id="word-add">Add New</a>
<div>
<form action="javascript:0" id="word-form">
<div class="form-group">
<label for="word"> Word:</label>
<input type="text"
class="form-control"
name="word" id="word"
placeholder="Type in the word here: "
required>
</div>
<div class="form-group">
<label for="meaning"> Meaning: </label>
<textarea class="form-control" id="meaning" placeholder="Type the meaning of the word shows here:" required></textarea>
</div>
<button type="submit" class="btn btn-primary btn-block btn-lg" id="submit">Submit</button>
<button type="button" class="btn btn-warning btn-block btn-lg" id="cancel">Cancel</button>
</form>
</div>
</div>
<div class="col-md-10 main">
<table class="table">
<thead>
<tr>
<th>SN</th>
<th>Word</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
{% for word in words %}
<tr>
<td>{{loop.index}}</td>
<td>{{word['word']}}</td>
<td>{{word['meaning']}}</td>
</tr>
{% else %}
<tr>
<td colspan="3">The Dictionary has no words at the moment</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endblock %}
get_json
是实际的方法,正如错误所说,它不可下标(即不支持[]
语法)。您需要使用括号(()
)调用它,然后在返回值下标您已经将返回值保存到
req
,现在只需使用它:相关问题 更多 >
编程相关推荐