#17549 closed New feature (fixed)
Add link to open URL from URLField
| Reported by: | Chris Wilson | Owned by: | nobody | 
|---|---|---|---|
| Component: | contrib.admin | Version: | dev | 
| 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)
Change History (6)
comment:1 by , 14 years ago
| Component: | Forms → contrib.admin | 
|---|---|
| Needs documentation: | set | 
| Needs tests: | set | 
| Patch needs improvement: | set | 
| Triage Stage: | Unreviewed → Accepted | 
comment:2 by , 13 years ago
| 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.
comment:3 by , 13 years ago
| Cc: | added | 
|---|---|
| Version: | 1.3 → master | 
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 by , 13 years ago
| Resolution: | → fixed | 
|---|---|
| Status: | new → closed | 
  Note:
 See   TracTickets
 for help on using tickets.
    
I suppose the code in the ticket description is the patch; a unified diff would be much easier to apply...