Using object's id in change_form_object_tools.

2020-05-07 07:07发布

问题:

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

回答1:

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.