29 Mart 2024 01:59

Anasayfa

undefined...

Türkçe Flask örnekleri ve  Flask dersleri için Python ile geliştirilen microframework olan Flask ile basit işlemler yapabilirsiniz. Aşağıdaki kodlarda bir çok request örneği, çerez işlemleri, form elementleri ve parametreler gösterilmiştir.

from flask import Flask, render_template, request, make_response
from werkzeug.utils import secure_filename

app = Flask(__name__)


@app.route('/')
def ansa_sayfa():
  return 'Merhaba!'


@app.route('/giris', methods=['GET', 'POST'], strict_slashes=False)
def giris():
  if request.method == 'POST':
    return '{}'.format("POST")
  else:
    return '{}'.format("GET")


@app.route('/cerezoku', strict_slashes=False)
def cerezoku():
  kullaniciadi = request.cookies.get('kullaniciadi')
  return render_template('cerez.html')


@app.route('/cerezguncelle', strict_slashes=False)
def cerezguncelle():
  resp = make_response(render_template('cerezokunacaksayfa.html'))
  resp.set_cookie('kullaniciadi', 'musluy')
  return resp


@app.route('/login', methods=['POST', 'GET'], strict_slashes=False)
def login():
  if request.method == 'POST':
    kullaniciadi = request.form['kullaniciadi']
    return render_template('login.html', kullaniciadi=kullaniciadi)
  else:
    uyari = 'Lütfen giriş yapınız'
    return render_template('login.html', uyari=uyari)


@app.route('/dosyayuklemeformu', methods=['POST', 'GET'], strict_slashes=False)
def post_dosya():
  if request.method == 'POST':
    f = request.files['dosya']
    f.save('/home/muslu/flask/uploads/{}'.format(secure_filename(f.filename)))
    return render_template('dosyayuklemeformu.html')
  else:
    uyari = 'Lütfen yüklemek için dosya seçiniz!'
    return render_template('dosyayuklemeformu.html', uyari=uyari)


@app.route('/kayit/goster/<int:kayit_id>', strict_slashes=False)
def kayit_goster(kayit_id):
  return '{}'.format(kayit_id)


@app.route('/merhaba/', strict_slashes=False)
@app.route('/merhaba/<isim>')
def hello(isim=None):
  return render_template('index.html', isim=isim)


app.run(host="127.0.0.1", port=8035, debug=True)

 

 

 

 

 

 

 

 

 

Kaynakça

Python

hakkında diğer konular

Makdos Bilişim Teknolojileri 2015 - 2022