Django 用于传递筛选参数的管理员URL解析程序
我想在管理表单中添加一个链接,以显示单击对象的子对象 在我的模型中,我有:Django 用于传递筛选参数的管理员URL解析程序,django,url,django-admin,Django,Url,Django Admin,我想在管理表单中添加一个链接,以显示单击对象的子对象 在我的模型中,我有: def change_form_link(self): changeform_url = urlresolvers.reverse('admin:customerData_wifirouter_changelist' return '<a href="%s" >Change</a>' % changeform_url change_f
def change_form_link(self):
changeform_url = urlresolvers.reverse('admin:customerData_wifirouter_changelist'
return '<a href="%s" >Change</a>' % changeform_url
change_form_link.allow_tags = True
另一方面,我的管理员筛选页面在http://127.0.0.1:8000/admin/customerData/wifirouter/?building__building_label=4+吕+德+杜埃
我知道我正在使用bas语法链接到过滤后的管理页面
建议?查询字符串
?building\uu building\u label=4+rue+de+Douai
不是反向url的一部分。您可以反转url,然后向其中添加查询字符串
from urllib.parse import urlencode
# In Python 2 from urllib import urlencode
changeform_url = urlresolvers.reverse('admin:customerData_wifirouter_changelist')
querydict = {'building__building_label': self.building_label}
changeform_url = '%s?%s' % (changeform_url, urlencode(querydict))
使用urlencode可确保字符串采用url编码(例如,空格转换为+
符号)
NoReverseMatch at /admin/customerData/customer/1/
Reverse for 'customerData_wifirouter_changelist' with arguments '('4 rue de Douai',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['admin/customerData/wifirouter/$']
from urllib.parse import urlencode
# In Python 2 from urllib import urlencode
changeform_url = urlresolvers.reverse('admin:customerData_wifirouter_changelist')
querydict = {'building__building_label': self.building_label}
changeform_url = '%s?%s' % (changeform_url, urlencode(querydict))