4

I have two buttons that appointing to different paths. And i want to pass the object.id with parameter.

my urls

urlpatterns = [
    path('', admin.site.urls, name ='home'),
    path('dpo/imprimir/aprovado/<int:id>/',Aprovado, name ='aprovado'),
    path('dpo/imprimir/reprovado/<int:id>/',Reprovado, name ='reprovado'),
    ]

My views

from django.http import HttpResponse
from django.shortcuts import render
from django.shortcuts import render_to_response
from .models import Projeto


def Aprovado(request, id):
        obj = Projeto.objects.get(id=id)
        context = {
                "object": obj
        }
        return render(request, "dpo/imprimir/aprovado.html", context)

def Reprovado(request, id):
        obj = Projeto.objects.get(id=id)
        context = {
                "object": obj
        }
        return render(request, "dpo/imprimir/reprovado.html", context)

** My template**

{% load i18n admin_urls %}
{% block object-tools-items %}
<li>
    <a href="{% url 'aprovado' object.id  %}">{% trans "Aprovado" %}</a></a>
</li>
<li>

    <a href="{% url 'reprovado' object.id  %}">{% trans "Aprovado" %}</a>
</li>
{% endblock %}

i think i am doing this the wrong way

Alasdair
  • 298,606
  • 55
  • 578
  • 516
Pedro Mariz
  • 151
  • 2
  • 20
  • What does this have to do with no reverse match? – Sayse Apr 30 '19 at 13:30
  • because when i open my object , i have a error `NoReverseMatch`, and if i remove the `href` from the `a` tag it is working but is not pointing to nowhere. – Pedro Mariz Apr 30 '19 at 13:31
  • You should include the stack trace in the question – Sayse Apr 30 '19 at 13:35
  • Show the view that renders that template. – Alasdair Apr 30 '19 at 14:14
  • @Alasdair i am adding 2 buttons in django template, i am changing the template `change_form_object_tools.html` so i dont have view , my only view is from my app – Pedro Mariz Apr 30 '19 at 14:22
  • It looks as if `object` isn't set in the template context. Looking at [this template](https://github.com/django/django/blob/b9cf764be62e77b4777b3a75ec256f6209a57671/django/contrib/admin/templates/admin/change_form_object_tools.html), it looks as if using `original.pk` instead of `object.id` might work. If that doesn't work, I would add [`{% debug %}`](https://docs.djangoproject.com/en/dev/ref/templates/builtins/#debug) to the template to find out what's in the context. – Alasdair Apr 30 '19 at 14:29
  • Yeh! it is working with `original.pk` but if i want to add some parameter that is not the `id` how can i do it? – Pedro Mariz Apr 30 '19 at 14:40
  • Then you need to override the view and add the variable to the template context. Depending on what the parameter is, other options might be context processors, template tags and middleware. – Alasdair May 01 '19 at 09:16

2 Answers2

6

The object id string is also available in the template context as object_id.

This can be seen in the source for ModelAdmin._changeform_view() here.

djvg
  • 11,722
  • 5
  • 72
  • 103
4

In the change_form_object_tools.html template, you should be able to access the object using original.

{% load i18n admin_urls %}
{% block object-tools-items %}
<li>
    <a href="{% url 'aprovado' original.pk  %}">{% trans "Aprovado" %}</a></a>
</li>
<li>

    <a href="{% url 'reprovado' original.pk  %}">{% trans "Aprovado" %}</a>
</li>
{% endblock %}

In general, you can use the {% debug %} tag or django-debug-toolbar to check what variables are in the template context.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • but if I want to add some parameter that is not the `id` how can I do it? – Pedro Mariz May 03 '19 at 00:09
  • That’s a separate question to your original question, so I can’t help with that. See my comment above for some hints. – Alasdair May 03 '19 at 07:12
  • The `debug` tag is described [here](https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#debug), but `original` does not seem to be mentioned explicitly in the documentation, other than [this example](https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#overriding-vs-replacing-an-admin-template). – djvg Aug 12 '20 at 09:06