首页 > 解决方案 > 将可点击的链接添加到管理页面 List_view

问题描述

我想为一个特定的应用程序添加一个可点击的链接到管理员 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 的链接,或者实现这一目标的最佳方法是什么。谢谢!

标签: djangodjango-admin

解决方案


我想你的表述有错误。根据您的描述,您想在 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"

推荐阅读