फ्लास्क में url_for
(टेम्पलेट्स में भी शामिल है) एक आवेदन पत्र भर URL को बदलने के लिए होने की भूमि के ऊपर रोकने के लिए एक URL बनाने के लिए प्रयोग किया जाता है। url_for
के बिना, यदि आपके ऐप के रूट यूआरएल में कोई बदलाव है तो आपको इसे हर पेज में बदलना होगा जहां लिंक मौजूद है।
सिंटेक्स: url_for('name of the function of the route','parameters (if required)')
यह रूप में इस्तेमाल किया जा सकता है:
@app.route('/index')
@app.route('/')
def index():
return 'you are in the index page'
अब अगर आप एक लिंक सूचकांक पृष्ठ है: यदि आप इस का उपयोग कर सकते हैं:
<a href={{ url_for('index') }}>Index</a>
आप एक कर सकते हैं इसके साथ बहुत कुछ ओ सामान, उदाहरण के लिए:
@app.route('/questions/<int:question_id>'): #int has been used as a filter that only integer will be passed in the url otherwise it will give a 404 error
def find_question(question_id):
return ('you asked for question{0}'.format(question_id))
ऊपर के लिए हम उपयोग कर सकते हैं:
<a href = {{ url_for(find_question,question_id=1) }}>Question 1</a>
इस तरह आप बस पैरामीटर पास कर सकते हैं!
जिसका अर्थ है कि समारोह है 'डीईएफ़ (चर)' जोड़ सकता हूँ? – endolith
@endolith, हां। ** 'के लिए पारित kwargs url_for' बोतल – highvolt