I am building a dictionary app with Flask where users can add new words, the code below is the app.py file, I am having issues with the POST request, the error I am receiving on my terminal is this:
JavaScript
x
4
1
line 45, in add_word
2
word = request.get_json['word']
3
TypeError: 'method' object is not subscriptable
4
This code below is the app.py file
JavaScript
1
56
56
1
from flask import Flask, render_template, url_for, request
2
from flaskext.mysql import MySQL
3
import datetime
4
import pymysql.cursors
5
import json
6
7
app = Flask(__name__)
8
9
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
10
app.config['MYSQL_DATABASE_DB'] = 'dictionary'
11
app.config['MYSQL_DATABASE_USER'] = 'root'
12
app.config['MYSQL_DATABASE_PASSWORD'] = ''
13
mysql = MySQL(app, cursorclass=pymysql.cursors.DictCursor)
14
15
**@app.route('/', methods=['GET', 'POST'])**
16
def index():
17
user_response = ''
18
if request.method == 'POST':
19
*user_input = request.form['word']*
20
conn = mysql.get_db()
21
cur = conn.cursor()
22
cur.execute('select meaning from word where word=%s', (user_input) )
23
rv = cur.fetchall()
24
if (len(rv) > 0):
25
user_response = rv[0]['meaning']
26
else:
27
user_response = 'The word can not be found in this dictionary, please try again with another word'
28
29
return render_template('index.html', user_response = user_response)
30
31
@app.route('/dashboard')
32
def dashboard():
33
conn = mysql.get_db()
34
cur = conn.cursor()
35
cur.execute('select * from word')
36
rv = cur.fetchall()
37
cur.close()
38
39
return render_template('dashboard.html', words=rv)
40
41
**@app.route('/word', methods=['POST'])
42
def add_word():
43
req = request.get_json()
44
word = request.get_json['word']
45
meaning = request.get_json['meaning']
46
conn = mysql.get_db()
47
cur = conn.cursor()
48
cur.execute('insert into word(word, meaning) VALUES (%s, %s)', (word, meaning))
49
conn.commit()
50
cur.close()
51
52
return json.dumps('success')
53
54
if __name__ == "__main__":
55
app.run(debug=True)
56
here is my dashboard page or route:
JavaScript
1
66
66
1
{% extends 'base.html' %}
2
3
{% block title %}
4
<title>Dictionary App - Dashboard</title>
5
{% endblock %}
6
{% block dash_active %}
7
class="active"
8
{% endblock %}
9
10
{% block content %}
11
<div class="row">
12
<h2>Word Index</h2>
13
<div class="col-md-2 sidenav">
14
<a href="#" class="side-active" id="word-index">All Words</a>
15
<a href="#" id="word-add">Add New</a>
16
<div>
17
<form action="javascript:0" id="word-form">
18
<div class="form-group">
19
<label for="word"> Word:</label>
20
<input type="text"
21
class="form-control"
22
name="word" id="word"
23
placeholder="Type in the word here: "
24
required>
25
</div>
26
<div class="form-group">
27
<label for="meaning"> Meaning: </label>
28
<textarea class="form-control" id="meaning" placeholder="Type the meaning of the word shows here:" required></textarea>
29
</div>
30
<button type="submit" class="btn btn-primary btn-block btn-lg" id="submit">Submit</button>
31
<button type="button" class="btn btn-warning btn-block btn-lg" id="cancel">Cancel</button>
32
33
34
</form>
35
</div>
36
37
</div>
38
<div class="col-md-10 main">
39
<table class="table">
40
<thead>
41
<tr>
42
<th>SN</th>
43
<th>Word</th>
44
<th>Meaning</th>
45
</tr>
46
</thead>
47
<tbody>
48
{% for word in words %}
49
<tr>
50
<td>{{loop.index}}</td>
51
<td>{{word['word']}}</td>
52
<td>{{word['meaning']}}</td>
53
</tr>
54
{% else %}
55
<tr>
56
<td colspan="3">The Dictionary has no words at the moment</td>
57
</tr>
58
{% endfor %}
59
</tbody>
60
</table>
61
62
</div>
63
</div>
64
65
{% endblock %}
66
Advertisement
Answer
get_json
is the actual method, which, as the error says, is not subscribable (i.e., doesn’t support the []
syntax). You need to call it using parenthesis (()
), and then subscript the return value.
You already saved the return value to req
, now you just need to use it:
JavaScript
1
4
1
req = request.get_json()
2
word = req['word']
3
meaning = req['meaning']
4