将可点击链接添加到管理页面 List_view

时间:2021-01-30 12:32:18

标签: django django-admin

我想为特定应用程序添加一个指向 admin list_view 的可点击链接,该链接会将我重定向到我创建的 html 文件,我已经创建了这样的 view.py

from django.shortcuts import render
from . models import *
from django.contrib.auth.decorators import login_required

@login_required
def transaction_print(request, transaction_id):
    transaction = Transaction.objects.get(id=transaction_id)

    return render(request, 'report.html', {'transaction': transaction})

和我的 urls.py

from django.urls import path
from . import views

urlpatterns = [
    # path('', views.transaction_print, name='transaction_print'),
    path('transaction/<int:transaction_id>/', views.transaction_print, name='transaction_print'),
]

我在 (root_dir/calculator/templates/report.html) 处创建了 report.html

现在我不确定如何在models.py 中创建一个方法来返回我的视图作为要添加到list_view 的链接,或者实现这一目标的最佳方法是什么。谢谢!

1 个答案:

答案 0 :(得分:0)

我猜你的表述有误。 根据您的描述,您想在 change_view 上添加一个按钮。

为此,在您的应用模板文件夹中,创建一个文件 extended_changeform.html

在那个文件中:

{% extends 'admin/change_form.html' %}
{% block object-tools-items %}

<li><a href="{% url 'transaction_print' original.id %}" class="historylink">My button label</a>
</li>

{{ block.super }}
{% endblock %}

在你想要按钮的管理类中:

class TransactionAdmin(ModelAdmin):
    change_form_template = "extended_changeform.html"
相关问题