Lorbat
Lorbat

Reputation: 385

Flask HTML link query

How do make each contact a link?

I was thinking doing this:

<li><h3><a href{url_for('addcontact/contact_id"{{e[1].encode("utf-8")}}, {{e[2].encode("utf-8")}}" {contact_id}</a></h3></li>

My html

 <html>
    <body>
      <h1>List of contacts</h1>
      <ul class=contacts>
  {% for e in contacts %}
    <li><h3>"{{e[1].encode("utf-8")}}, {{e[2].encode("utf-8")}}"></h3></li>
  {% else %}
    <li><em>No contacts available</em></li>
  {% endfor %}
  </ul>
  <a href="/">Home</a>
  <a href="/addcontact/">Add a contact</a>

My Methods: contacts method

 @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

Add contacts method:

 @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')

` Additional description Basically I want to show surname and firstname but pass contact_id when you click into the link and go into detail that contact.

contact detail method:

detail of a contact

@app.route('/contacts/<int:contact_id>', methods=['GET'])
def contact_detail(contact_id):
    if request.method == 'GET':
        cur = g.db.execute('select surname, firstname, email, mobile\
                        from address where contact_id = ?', [contact_id])
        select = cur.fetchall()
        return render_template('modcontact.html', select=select, contact_id=contact_id)

detail contact template:

<html>
 <body>
     <h1>Detail of contact {{select[0]}}</h1>
     <form action='/addcontact/' method="post">
    <dl>      
      <dd><input type="submit" value="Modify Contact">
        <dd><input type="submit" value="Delete Contact">      
    </dl>
     </form>
     <a href="/">Home</a>
     <a href="/contacts">List of contacts</a>
  </body>
</html>

Upvotes: 1

Views: 1984

Answers (1)

Joel Cornett
Joel Cornett

Reputation: 24788

Looks like there are two issues with your current method.

First, you use url_for() for endpoints not routes, so assuming you have a function

@app.route('/contacts')
def contact_list():
    return ''

To get this url within a template, you would use:

url_for('contact_list')

not

url_for('/contacts')

Second, it looks like you're escaping twice, which you don't need to do. Try:

<li><h3>
    <a href="{{ url_for('the_endpoint_Im_trying_to_reach', contact_id=contact_id_field) }}">
        {{ first_name_field.encode('utf-8') }}, {{ surname_field.encode('utf-8') }}
    </a>
</h3></li>

Update: In order for keyword arguments to get passed as routes in url_for(), you need to define your route as such:

@app.route('/contacts/<contact_id>')
def the_endpoint_Im_trying_to_reach(contact_id=None):
    pass

Some more information on url_for can be found here.

Upvotes: 3

Related Questions