|
From flask import Flask app = Flask
|
bet | 4/13 | Sana | 30.07.2024 | Hajmi | 252,31 Kb. | | #268934 |
Bog'liq Flask notesfrom flask import url_for
@app.route('/')
def index():
return 'index'
@app.route('/login')
def login():
return 'login'
@app.route('/user/')
def profile(username):
return f'{username}\'s profile'
with app.test_request_context():
print(url_for('index'))
print(url_for('login'))
print(url_for('login', next='/'))
print(url_for('profile', username='John Doe'))
/
/login
/login?next=/
/user/John%20Doe
HTTP usullari
Veb-ilovalar URL manzillariga kirishda turli HTTP usullaridan foydalanadi. Flask bilan ishlashda HTTP usullari bilan tanishishingiz kerak. Odatiy bo'lib, marshrut faqat GETso'rovlarga javob beradi. Turli HTTP usullarini boshqarish uchun dekoratorning methodsargumentidan foydalanishingiz mumkin .route()
from flask import request
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
return do_the_login()
else:
return show_the_login_form()
Yuqoridagi misol marshrutning barcha usullarini bitta funktsiya doirasida saqlaydi, bu har bir qism ba'zi umumiy ma'lumotlardan foydalansa foydali bo'lishi mumkin.
Bundan tashqari, turli usullar uchun ko'rinishlarni turli funktsiyalarga ajratishingiz mumkin. get()Flask har bir umumiy HTTP usuli uchun bunday marshrutlarni , va hokazo bilan bezash uchun yorliq beradi post().
@app.get('/login')
def login_get():
return show_the_login_form()
@app.post('/login')
|
| |