3

I have a python function edited from this post, this creates an html code to display the images in a folder and it works fine ( I run it independently and copy paste the code in the template ) but now I want to make it inside Django so I create a templatetag but I only get plain text.

from django import template
import os 
from pathlib import Path
register = template.Library() 
@register.simple_tag
def generate_tree():
 for file in os.listdir(path):
 rel = path + "/" + file
 if os.path.isdir(rel):
 html += """<button type="button" class="collapsible">%s</button>"""% (file)
 html +='<div class="content">'
 html += generate_tree(rel)
 html += '</div>'
 else:
 html +='<div class="col-md-4 d-flex align-items-stretch"> <picture>'
 x=str(rel)[38:]
 html += """<img src="{% static "%s" """% ('% s',x)
 html += """%} " class="lazy card-img-top" >"""
 html +='</picture> </div>'
 return html

this is how i call my function in the html file:

{% load static html_tags %}
{% generate_tree 'path' %}

and this is a portion of the plain text i get

enter image description here

any ideas on how can I get the text like html code?

asked Jul 23, 2020 at 3:03

3 Answers 3

4

I found the answer in this post this post. all i needed to do was to import mark_safe and use it the function

from django.utils.safestring import mark_safe
from django import template
import os 
from pathlib import Path
register = template.Library() 
@register.simple_tag
def generate_tree():
 for file in os.listdir(path):
 rel = path + "/" + file
 if os.path.isdir(rel):
 html += """<button type="button" class="collapsible">%s</button>"""% (file)
 html +='<div class="content">'
 html += generate_tree(rel)
 html += '</div>'
 else:
 html +='<div class="col-md-4 d-flex align-items-stretch"> <picture>'
 x=str(rel)[38:]
 html += """<img src="{% static "%s" """% ('% s',x)
 html += """%} " class="lazy card-img-top" >"""
 html +='</picture> </div>'
 return mark_safe(html)
answered Jul 23, 2020 at 16:49
Sign up to request clarification or add additional context in comments.

Comments

1

make sure you use like this {{ text|safe }} in your template.

https://docs.djangoproject.com/en/4.1/ref/templates/builtins/

answered Oct 3, 2022 at 12:36

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Actually it returns a string value so look up for a attribut or js function that will remove the strings

answered Jul 23, 2020 at 3:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.