2015-11-05 49 views
1

我希望這是可能的。燒瓶使用標記數據庫查詢返回作爲參數?

有沒有辦法從db查詢返回一個返回的元素作爲參數傳遞給url_for?

這是由jinja模板標記,但它不是一個表單元素。

@app.route('/matcher/', methods=['GET', 'POST']) 
def show_entries(): 
    #global account_id 
    global load_file_id 
    if request.method == 'GET': 
     option = str(request.args.get('button')) 
     load_file_id = int(request.args.get('a')) 
     if option == 'All': 
      sql = """SELECT * 
        FROM TABLE WHERE LOAD_FILE_ID = :load_file_id""" 
      c = g.db.cursor() 
      c.execute(sql, load_file_id=load_file_id) 
      rows = c.fetchall() 
      for row in rows: 
       entries = [dict(title=row[0], text=row[1], text1=row[2], text2=row[3], text3=row[4], text4=row[5], text5=row[6], text6=row[7])] 
       media = row[8] 
       account_id = int(row[0]) 
       c.execute("""UPDATE TABLE SET STATUS = 'PENDING' WHERE ACCOUNT_ID = :account_id""", account_id=account_id) 
       g.db.commit() 
       outs = mediapod(account_id) 
       return render_template('show_entries.html', entries=entries, media=media, outs=outs, src=src) 
      c.close() 
    elif: 
      ############DOESN'T MATTER############# 

然後

{% extends "layout.html" %} 
{% block body %} 
    {% if session.logged_in %} 
     {% for entry in entries %} 
     <div class=entry> 
     <form action="{{ url_for('next') }}" method=get 
       class=validate> 
      <h2 name=id>{{ entry.title }}</h2> 
      <div class=subentry> 
       <div class=titles> 
       INFO:<br> 
       INFO:<br> 
       INFO:<br> 
       INFO:<br> 
       INFO:<br> 
       INFO:<br> 
       INFO:<br></div> 
       <div class=elements> 
       ${{ entry.text|string }}<br> 
       {{ entry.text1|string }}<br> 
       {{entry.text2|string }}<br> 
       {{ entry.text3|string }}<br> 
       {{ entry.text4|string }}<br> 
       {{ entry.text5|string }}<br> 
       {{ entry.text6|string }}<br></div> 
       <div class=mediatable> 
        <table> 
         <tr> 
          <th>Type</th> 
          <th>Date</th> 
          <th>Status</th> 
          <th>Doc ID</th> 
          <th>Scan Date</th> 
         </tr> 
       {% for put in outs %} 
         <tr> 
          <td>{{ put.media_type|safe }}</td> 
          <td>{{ put.statement_date|safe }}</td> 
          <td>{{ put.media_status|safe }}</td> 
          <td>{{ put.doc_id|safe }}</td> 
          <td>{{ put.scan_date|safe }}</td> 
         </tr> 
       {% endfor %} 
        </table> 
       </div> 
      <div class=buttons> 
      <input type=radio name=box value=match>Match 
      <input type=radio name=box value=nomatch>No Match 
      <input type=radio name=box value=fup>Follow-Up 
      <input type=radio name=box value=nomedia>No Media 
      <input type=submit class=next name=submit value=Next> 
      </div> 
      </div> 
     </form> 
     </div> 
     {% else %} 
      <em>stuff</em> 
     {% endfor %} 
    {% endif %} 
{% endblock %} 

,然後我有以後的路線,需要採取從show_entries()的ACCOUNT_ID

#Next Button 
@app.route('/next', methods=['GET']) 
def next(): 
    status = request.args.get('box') 
    c = g.db.cursor() 
    c.execute("""UPDATE TABLE 
       SET STATUS = :status 
       WHERE ACCOUNT_ID = :account_id""", 
       {"status" : str(status), 
       "account_id" : account_id 
       }) 
    g.db.commit() 
    return redirect(url_for('nextnext')) 
+0

['url_for'文檔(HTTP:

url_for('next', account_id=account_id) 

如果您需要show_entries觀點有模板該值,然後與render_template呼叫傳遞值: //flask.pocoo.org/docs/0.10/quickstart/#url-building)你可以爲期望參數的方法傳遞參數給'url_for'。 – Celeo

+0

我明白了,但是我通過了什麼?假設我設置爲def next(account_id):那麼如何使用該元素傳遞給url_for('next')? – tadamhicks

+0

'url_for('next',account_id = 100)' – Celeo

回答

0

以峯值回到了url_for docs

It accepts the name of the function as first argument and a number of keyword arguments, each corresponding to the variable part of the URL rule.

from flask import url_for 

url_for('index') 

,並在您的模板:3210 url_for可以在你的Python源使用

<a href="{{ url_for('index') }}"></a> 

如果你希望你的next方法獲得的值作爲URL的一部分,那麼你可以改變到

@app.route('/next/<int:account_id>', methods=['GET']) 
def next(account_id): 
    # ... 

然後就可以通過url_for呼叫傳遞變量,無論是從Python源方法定義或從您的模板:

return render_template('show_entries.html', entries=entries, media=media, outs=outs, src=src, account_id=account_id)