Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

django how to hide url and show renamed value?

I am using django-tables2 and trying to hide url and rename it on field. for example, url link is www.youtube.com but on actual field, I want it to show as ‘link’ instead of revealing entire url link. how do I achieve this?

tables.py

class MyVideoTable(tables.Table):

    class Meta:
        model = PPVideo
        fields = ('title', 'url')

models.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class PPVideo
       title = models.CharField('Title', max_length=100, null=True)
       url = models.URLField('URL', max_length=150, null=True)

>Solution :

You can define a .render_url(…) method to specify how to render this column:

from django.utils.html import format_html

class MyVideoTable(tables.Table):
    
    def render_url(self, value, record):
        return format_html('<a href="{}">link</a>', value)
    
    class Meta:
        model = PPVideo
        fields = ('title', 'url')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading