0

I'm new to programming and i'm trying to make a registration program using Flask from Python. I've made a form in HTML using flask as a framework. I want the information from the form to be saved in a database, so we can see if who has registered. I use SQLALchemy and SQlite for my database setup. When I want to submit the form to save the data (from the page in the webbrowser) I get the following error: "TypeError: 'ImmutableMultiDict' object is not callable"

This is my HTML code:

{% block body %}

<form action="/", method="POST">
    <div class="container">
        <h1>Gasten registratie</h1>
        <p>Vul dit formulier in om te registreren</p>
        <hr>

        <label for="First-Name"><b>Voornaam</b></label>
        <input type="text" id="First-Name" placeholder="Vul hier uw voornaam in" name="Voornaam" required>

        <label for="Last-Name"><b>Achternaam</b></label>
        <input type="text" id="Last-Name" placeholder="Vul hier uw achternaam in" name="Achternaam" required>


        <label for="Company-name"><b>Bedrijf</b></label>
        <input type="text" id="Company-name" placeholder="Vul hier uw bedrijfsnaam in" name="Bedrijfsnaam" required>


        <label for="Date"><b>Datum</b></label>
        <input type="date" id="Date" placeholder="Selecteer de Datum" name="date" required/>
        <hr>


        <p>In het kader van onze ISAE certificering registreren wij uw gegevens.</p>
        <p>Wij verwerken uw gegevens volgends de regels van het AVG.</p>
        <button type="submit" class="registerbtn">Registreren</button>
    </div>


</form>

{% endblock %}

And this is my Python code:

from flask import Flask, render_template, url_for, request
from flask_sqlalchemy import SQLAlchemy 
from datetime import datetime'''


app = Flask(__name__) 
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///guest.db'
db = SQLAlchemy(app)


class gast(db.Model):
    id = db.Column(db.Integer, primary_key=True) 
    voornaam = db.Column(db.String(15), nullable=False)
    achternaam = db.Column(db.String(15), nullable=False)
    bedrijfsnaam = db.Column(db.String(50), nullable=False)
    datum = db.Column(db.Date)
    date_created = db.Column(db.Date, default=datetime.utcnow)

    def __repr__(self):
       return '<gast %r>' % self.voornaam

@app.route('/', methods=['POST','GET'])
def index():
    if request.method == 'POST':
        guest_vnaam = request.form('First-Name')
        guest_anaam = request.form('Last-Name')
        guest_cnaam = request.form('Company-name')
        guest_datum = request.form('Date')

        safe_vnaam = gast(voornaam=guest_vnaam)
        safe_anaam = gast(achternaam=guest_anaam)
        safe_cnaam = gast(bedrijfsnaam=guest_cnaam)
        safe_datum = gast(datum=guest_datum)

        try:
            db.session.add(safe_vnaam, safe_anaam, safe_cnaam, safe_datum)
            db.session.commit()
            return redirect('/')
        except:
            return 'Er ging iets fout met het opslaan van uw gegevens'

    else:
        return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)

Can anyone help me figuring out what I did wrong or what i forgot? Thanks in advance

Ronny Giezen
  • 557
  • 2
  • 10
  • 21
  • 2
    As the error says, `request.form` is a (type of) dict. You don't call dicts with parens, you access their elements with square brackets. All your `request.form(...)` lines should be `request.form[...]`. – Daniel Roseman Dec 06 '19 at 14:17
  • Hi Daniel, thanks for your quick response. I replaced the request.form(...) with request.form[...] . But now I get a different error. the error says: werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand. KeyError: 'First-Name' – Ronny Giezen Dec 06 '19 at 14:24
  • Well, your HTML fields have names like `Voornaam` and `Achternaam`, not `First-Name` and `Last-Name` etc. – Daniel Roseman Dec 06 '19 at 14:27
  • Hi Daniel, Thanks for helping me! that worked. I thought i had to pick the ID from each field in my form – Ronny Giezen Dec 06 '19 at 17:59

0 Answers0