2016-08-01 53 views

回答

0

在下面的例子中,圖像存儲在SQLite數據庫中。從那裏,你可以這樣做:

@app.route('/show') 
def show_pic(): 
    filename = request.args.get('filename','') 
    t = (filename,) 
    cur = g.db.execute('select label from pics where filename=?', t) 
    label = cur.fetchone()[0] 

    return render_template('upload.html', filename=filename, label=label) 

,然後在模板:

{% macro pic_path(pic) %} 
    {{ url_for('return_pic', filename='%s' % pic) }} 
{% endmacro %} 

{% block content %} 
    {% if filename is defined %} 
     <div id="image"> 
      <a href="{{ pic_path(filename) }}" target="_blank"> 
       <img class="new" src="{{ pic_path(filename) }}"> 
      </a> 
     </div> 
    {% else %} 
     <ul> 
     {% for pic in pics %} 
      <li class="thumb"> 
       <a href="{{ url_for('show_pic', filename=pic.filename) }}"> 
        <img class="thumb" src="{{ pic_path('thumb_'+pic.filename) }}"> 
       </a> 
       <strong>{{ pic.label }}</strong> 
      </li> 
     {% endfor %} 
     </ul> 
    {% endif %} 
{% endblock %}