0

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 %}
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Ania
  • 9
  • 1
  • 1
    if you dont get any input, how will you know how to filter anything? your server will have to receive something, **sanitize it** and use that as a filter – Nullman May 28 '19 at 08:17
  • In this case it's really simple, you should make your urls contain the category you want to display (/uslugodawcy/). So change your urls.py and your view will then have an extra parameter for the category that you can use to filter your objects. Then in your list of buttons, you can append the category_id to the url (in href attribute). You should take some time doing the [Django tutorial](https://docs.djangoproject.com/en/2.2/intro/tutorial01/) entirely to grasp some of these concepts. – dirkgroten May 28 '19 at 08:29
  • May I kindly suggest that you first do the full Django tutorial ? – bruno desthuilliers May 28 '19 at 08:30
  • Okey, filter works, but how to "get" filter atribute e.g. obj = Uslugodawcy.objects.filter(idkategorie=2) as a variable(not constant value) or if not, so what is other way to filter data by clicking a button? Those are our "first steps" in creating web applications and our knowlage in this field is really poor. We've done some django tutorial(from CodingEntrepreneurs) and also tried to find some tips on different internet forums , but we haven't found appropriate answer. – Ania May 28 '19 at 09:24

1 Answers1

0

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.

nigel222
  • 7,582
  • 1
  • 14
  • 22
  • I have tried this second option but I couldn't use this value in function in views.py. Could you help me with it? I know it should be easy but I am very beginner and it is my first project. – Ania May 29 '19 at 16:58
  • I think this is your answer: https://stackoverflow.com/questions/866272/how-can-i-build-multiple-submit-buttons-django-form – nigel222 May 29 '19 at 18:19
  • Meta-answer, always search Stackoverflow `[django] other keywords`. The term(s) in square brackets are tags. Above found with `[django] multiple submit button value` – nigel222 May 29 '19 at 18:23