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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1172584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:39:04  来源:igfitidea点击:

Django-admin : How to display link to object info page instead of edit form , in records change list?

pythondjangodjango-adminadmin

提问by Hamza

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/extending-the-django-admin-interface/汤姆说:

" 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 ?!

我怎么能做到这一点?!

回答by Alexander Ljungberg

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)

回答by enoch_qin

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/,ModelAdmin.list_display 部分,它说: 代表模型属性的字符串。这与可调用的行为几乎相同,但在此上下文中 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

然后你完成了