django - 将可点击的链接添加到管理页面 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 的链接,或者实现这一目标的最佳方法是什么。谢谢!
解决方案
我想你的表述有错误。根据您的描述,您想在 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"
推荐阅读
- java - 在@Component 和@Configuration 中使用@Bean
- vb.net - vb.net 中的 BC36645 错误
- c# - 使用 VSTS Git API C# 获取分支项目
- java - 如何从kafka avro记录生成pojo?
- tendermint - Tendermint Networks,如果某些节点离线,有什么方法可以获取该节点丢失的数据?
- c++ - 在 Visual Studio 中查找包含 iostream 的文件
- simulation - Contiki RPL 和单播 DIO
- json - curl_easy_perform 不等待在 URL 上提供 JSON 输入
- angular - 创建后添加Highchartsevent
- java - 在 gradle 项目中使用 HttpUrlBuilder