python - How to map a static file path to a url in Flask? -


currently,i want write gallery system on flask! can upload image now,i can't show image !i have try use send_from_directory function,but it's return me response stream instance!

class image(db.model):     __tablename__ = 'images'     id = db.column(db.integer, primary_key=true)     url = db.column(db.string)      timestamp = db.column(db.datetime, index=true,default=datetime.utcnow)     author_id = db.column(db.integer, db.foreignkey('users.id'))      def get_image(self):        return send_from_directory(self.url, '') 

and view below:

@gallery.route('/', methods=['get', 'post']) @gallery.route('/index', methods=['get', 'post']) def index():     page = request.args.get('page', 1, type=int)     pagination = image.query.order_by(image.timestamp.desc()).paginate(         page, per_page=current_app.config['landpack_posts_per_page'],         error_out=false )     posts = pagination.items     return render_template('gallery/index.html', posts=posts, pagination=pagination) @gallery.route('/upload', methods=['get', 'post']) def upload():     form = imageform()     if request.method == 'post':         filename = secure_filename(form.image.data.filename)         image_url = os.path.join(current_app.config['upload_folder'], filename)         form.image.data.save(image_url)         image = image(url=image_url)         db.session.add(image)         db.session.commit()         flash('you have add new photo!')         send_file = send_from_directory(image_url, filename)         print send_file         return redirect(url_for('.index'))     else:         filename = none      return render_template('gallery/upload.html', form=form) 

my form:

from flask.ext.wtf import form flask.ext.wtf import form wtforms import stringfield, passwordfield, booleanfield,  submitfield, integerfield wtforms.validators import required, email, length, regexp, equalto wtforms import validationerror flask_wtf.file import filefield   class imageform(form):     name = stringfield('image name', validators=[length(1, 64)])     tag = integerfield('tag value', default=50, validators=[required(), length(1, 64)])     image = filefield('your photo')     submit = submitfield('upload image') 

my template file simple that:

<img class="img-rounded profile-thumbnail" src="{{ post.get_image() }}"> 

how can have url src,i hope get_image() function can return me url (example:http://127.0.0.1/image/sample.jpg) help?

the answer @lapinkoira correct. in addition this, can check inside index() function using below code, whether there file exist or not:

if request.files:         filename = request.files['file']     print filename 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -