Flask Build Error

When I fill in the form I get this error, I dont see the flash message although its added it to my db:

werkzeug.routing.BuildError
BuildError: ('/contacts', {}, None)

my methods:

@app.route('/contacts/', methods=['GET','POST'])
def contact_list():
    cur = g.db.execute('select contact_id, surname, firstname from address order by surname')
    contacts = cur.fetchall()
    return render_template("contacts.html", contacts = contacts) #refer to template


@app.route('/addcontact/', methods=['GET','POST'])
def contact_add():
    if request.method == 'POST':
        g.db.execute('insert into address (surname, firstname, email, mobile) values (?, ?, ?, ?)',
                 [request.form['firstname'], request.form['surname'], request.form['email']
                , request.form['mobile']])
        g.db.commit()
        flash('New entry was successfully posted')
        return redirect(url_for('/contacts')) #redirect to the contacts page
    elif request.method != 'POST':
        return render_template('addcontact.html')

My html:

<html>
  <body>
      <h1>Add new contact</h1>
  <form action='/addcontact/' method="post">
    <dl>
      <dt>First Name:
      <dd><input type="text" size=30 name="firstname">
      <dt>Surname:
      <dd><input type="text" size=30 name="surname">
      <dt>Email:
      <dd><input type="text" size=30 name="email">
      <dt>Mobile:
      <dd><input type="text" size=30 name="mobile">            
      <dd><input type="submit" value="Add New Contact">
    </dl>
  </form>
  <a href="/">Home</a>
  <a href="/contact">List of contacts</a>

Answers


The url_for() call fails; you need to give it the route name, not the url. The exception is raised because there is no /contacts route. This should work:

url_for('contact_list')

Because you called g.db.commit() the new contact has already been committed to the database by the time the url_for() call raises an exception.


Need Your Help

Javascript form validation for custom browser extensions

javascript mongodb url meteor

I want to let users create custom urls for group pages, and I'm trying to find the best way to restrict them to appropriate characters: pretty much just numbers, letters, or a hyphen.

How to configure a single-threaded ForkJoinPool?

java multithreading fork-join forkjoinpool

Is it possible to configure ForkJoinPool to use 1 execution thread?