starylass
starylass

Reputation: 31

Passing button name from name to function - Django

I'm looking for help. I'm newbie in Django, python.

I'm want to pass name of clicked button to function in views and then use it as a filter.

This is html:

<tr>
  <td><a class="btn btn-primary" href="{% url 'display_ph' %}" role="button" method="post">{{ item.nazwa }}</a></td>
  <td>{{ item.ulica }}</td>
  <td>{{ item.miejscowosc }}</td>
  <td>{{ item.kod_pocztowy }}</td>
</tbody>

And I want to take "nazwa" and pass it to function:

def display_ph(request, nazwa):
  filter = request.GET.get('nazwa', False)
  items = Phandlowy.objects.filter(firma=filter)
  context = {
      'items': items,
  }
  return render(request, 'ph.html', context)

I don't now if it is possible with this button. Thank you for looking at this issue.

Upvotes: 0

Views: 282

Answers (1)

drec4s
drec4s

Reputation: 8077

You can pass it as query parameter:

<a href="{% url 'display_ph'  %}?nazwa={{item.nazwa}}">

And inside your view:

def display_ph(request):
    filter = request.GET.get('nazwa', False)
    items = Phandlowy.objects.filter(firma=filter)
    context = {
      'items': items,
    }
    return render(request, 'ph.html', context)

Upvotes: 1

Related Questions