Django-admin:如何在记录更改列表中显示对象信息页面的链接而不是编辑表单?

时间:2022-10-09 07:20:36

I am customizing Django-admin for an application am working on . so far the customization is working file , added some views . but I am wondering how to change the records link in change_list display to display an info page instead of change form ?!

我正在为正在处理的应用程序定制Django-admin。到目前为止定制工作文件,添加了一些视图。但我想知道如何更改change_list显示中的记录链接以显示信息页而不是更改表单?!

in this blog post :http://www.theotherblog.com/Articles/2009/06/02/ extending-the-django-admin-interface/ Tom said :

在这篇博文中:http://www.theotherblog.com/Articles/2009/06/02/ extend-the-django-admin-interface / Tom说:

" You can add images or links in the listings view by defining a function then adding my_func.allow_tags = True "

“您可以通过定义一个函数然后添加my_func.allow_tags = True来在列表视图中添加图像或链接”

which i didn't fully understand !!

我还没完全明白!!

right now i have profile function , which i need when i click on a member in the records list i can display it ( or adding another button called - Profile - ) , also how to add link for every member ( Edit : redirect me to edit form for this member ) .

现在我有配置文件功能,当我点击记录列表中的成员我可以显示它(或添加另一个名为 - 配置文件 - 的按钮),以及如何为每个成员添加链接(编辑:重定向我编辑)该成员的表格)。

How i can achieve that ?!

我怎么能做到这一点?!

2 个解决方案

#1


20  

If I understand your question right you want to add your own link to the listing view, and you want that link to point to some info page you have created.

如果我理解您的问题,您想要将自己的链接添加到列表视图,并且您希望该链接指向您创建的某个信息页面。

To do that, create a function to return the link HTML in your Admin object. Then use that function in your list. Like this:

为此,请创建一个函数以在Admin对象中返回链接HTML。然后在列表中使用该功能。喜欢这个:

class ModelAdmin(admin.ModelAdmin):
    def view_link(self):
        return u"<a href='view/%d/'>View</a>" % self.id
    view_link.short_description = ''
    view_link.allow_tags = True
    list_display = ('id', view_link)

#2


9  

Take a look at: http://docs.djangoproject.com/en/dev/ref/contrib/admin/, ModelAdmin.list_display part, it says: A string representing an attribute on the model. This behaves almost the same as the callable, but self in this context is the model instance. Here's a full model example:

看一下:http://docs.djangoproject.com/en/dev/ref/contrib/admin/,TodeAdmin.list_display部分,它说:表示模型属性的字符串。这与callable几乎相同,但在此上下文中self是模型实例。这是一个完整的模型示例:

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    color_code = models.CharField(max_length=6)

def colored_name(self):
    return '<span style="color: #%s;">%s %s</span>' % (self.color_code, self.first_name, self.last_name)
colored_name.allow_tags = True

class PersonAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'colored_name')

So I guess, if you add these two methods to Person

所以我想,如果你将这两个方法添加到Person

def get_absolute_url(self):
    return '/profiles/%s/' % (self.id)

def profile_link(self):
    return '<a href="%s">%s</a>' % (self.get_absolute_url(), self.username)
profile_link.allow_tags = True

and changes PersonAdmin to

并将PersonAdmin更改为

class PersonAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'colored_name', 'profile_link')

Then you done

然后你完成了

#1


20  

If I understand your question right you want to add your own link to the listing view, and you want that link to point to some info page you have created.

如果我理解您的问题,您想要将自己的链接添加到列表视图,并且您希望该链接指向您创建的某个信息页面。

To do that, create a function to return the link HTML in your Admin object. Then use that function in your list. Like this:

为此,请创建一个函数以在Admin对象中返回链接HTML。然后在列表中使用该功能。喜欢这个:

class ModelAdmin(admin.ModelAdmin):
    def view_link(self):
        return u"<a href='view/%d/'>View</a>" % self.id
    view_link.short_description = ''
    view_link.allow_tags = True
    list_display = ('id', view_link)

#2


9  

Take a look at: http://docs.djangoproject.com/en/dev/ref/contrib/admin/, ModelAdmin.list_display part, it says: A string representing an attribute on the model. This behaves almost the same as the callable, but self in this context is the model instance. Here's a full model example:

看一下:http://docs.djangoproject.com/en/dev/ref/contrib/admin/,TodeAdmin.list_display部分,它说:表示模型属性的字符串。这与callable几乎相同,但在此上下文中self是模型实例。这是一个完整的模型示例:

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    color_code = models.CharField(max_length=6)

def colored_name(self):
    return '<span style="color: #%s;">%s %s</span>' % (self.color_code, self.first_name, self.last_name)
colored_name.allow_tags = True

class PersonAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'colored_name')

So I guess, if you add these two methods to Person

所以我想,如果你将这两个方法添加到Person

def get_absolute_url(self):
    return '/profiles/%s/' % (self.id)

def profile_link(self):
    return '<a href="%s">%s</a>' % (self.get_absolute_url(), self.username)
profile_link.allow_tags = True

and changes PersonAdmin to

并将PersonAdmin更改为

class PersonAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'last_name', 'colored_name', 'profile_link')

Then you done

然后你完成了