画像を表示したいです。
html
1<p><img src="media/1.png"></p>
現在、このHTMLファイルはtemplatesというディレクトリにあり、
「1.png」という画像ファイルはmediaというディレクトリに存在しています。
上記の方法で実行すると、エラーなどは出ないものの、
四角い小さなアイコンのようなものに置き換わってしまいます。
原因が分かる方いらっしゃいますでしょうか?
よろしくお願いいたします。
追加:
ディレクトリ構造
flask -media/ 1.png -static/ stylesheet.css -templates/ show.html -venv flask.py hello.db
flask.pyとhello.dbはflask直下に置いてあります。
書き方が違うかもしれませんが、
ディレクトリ構造はこのような感じです。
画像をアップロードするコード:
python
1# 画像アップロード 2@app.route('/image_entry', methods=['GET', 'POST']) 3def image_entry(): 4 if request.method == 'POST': 5 img_file = request.files['img_file'] 6 if img_file and allowed_file(img_file.filename): 7 filename = secure_filename(img_file.filename) 8 img_file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) 9 img_url = '/uploads/' + filename 10 return render_template('show.html', media=img_url)
show.htmlを表示するコード:
python
1@app.route('/') 2def show(): 3 cur = g.db.execute('select id, title, text from entries order by id asc') 4 entries = [dict(id = row[0] ,title=row[1], text=row[2]) for row in cur.fetchall()] 5 return render_template('show.html', entries=entries)
実行環境:
OS:Mac
IDE:Pycharm
回答2件