简体   繁体   中英

How to pass values from html to python

I have a 4 buttons on first page which put me through to another page(All those 'buttons' are connected to database in mysql) and on this second page I have some data from mysql tables which I wanna display depending on what I have chosen in the first page. Right now I just display everything I have and I don't really know how to change that. I was looking for solutions but none worked.

views.py

def kategorie_list(request):

    obj = Kategorie.objects.all()
    context ={'obj': obj}
    return render(request, "kategorie/lista.html", context)

def uslugodawcy_list(request):

    obj = Uslugodawcy.objects.all()
    context ={'obj': obj}
    return render(request, "uslugodawcy/uslugodawcy_lista.html", context)

first html page

{% for Kategorie in obj %}

     <p> <a class="btn btn-primary" href="/uslugodawcy"><button type="nw" style="height: 65px; width: 170px"> <font size="4">{{Kategorie.idkategorie}} . {{Kategorie.nazwakategorii}}</font> </button> </a> </p>

{% endfor %}

second

{% for Uslugodawcy in obj %}

     <p> <a class="btn btn-primary" href="/promocje"><button type="nw" style="height: 65px; width: 170px"> <font size="4">{{Uslugodawcy.iduslugodawcy}} . {{Uslugodawcy.nazwa_uslugodawcy}} </font> </button> </a> </p>

{% endfor %}

There are many approaches. In outline:

  1. Style a link as a button. . If the href is http://djangoserver/url?foo=bar then request.GET['foo'] will be available and equal to "bar" .

  2. Make all the buttons relate to a form that you are displaying and POSTing, so all of them do a SUBMIT but have diffferent values that you can find in request.POST. <button form="form-id" type="submit" ...>

  3. Use Javascript to cause clicking the button to fill in fields on the form (which may be hidden fields, so the user has no other way of changing them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM