Django admin custom AdminURLFieldWidget widget misses the Browse button


Django admin custom AdminURLFieldWidget widget misses the Browse button



I'm trying to customize Django admin and I need to create a custom URL displayed for a FileField record in the change_form template. So, by googling I've found the following recipe adpated from a customization of an image field found here. Here is my code:


FileField


change_form


class MyAdminURLFieldWidget(URLInput):
template_name = 'admin/widgets/url.html'

def __init__(self, attrs=None):
final_attrs = {'class': 'vURLField'}
if attrs is not None:
final_attrs.update(attrs)
super(MyAdminURLFieldWidget, self).__init__(attrs=final_attrs)


def get_context(self, name, value, attrs):
context = super(MyAdminURLFieldWidget, self).get_context(name, value, attrs)
context['current_label'] = _('Currently:')
context['change_label'] = _('Change:')
context['widget']['href'] = smart_urlquote('/DownloadView/' + str(value.instance.id) + '/attachment/') if value else ''
return context


class FilesAdmin(admin.ModelAdmin):
list_display = ('id', '_animalid', '_filename', '_filedesc', '_ispublic', 'extra_info')
search_fields = ('subjectid__animalid',)
list_per_page = 50

def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == 'filename':
request = kwargs.pop("request", None)
kwargs['widget'] = MyAdminURLFieldWidget
return db_field.formfield(**kwargs)
else:
return super(FilesAdmin, self).formfield_for_dbfield(db_field, **kwargs)



However, the form display the field without the `Browse button:



enter image description here



I would like to have something like the default:



enter image description here



So, how can I make the Browse button to appear in my custom widget?


Browse









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages