>>> from assemble_src import get_pic
>>> get_pic("levis_white.png")
"{%static 'tshirts/images/levis_white.png' %}"
which is what I want in html as src in the img tag. the file name comes from a postgres table. so don’t mind the name of the file which is different. I place the string in row.1
This is what django makes of it:
The Django template language will not parse a template twice, it would expect {{ row.1 }} to output HTML not another template language stanza.
However if you are getting a filename dynamically then I would recommend reading up on Django MEDIA files which are to do with user uploaded content (be those users and admin or a visitor to your site)
Thank you for your reply, but this is not what I’m looking for. When I paste the output of my module in html, it works. When I use a view, the string is changed into nonsense. How can I make the view put the string in html without changing it. I probably need something like htmlencode or so.
1.Get a filename from postgress
2. create a string to be used in a template img tag as src.
3. pass a variable with the string to the template
4. display an image (the image is in the statc images dir)
Right ok. So there are 2 issues (or misunderstandings) here.
When dealing with dynamic images/files that are referenced in the database an ImageField along with media settings ought to be the preferred solution generally
Django does not parse a template twice, hence the result you are getting as you are returning Django template code which is not valid HTML
"{% static 'print_of_love.png' %}" this is Django template code not HTML {{ pic }} this is Django template code not HTML
Perhaps you want this? "{% static pic %}" where pic is the path to be passed to the static tag?
or {{ pic }} needs to just return the absolute path to the image (which is what an ImageField would do)?
this one did it. I got confused because enclosed pic in curly brackets, and that didn’t work. then I got lost trying to get the complete string in pic.