ฉันแน่ใจว่ามีวิธีอื่นในการบรรลุเป้าหมายนี้ แต่วิธีหนึ่งคือการใช้โครงสร้างพิมพ์เขียวและมีไฟล์คงที่ทั้งหมดสำหรับพิมพ์เขียวเฉพาะนั้นภายในโฟลเดอร์พิมพ์เขียว
app
|- collections
| - blueprint_one
| - configs
| - static
| - css
| - js
| js_file_one.js
| - templates
| - blueprint_one
| html_file_one.html
| blue_print_one_view_file_one.py
| __init__.py
| - blueprint_two
| - blueprint_three
|- static
| - js
| - css
|- templates
__init__.py
โครงสร้างโฟลเดอร์ด้านบนช่วยให้คุณสามารถแยกไม่เพียงแต่ไฟล์แบบคงที่ แต่ยังรวมถึงไฟล์มุมมอง Flask ด้วย
ด้านล่างนี้เป็นตัวอย่างของการเปิดใช้งาน/นำเข้าไฟล์:
1) app/init.py
app = Flask(__name__, static_folder='static')
app.config.from_pyfile('app_config.py')
# Do all your db set up and etcs..
# Import blueprints below set ups to prevent circular import
app.collections.blueprint_one import blueprint_one
app.collections.blueprint_two import blueprint_two
# Now register blueprints that you've imported
app.register_blueprint(blueprint_one, url_prefix='/blueprint_one')
app.register_blueprint(blueprint_two, url_prefix='/blueprint_two')
2) blueprint_one/init.py
# Create a blueprint
from flask import Blueprint
blueprint_one = Blueprint('blueprint_one_name', __name__, template_folder='templates', static_folder='static')
# Import required views to use
from . import blue_print_one_view_file_one.py
3) blueprint_one/blue_print_one_view_file_one.py
# Import blueprint
from app.collections.blueprint_one import blueprint_one
# Any view functions using above blueprint
@blueprint_one.route('/login/, methods=['GET'])
def login():
return render_template('blueprint_one/html_file_one.html')
4) blueprint_one/templates/blueprint_one/html_file_one.html
// Note: instead of .static, you can use blueprint_one.static.
<script type="text/javascript" src="{{ url_for('.static', filename = 'js/js_file_one.js') }}"></script>
5) app/static และ app/templates ยังคงสามารถใช้งานได้ในขณะที่คุณใช้งานอยู่ในปัจจุบัน
โซลูชันนี้ไม่ได้แก้ปัญหาการวาง js และ html ลงในโฟลเดอร์เดียวกัน แต่อย่างน้อยคุณก็สามารถแบ่งแต่ละคุณลักษณะออกเป็นส่วนประกอบ เช่น พิมพ์เขียว เพื่อโครงสร้างโปรเจ็กต์แบบโมดูลาร์มากขึ้น
person
Jessi
schedule
28.06.2019
templates
ของคุณไปยังโฟลเดอร์static
- person Avm-x   schedule 28.06.2019