From flask import Flask app = Flask




Download 252,31 Kb.
bet8/13
Sana30.07.2024
Hajmi252,31 Kb.
#268934
1   ...   5   6   7   8   9   10   11   12   13
Bog'liq
Flask notes

from flask import request

@app.route('/upload', methods=['GET', 'POST'])


def upload_file():
if request.method == 'POST':
f = request.files['the_file']
f.save('/var/www/uploads/uploaded_file.txt')
...
Agar fayl ilovangizga yuklanishidan oldin mijozda qanday nomlanganini bilmoqchi bo'lsangiz, atributga kirishingiz mumkin filename. Ammo shuni yodda tutingki, bu qiymat soxtalashtirilishi mumkin, shuning uchun hech qachon bu qiymatga ishonmang. Agar siz faylni serverda saqlash uchun mijozning fayl nomidan foydalanmoqchi bo'lsangiz, uni secure_filename()Werkzeug sizga taqdim etgan funksiya orqali o'tkazing:
from werkzeug.utils import secure_filename

@app.route('/upload', methods=['GET', 'POST'])


def upload_file():
if request.method == 'POST':
file = request.files['the_file']
file.save(f"/var/www/uploads/{secure_filename(file.filename)}")
...
Yaxshiroq misollar uchun Fayllarni yuklash ga qarang .
Cookie fayllari 
Cookie-fayllarga kirish uchun siz cookies atributdan foydalanishingiz mumkin. set_cookieCookie-fayllarni o'rnatish uchun javob ob'ektlari usulidan foydalanishingiz mumkin . So'rov ob'ektlarining atributi cookiesmijoz uzatadigan barcha cookie-fayllarga ega lug'atdir. Agar siz seanslardan foydalanmoqchi bo'lsangiz, to'g'ridan-to'g'ri cookie-fayllardan foydalanmang, aksincha siz uchun cookie-fayllar ustiga xavfsizlik qo'shadigan Flask-dagi Sessiyalardan foydalaning.
Cookie-fayllarni o'qish:
from flask import request

@app.route('/')


def index():
username = request.cookies.get('username')
# use cookies.get(key) instead of cookies[key] to not get a
# KeyError if the cookie is missing.
Cookie-fayllarni saqlash:

Download 252,31 Kb.
1   ...   5   6   7   8   9   10   11   12   13




Download 252,31 Kb.

Bosh sahifa
Aloqalar

    Bosh sahifa



From flask import Flask app = Flask

Download 252,31 Kb.