mardi 2 février 2016

How do you load information into a database using flask?

Trying to teach myself flask by making a web app. I'm having trouble posting inputs from the user to my dataBase and when I load the page and then try to submit info through my form I get this 405 error:

"GET / HTTP/1.1" 200 -,

"POST / HTTP/1.1" 405 -

Any insight would be greatly appreciated, Thanks.

Here is the python snippet:

session = DBSession()

app = Flask(__name__)

@app.route('/')
def index(methods=['GET','POST']):
    print request.method
    if request.method == 'POST':
        instances = session.query(Vocab)
        newItem = Vocab(id=len(instances), word=request.form['new_word'])
        session.add(newItem)
        session.commit()
    instances = session.query(Vocab)
    return render_template('vocab_template.html', instances = instances)

The html template:

<!DOCTYPE html>
<html>
        <head>    
                <title>Vocab</title>
        </head>

        <body>
                <div>
                        <h1>Words!</h1>
                        <ul id='Words'>
                                {% for i in instances %}
                                        <li>
                                        {{i.word}}                                      
                                        </li>
                                {% endfor %}
                        </ul>
                        <form action="/" method="post">
                                <input type="text" name='new_word'>
                                <input type="submit" value="Add" name='submit'>
                        </form>
                </div>
        </body>

</html>



Aucun commentaire:

Enregistrer un commentaire