काश flask.Flask
निर्माता के static_url_path
तर्क एक पूर्ण यूआरआई को स्वीकार करेंगे, लेकिन यह रास्ता एक अग्रणी स्लैश की आवश्यकता होगी, जब आप उस कोशिश के बारे में चिल्लाती है। अगर इसे स्वीकार किया जाता है, तो आप static_url_path='http://my_s3_bucket.aws.amazon.com/'
या फिर सेट करने के बाद url_for
का उपयोग कर सकते हैं।
एक और संभावित समाधान एक संदर्भ प्रोसेसर का उपयोग करना है। मेरा मानना है कि यह सबसे साफ समाधान है, लेकिन मुझे फ्लास्क के साथ ज्यादा अनुभव नहीं है। यह सुनिश्चित करें कि साफ दिखता है।एक संदर्भ प्रोसेसर का उपयोग करके, मैं पर्यावरण से यूआरएल खींचता हूं (मैं उसकेोकू का उपयोग कर रहा हूं इसलिए इसे सेट करना आसान है)। फिर संदर्भ प्रोसेसर static_url चर मेरे टेम्पलेट्स में उपलब्ध कराता है।
app.py में:
# Environment has STATIC_URL='http://my_s3_bucket.aws.amazon.com/'
@app.context_processor
def inject_static_url():
"""
Inject the variable 'static_url' into the templates. Grab it from
the environment variable STATIC_URL, or use the default.
Template variable will always have a trailing slash.
"""
static_url = os.environ.get('STATIC_URL', app.static_url_path)
if not static_url.endswith('/'):
static_url += '/'
return dict(
static_url=static_url
)
टेम्पलेट में:
<link rel="stylesheet" type="text/css" href="{{ static_url }}css/main.css" />
परिणाम
<link rel="stylesheet" type="text/css" href="http://my_s3_bucket.aws.amazon.com/css/main.css" />
स्रोत
2012-09-21 22:40:14
बदसूरत, कोई ज़रूरत नहीं है। चर सेट करने के लिए बस एक संदर्भ प्रोसेसर का उपयोग करें। –