다양한 html 페이지에 수백 개의 href = ".html, href ="css /, href = "img/및 src ="js/references "가있는 커다란 웹 사이트가 있습니다. 나는 "css, js, img, sound, etc ..."폴더를 "정적"폴더에두고 모든 html 파일을 "템플릿 폴더"에 넣었습니다.플라스크 html js css img reference 404 오류
저는 모든 HTML 파일을 검토하고 Flask의 "url_for"기능을 사용하여 각 href를 수동으로 편집하고 싶지 않습니다.
기존의 html 파일 만 남겨두고 html 파일에 정의 된 css /, js/및 이미지 경로를 사용하도록 플라스크에 지시 할 수있는 방법이 있습니까?
는 지금 다음 코드는 마티의 스크립트를 기반으로 404 오류 좋아
import os
from flask import Flask, render_template
PROJECT_PATH = os.path.dirname(os.path.realpath(__file__))
app = Flask(__name__,
template_folder=os.path.join(PROJECT_PATH, 'templates'),
static_folder=os.path.join(PROJECT_PATH, 'static')
)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)
의 무리로 연결, 내 사이트는 다음과 같은 코드를 사용하여 실행되었다. 그러나 내 index.html 파일에 내 많은 href = someHTMLfile.html 링크를 잡기 위해 @ app.route ('/')를 추가했습니다. 이 작업을 수행하는 더 좋은 방법이 있는지 궁금합니다.
from flask import Flask, render_template, safe_join, send_from_directory
app = Flask(__name__)
@app.route('/<any(css, js, img, fonts, sound):folder>/<path:filename>')
def toplevel_static(folder, filename):
filename = safe_join(folder, filename)
cache_timeout = app.get_send_file_max_age(filename)
return send_from_directory(app.static_folder, filename, cache_timeout=cache_timeout)
@app.route('/<path:filename>')
def public(filename):
return render_template(filename)
# @app.route('/')
# def index():
# return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080, debug=True)
내 index.html 파일에는 button.html, grid.html 등 다양한 html 파일에 수백 개의 href가 있습니다. 클릭하면 404로 연결됩니다. 위의 스크립트를 사용하여 포함시킬 수 있습니까? 그렇지 않으면 내 index.html 파일에 완벽하게 작동합니다! – spitz
임의의 HTML 경로도 처리 할 경로를 추가했습니다. –
많은 감사를드립니다! – spitz