简体   繁体   English

如何将值从html传递给python

[英]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. 我在第一页上有一个4个按钮,它让我通过另一个页面(所有这些'按钮'都连接到mysql中的数据库),在第二页上我有一些来自mysql表的数据,我想根据我选择的内容显示在第一页。 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 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 第一个HTML页面

{% 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" . 如果hrefhttp://djangoserver/url?foo=bar那么request.GET['foo']将可用并等于"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. 使所有按钮与您正在显示和POST的表单相关,因此所有按钮都执行SUBMIT但具有您在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. 使用Javascript导致单击按钮以填充表单上的字段(可能是隐藏字段,因此用户无其他方式更改它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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