Issue
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:
line 45, in add_word
word = request.get_json['word']
TypeError: 'method' object is not subscriptable
This code below is the app.py file
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)
here is my dashboard page or route:
{% 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 %}
Solution
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:
req = request.get_json()
word = req['word']
meaning = req['meaning']
Answered By - Mureinik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.