I work on python web app.
In a page i want users to chose their friends to add their organization. But to build that page i prepare data in flask side first. Then i send it to HTML. Then i use for loop in HTML doc to build div blocks ( and add friend information into the blcok ). Then when user chose more than 1 friend, i want to get back data from the form to flask side to use data in my database. But unfortunatelly i cant receive all the values from checkboxes. Coz checkboxes are builded by for loop and all of them names are same.
Here is the flask side that i prepare the data. arkadasList goes to yeni.HTML.
@app.route("/yeni")
def newPlan():
username = session["username"]
cursor = mysql.connection.cursor()
sorgu = "Select friends from users where username = %s"
result = cursor.execute(sorgu,(username,))
if result > 0:
kanka = True
arkadaslar = cursor.fetchone()
arkadasList = list()
arkadaslar = arkadaslar["friends"].split(",")
arkadaslar.pop()
for i in arkadaslar:
sorgu2 = "Select name from users where username = %s"
cursor.execute(sorgu2,(i,))
isim = cursor.fetchone()
isim = isim["name"]
kAdı = i
x = dict(name=isim,username = kAdı)
arkadasList.append(x)
else:
arkadasList = list()
kanka = False
return render_template("yeni.html",arkadasList=arkadasList, kanka=kanka)
Here in yeni.HTML doc i use for loop in arkadasList and build a div contains username and name for each loop. ( Also this codes covered by form as well. I did not add here to not make code longer )
<div class="arkadasEkle">
<div class="arkadasBaslık">
<h2>Ekleyebileceğiniz Arkadaşlarınız</h2>
<p>Hesaba eklemek istediğiniz arkadaşlarınızı listeden seçiniz</p>
</div>
{% if kanka %} {% for ark in arkadasList %}
<div class="arkadasKutu">
<i class="fa-solid fa-user" style="font-size: 3.7rem"></i>
<section>
<label for="checkbox">{{ark.name}}</label>
<label for="checkbox" style="font-size: 1.7rem"
>{{ark.username}}</label
>
</section>
<input
type="checkbox"
class="checkbox-round"
value="{{ark.username}}"
name="checkbox"
/>
</div>
{% endfor %} {% else %}
<p>oradayım</p>
{% endif %}
</div>
Here i try to get data back from the FORM. But next pictures you will see, it only gives me the one that i clicked ( checkboxed ) first.
@app.route("/layout", methods = ["GET","POST"])
def layout():
if request.method == "POST":
x = request.form["paymentType"]
a = request.form["hesapName"]
c = request.form["hesapKat"]
y = request.form["checkbox"]
b = x+a+c+"----"+y
return render_template("layout.html", b=b)
else:
return render_template("layout.html")
yeni.HTML
layout.HTML after post request from yeni.html
I want to receive the usernames that checkboxed
Anıl Demirelli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.