An warware: yadda ake ƙirƙirar shafin shiga

A cikin duniyar dijital ta yau, samun amintaccen shafin shiga mai aminci da mai amfani yana da mahimmanci ga kowane gidan yanar gizo ko aikace-aikace. Tsarin shiga da aka tsara da kyau ba kawai yana tabbatar da sirrin bayanan mai amfani ba amma yana haɓaka ƙwarewar mai amfani. A cikin wannan labarin, za mu nuna yadda ake ƙirƙirar shafin shiga mai sauƙi ta amfani da Python, wanda ke rufe bangarori daban-daban kamar ɗakunan karatu da ake buƙata, tantancewa, da aiwatar da mataki-mataki. Wannan koyawa na iya zama mafari ga masu farawa da kuma wartsakewa ga ƙwararrun masu haɓakawa.

Dakunan karatu da kayan aikin da ake buƙata

Don ƙirƙirar shafin shiga cikin Python, za mu buƙaci wasu mahimman ɗakunan karatu da kayan aiki. Bari mu tattauna su dalla-dalla:

  • Flask: Flask tsarin gidan yanar gizo ne mai nauyi don Python, wanda ke ba ku damar ƙirƙira da tura aikace-aikacen yanar gizo da sauri. Ita ce ainihin ɗakin karatu da za mu yi amfani da ita don tsara shafin shiga mu.
  • Aikin: Werkzeug babban kayan aiki ne na WSGI (Web Server Gateway Interface) wanda za'a iya amfani dashi tare da Flask don gudanar da ayyuka kamar tantance mai amfani da hashing kalmar sirri.
  • Jinja 2: Jinja2 injiniya ne mai gwadawa don Python, wanda ke ba ku damar ƙirƙirar samfuran HTML tare da maganganun Python. Za mu yi amfani da shi don tsara ƙirar gaba-gaba na shafin shiga mu.

Za mu ɗauka cewa an shigar da Python da editan lamba. Idan baku shigar da Flask, Werkzeug, da Jinja2 ba, gudanar da umarni mai zuwa:

pip install flask werkzeug jinja2

Da zarar an shigar da dakunan karatu masu mahimmanci, za mu iya ci gaba da aiwatar da shafin shiga.

Ƙirƙirar Shafin Shiga na asali

Bari mu fara da saita Flask don sarrafa hanyoyin shiga shafin mu. Za mu ƙirƙiri aikace-aikace mai sauƙi wanda ke da hanyoyi biyu: shafin gida da shafin shiga. Shafin gida zai nuna saƙon maraba, yayin da shafin shiga zai kula da amincin mai amfani.

Ga jagorar mataki-mataki don ƙirƙirar hanyoyin:

1. Shigo da abubuwan da ake buƙata:

from flask import Flask, render_template, redirect, url_for, request
from werkzeug.security import check_password_hash, generate_password_hash

2. Buɗe aikace-aikacen Flask:

app = Flask(__name__)

3. Ƙayyade hanyar shafin gida:

@app.route("/")
def home():
    return "<h1>Welcome to our website!</h1>"

4. Ƙayyade hanyar shiga:

@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        username = request.form["username"]
        password = request.form["password"]

        # Perform authentication here

    return render_template("login.html")

5. Gudanar da aikace-aikacen Flask:

if __name__ == "__main__":
    app.run(debug=True)

A cikin lambar da ke sama, mun bayyana hanyoyi guda biyu: shafin gida ("/") da shafin shiga ("/ shiga"). Hanyar shiga tana sauraron duka buƙatun GET da POST - tare da buƙatun GET da ke ba da shafin shiga da buƙatun POST na sarrafa amincin mai amfani.

Tabbatar da mai amfani da kalmar wucewa Hashing

Don wannan misalin, bari mu ɗauka cewa muna da mai amfani da waɗannan takaddun shaida masu zuwa:

  • Sunan mai amfani: user1
  • Kalmar wucewa: kalmar sirri ta

Don adana kalmar sirri ta mai amfani, za mu yi amfani da haifar da_password_hash aiki daga Werkzeug. Wannan zai haifar da hash na kalmar sirri, wanda za a yi amfani da shi don tabbatar da bayanan mai amfani yayin tantancewa.

Anan ga yadda ake samar da hash na kalmar sirri don 'my_password':

password_hash = generate_password_hash("my_password")

Yanzu, bari mu sabunta hanyar shiga mu don tabbatar da mai amfani:

@app.route("/login", methods=["GET", "POST"])
def login():
    if request.method == "POST":
        username = request.form["username"]
        password = request.form["password"]

        if username == "user1" and check_password_hash(password_hash, password):
            return redirect(url_for("home"))
        else:
            return "<h2>Invalid username or password!</h2>"

    return render_template("login.html")

A cikin lambar da ke sama, mun ƙara rajistan tantancewa don tabbatar da shaidar mai amfani. Idan sunan mai amfani ya yi daidai da “user1” kuma kalmar sirri ta dace da hash ɗin mu, ana tura mai amfani zuwa shafin farko; in ba haka ba, ana nuna saƙon kuskure.

A wannan gaba, muna da shafin shiga mai aiki amma na asali da aka kirkira ta amfani da Python. Kuna iya ƙara siffanta wannan misalin ta ƙara ƙarin fasali, kamar rajistar mai amfani, sake saitin kalmar sirri, da sarrafa zaman, don ƙirƙirar ingantaccen tsarin shiga mai tsaro.

Shafi posts:

Leave a Comment