Home > Net >  django how to hide url and show renamed value?
django how to hide url and show renamed value?

Time:03-08

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

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

CodePudding user response:

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')
  • Related