69👍
Define a custom method in your LawyerAdmin class that returns the link as HTML:
def show_firm_url(self, obj):
return '<a href="%s">%s</a>' % (obj.firm_url, obj.firm_url)
show_firm_url.allow_tags = True
See the documentation.
153👍
Use the format_html
utility. This will escape any html from parameters and mark the string as safe to use in templates. The allow_tags
method attribute has been deprecated in Django 1.9.
from django.utils.html import format_html
from django.contrib import admin
@admin.display(description="Firm URL")
class LawyerAdmin(admin.ModelAdmin):
list_display = ['show_firm_url', ...]
...
def show_firm_url(self, obj):
return format_html("<a href='{url}'>{url}</a>", url=obj.firm_url)
Now your admin users are safe even in the case of:
firm_url == 'http://a.aa/<script>eval(...);</script>'
See the documentation for more info.
- [Django]-Multiple Models in a single django ModelForm?
- [Django]-Django.db.migrations.exceptions.InconsistentMigrationHistory
- [Django]-Images from ImageField in Django don't load in template
- [Django]-How can one use enums as a choice field in a Django model?
- [Django]-Trying to parse `request.body` from POST in Django
- [Django]-Disable migrations when running unit tests in Django 1.7
5👍
But it overrides the text display specified in my models and displays “show firm url” on the head of the column
You can change it by assigning short_description property:
show_firm_url.short_description = "Firm URL"
- [Django]-Annotate a queryset with the average date difference? (django)
- [Django]-How to write setup.py to include a Git repository as a dependency
- [Django]-Django: Arbitrary number of unnamed urls.py parameters
5👍
You can handle it in the model if you prefer:
In models.py :
class Foo(models.Model):
...
def full_url(self):
url = 'http://google.com'
from django.utils.html import format_html
return format_html("<a href='%s'>%s</a>" % (url, url))
admin.py:
list_display = ('full_url', ... )
- [Django]-How to set True as default value for BooleanField on Django?
- [Django]-Resize fields in Django Admin
- [Django]-Testing email sending in Django
1👍
its more easy..
#in your admin.py add
from django.utils.html import format_html
##Code:
#url_file = "your field contain de url of file" example: http://127.0.0.1:8000
/backdown/respaldo.zip
list_display = ["url_file","download_content"]
#Link download
def download_content(self, obj):
return format_html('<a href="%s">%s</a>' % (obj.url_file, "Download"))
download_content.allow_tags = True
download_content.short_description = "Download Content File"
##in your html see like this..
<a href="http://127.0.0.1:8000/backdown/respaldo.zip">http://127.0.0.1:8000/backdown/respaldo.zip</a>
- [Django]-Django 1.4 – can't compare offset-naive and offset-aware datetimes
- [Django]-Referencing multiple submit buttons in django
- [Django]-How to access the local Django webserver from outside world