简体   繁体   English

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

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

I am customizing Django-admin for an application am working on . 我正在为正在处理的应用程序定制Django-admin。 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 ?! 但我想知道如何更改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 ?! 我怎么能做到这一点?!

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. 为此,请创建一个函数以在Admin对象中返回链接HTML。 Then use that function in your list. 然后在列表中使用该功能。 Like this: 像这样:

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)

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. 看一下: http ://docs.djangoproject.com/en/dev/ref/contrib/admin/,ModelAdmin.list_display part,它说:表示模型属性的字符串。 This behaves almost the same as the callable, but self in this context is the model instance. 这与callable几乎相同,但在此上下文中self是模型实例。 Here's a full model example: 这是一个完整的模型示例:

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 然后你完成了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 django 的管理员中禁用编辑 object 的链接(仅显示列表)? - Disable link to edit object in django's admin (display list only)? 如何在django管理员更改列表页面中删除和编辑链接 - How can i have delete and edit link in django admin change list page 如何在 Django 管理列表显示页面中编辑 ManyToManyField? - How To Edit ManyToManyField In Django Admin list Display Page? Django如何显示 <bound method … > &gt;在django-admin中将对象作为字符串对象? - How does Django display a <bound method … >> object as a string object in django-admin? 如何在 django 管理面板的列表显示旁边添加自定义按钮? 从列表中编辑表单显示它自己 - How to add a custom button beside the list display in django admin panel? To edit the form from list display it self 如何在Django管理后台的“更改列表”页面显示上传的图片? - How to display uploaded images in "Change List" page in Django Admin? 从另一个页面链接到用户的django admin编辑表单 - Link to the user's edit form of django admin from another page Django-admin:渲染前以更改形式更改字段值 - Django-admin: Changing field values in change form before rendering 从django-admin中的list_display向用户分配票证 - Assign user to ticket from list_display in django-admin 如何不在Django管理对象页面中将外键显示为列表? - How to not display foreign key as list in Django admin object page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM