Xadmin自定义list_display_links链接跳转页面
通常list_display_links跳转的是编辑页面,
如果想要在
列表页之间
进行跳转,而不是跳转到update页面怎么办呢:
需要自定义xadmin/views/base.py 和list.py文件
以自定义开始的部分都是需要添加的,只要找到对应的地方,参考 "#自定义 " 后面的内容即可,本例子因为没有用到自定义的页面和视图函数,所以没有使用url的反向解析。由于赶时间,也只是简单的实现了链接跳转自定义的粗糙的解决方案,有时间了在琢磨。
#base.py
...
class
ModelAdminView
(CommAdminView):
#自定义,先查看下需要跳转到的页面的url,根据自己需要的url进行return的url链接设计
def
model_redirect_url
(
self
,
*args
,
**kwargs
):
return
'/{0}/根据需要填写/'
.format(
self
.opts.app_label)
def get_model_perms(self):
"""
Returns a dict of all perms for this model. This dict has the keys
``add``, ``change``, and ``delete`` mapping to the True/False for each
of those actions.
"""
return
{
'view'
:
self
.has_view_permission()
,
'add'
:
self
.has_add_permission()
,
'change'
:
self
.has_change_permission()
,
'delete'
:
self
.has_delete_permission()
,
#自定义,如果需要跳转到自定义的页面则可以自己添加,如果不是自定义的页面可以不需要添加
'redirect'
:
self
.has_redirect_permission()
,
}
#自定义
def
has_redirect_permission
(
self
,
obj=
None
):
codename = get_permission_codename(
'redirect'
,
self
.opts)
return
(
'redirect'
not in
self
.remove_permissions)
and
self
.user.has_perm(
'%s.%s'
% (
self
.app_label
,
codename))
#list.py
class
ListAdminView(ModelAdminView):
"""
Display models objects view. this class has ordering and simple filter features.
"""
...
#自定义
redirect = False
@
filter_hook
def
result_item
(
self
,
obj
,
field_name
,
row):
# If list_display_links not defined, add the link tag to the first field
if
(item.row[
'is_display_first'
]
and not
self
.list_display_links) \
or
field_name
in
self
.list_display_links:
item.row[
'is_display_first'
] =
False
item.is_display_link =
True
if
self
.list_display_links_details:
item_res_uri =
self
.model_admin_url(
"detail"
,
getattr
(obj
,
self
.pk_attname))
if
item_res_uri:
if
self
.has_change_permission(obj):
edit_url =
self
.model_admin_url(
"change"
,
getattr
(obj
,
self
.pk_attname))
else
:
edit_url =
""
item.wraps.append(
'<a data-res-uri="%s" data-edit-uri="%s" class="details-handler" rel="tooltip" title="%s">%%s</a>'
% (item_res_uri
,
edit_url
,
_(
u'Details of %s'
) %
str
(obj)))
#自定义
elif
self
.redirect
and
(item.field_name ==
'need_pay'
):
item_red_uri =
self
.model_redirect_url()
if
item_red_uri:
if
self
.has_redirect_permission(obj):
item.wraps.append(
u'<a href="%s">%%s</a>'
% item_red_uri)
else
:
return
最后也是最重要的,记得在对应的adminx.py里添加
redirect = True