Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#17549 closed New feature (fixed)

Add link to open URL from URLField

Reported by: Chris Wilson Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords:
Cc: tomas.ehrlich@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description

It's useful for URLField to give you a way to open the URL; otherwise you might as well use a CharField.

class URLFieldWidgetWithLink(AdminURLFieldWidget):
    def render(self, name, value, attrs=None):
        html = AdminURLFieldWidget.render(self, name, value, attrs=attrs)

        if value is not None:
            final_attrs = dict(href=value, target='_blank')
            html += " <a %s>(open)</a>" % attributes_to_str(final_attrs)
        
        from django.utils.safestring import mark_safe
        return mark_safe(html)

class DocumentAdmin(admin.ModelAdmin):
    formfield_overrides = {
        django_fields.URLField: {'widget': URLFieldWidgetWithLink},
    }

Attachments (1)

17549.patch (3.5 KB) - added by Tomáš Ehrlich 4 years ago.
Fixed force_unicode in AdminURLFieldWidget

Download all attachments as: .zip

Change History (6)

comment:1 Changed 5 years ago by Aymeric Augustin

Component: Formscontrib.admin
Needs documentation: set
Needs tests: set
Patch needs improvement: set
Triage Stage: UnreviewedAccepted

I suppose the code in the ticket description is the patch; a unified diff would be much easier to apply...

comment:2 Changed 4 years ago by Ulrich Petri

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

I've turned the ad-hoc code in the description into a proper patch for AdminURLFieldWidget and added tests as well as a short mention of this new feature in the docs.

Code at: https://github.com/django/django/pull/190

Changed 4 years ago by Tomáš Ehrlich

Attachment: 17549.patch added

Fixed force_unicode in AdminURLFieldWidget

comment:3 Changed 4 years ago by Tomáš Ehrlich

Cc: tomas.ehrlich@… added
Version: 1.3master

Pull request has broken tests with latest checkout of Django:

NameError: global name 'force_unicode' is not defined

in django/contrib/admin/widgets.py.

I've just renamed force_unicode to force_text and tests passed.

comment:4 Changed 4 years ago by Florian Apolloner <florian@…>

Resolution: fixed
Status: newclosed

In ac2052ebc84c45709ab5f0f25e685bf656ce79bc:

Fixed #17549 -- Added a clickable link for URLFields in admin change list.

comment:5 Changed 4 years ago by Florian Apolloner <florian@…>

In f12fa7750c95cafa3a317be6a62ed3a2f53caf3c:

[1.5.X] Fixed #17549 -- Added a clickable link for URLFields in admin change list.

Backport of ac2052ebc84c45709ab5f0f25e685bf656ce79bc from master.

Note: See TracTickets for help on using tickets.
Back to Top