【问题标题】:Django-jet admin- add button for each rowDjango-jet admin-为每一行添加按钮
【发布时间】:2023-03-06 06:06:01
【问题描述】:

我想创建一个用于删除表格中选定行的按钮(每行 1 个按钮)

admin.py

from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from import_export.admin import ImportExportMixin
from .models import Applicant


class ApplicantAdmin(ImportExportModelAdmin, admin.ModelAdmin):

    list_display = ('Name', 'DOB', 'PhoneNumber', 'Address', 'Batch',
                   'created_at', 'updated_at',)
    list_filter = ('Name', 'Address', 'Batch', 'created_at', 'updated_at',)
    list_per_page = 10
    # actions = [transferdata, ]


# Register the admin class with the associated model
admin.site.register(Applicant, ApplicantAdmin)

models.py

from django.db import models
from django.utils import timezone

class Applicant(models.Model):
    id = models.CharField(max_length=10).primary_key
    Name = models.CharField(max_length=50)
    DOB = models.CharField(max_length=10)
    PhoneNumber = models.CharField(max_length=20)
    Address = models.CharField(max_length=200)
    Batch = models.CharField(max_length=200)
    created_at = models.DateTimeField(default=timezone.now)
    updated_at = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return self.Name

我已经知道 django-jet,它为这个工具提供了一个下拉菜单,但适用于整个表格(即不是每一行)

【问题讨论】:

    标签: django django-admin django-jet


    【解决方案1】:

    通过在所需的管理类中创建一个函数来解决这个问题。

    admin.py

    import django.contrib import admin
    import import_export.admin import ImportExportModelAdmin
    import import_export.admin import ImportExportMixin
    import .models import Applicant
    
    class ApplicantAdmin(ImportExportModelAdmin, admin.ModelAdmin):
        list_display = ('Name', 'DOB', 'PhoneNumber', 'Address', 'Batch',
                   'created_at', 'updated_at',)
        list_filter = ('Name', 'Address', 'Batch', 'created_at', 'updated_at',)
        list_per_page = 10
        # actions = [transferdata, ]
    
        @staticmethod
        def action_button(self):
            # assuming the url is saved as 'button_url'
            # enter the url to be parsed when the button will be clicked and name the button
            return format_html('<a class="button" href="%s">(name of the button)</a>' % button_url)
    
    # Register the admin class with the associated model
    admin.site.register(Applicant, AdminApplicant)
    

    views.py

    中创建按钮的功能

    urls.py 中,在应用程序的 urlpatterns 中输入 url(几乎与 admin 类中相同)并调用 views.py 中的函数p>

    【讨论】:

      猜你喜欢
      • 2012-08-24
      • 2019-05-30
      • 2022-01-06
      • 2013-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      相关资源
      最近更新 更多