Python / Django render_to_string outpu rendered as text with html entities

Viewed 23

I am porting a python / Django application from: Django==1.5.1 python version 2.6.6

to

Django==3.2 python version 3.6.8

The issue that I am having is that I have a section of code that renders to string, a particular HTML template, and then adds that to list to output elsewhere.

The actual code that produces this HTML is:

class AccountAdmin(SmarterModelAdmin):
   list_display = ('username', 'files_url', 'teamMembers', 'roleMemberships', 'safetyLimit', 'admin_quota', 'manager', 'enabled', 'path')
   list_filter = ['enabled', 'manager', 'accountType']
   search_fields = ['username']
   inlines = [RolesInline]
   valid_lookups = (
      'members__member__username',
      'teams__role__username',
   )
      roles = Account.objects.filter(teams__member=account).order_by('username')
      roleList = []
      for role in roles:
         link = '/admin/files/account/?teams__role__username=' + role.username
#        mylink = '<a href="{myurl}">'+role.username+'</a>'
#        linkText = format_html(mylink,myurl=link)
         linkText = render_to_string('common/contact.html', context={'URL': link, 'contact': role})
         roleList.append(linkText)
      return ', '.join(roleList)
   roleMemberships.short_description='Roles'
   roleMemberships.allow_tags=True```

I have added in a logging.warn to validate what comes out of the render_to_string, and it is straight HTML.

The commented out lines were something that I tried that fixed a similar issue.

common/contact.html is:

<a
   href="{{ URL }}"
   {% if not contact.enabled %}
      style="text-decoration:line-through;"
   {% endif %}
>{{ contact.username }}</a>

However, on the final render, It comes out like this:

<a href="/admin/files/account/?teams__role__username=abaumann" >abaumann</a>, <a href="/admin/files/account/?teams__role__username=abaumann">abaumann</a>

which when run through a browser looks like this: django admin output

I haven’t been able to find anything that references this particular issue. I have everything else working in the admin section of the django application, and I am at a loss for why it is not rendering as expected.

Please let me know if there is anything else that you need to see.

0 Answers
Related