Django button get id

Hello, i’m in deep struggling with an easy thing i think, probably too stressed to figure out the problem.
I created a model:

class Incarico(models.Model):
id = models.AutoField(primary_key=True)
incarico = models.DateField()
mandante = models.CharField(max_length=50, default=None)

I created 3 entries inside the database and created a page(slice_incarico) where to display these entries in this way:

def slice_incarico(request):
elements = Incarico.objects.all()
context = {‘elements’: elements}
return render(request, ‘incarico/slice_incarico.html’, context)

So i alwaays display into a table all the entries in the database referring to that model using html like so:


{% for obje in elements %} {% endfor %}
vai a: ID Incarico Mandante
vai a ID {{}} {{obje.incarico}} {{obje.mandante}}

This structure create something like this right?

well, the missing part here is that i wish to use this button vai a ID
To jump into a new page (incarico_view) which will only let me visualize that specific entry.
I’m thinking about using the id, but i don’t know how, is very frustrating
Any help is much appreciated!!
Thanks in advance!

What does your template code look like? A button by itself doesn’t do much unless you want to tie some JavaScript to it. Instead you can use CSS to make an <a> element look like a button. For example: <a class="btn btn-primary" href="#" role="button">Link</a> would render as a button with the Bootstrap framework, and clicking on it would take you to where you want to go.

So you’d end up with something like this:

<a class="btn btn-primary" href="{% url 'your_url_method' %}">{{ obje.incarico }}</a>
1 Like

ok i figured it out

changed 2 easy things:

def view_incarico_schema(request, my_id):
#sample_id = 1
#print(Incarico.objects.all(), ‘asd’)

obj = Incarico.objects.get(id=my_id)
context = {'object': obj}
return render(request,'incarico/view_incarico.html', context) just added int:my_id
path(‘incarico_view/int:my_id’, view_incarico_schema, name=‘incarico’),

this + your dynamic button it’s enough to generate a dynamic url based on id of each entry.

Thanks a lot!