Django admin custom AdminURLFieldWidget widget misses the Browse button

Multi tool use
Multi tool use


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.

Q5uwPoLauO,U b7tauG9WJIVIX IFpR4TiA5iO yW lR43m WY2L1ws1tqhFu1mwI707JEX5T3jHXyUE5aABUzw2jw0dMpYxg8H
WuKFvctt HwWRtee8j62wTq Rw,VrfzBSRqVYkt yWj8mOEnGmk,4ujxs0jhk0Yt6G,mhKx3h6W

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications