From flask import Flask app = Flask




Download 252,31 Kb.
bet4/13
Sana30.07.2024
Hajmi252,31 Kb.
#268934
1   2   3   4   5   6   7   8   9   ...   13
Bog'liq
Flask notes

from 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')



Download 252,31 Kb.
1   2   3   4   5   6   7   8   9   ...   13




Download 252,31 Kb.

Bosh sahifa
Aloqalar

    Bosh sahifa



From flask import Flask app = Flask

Download 252,31 Kb.