I store blogs posts as HTML so that I can custom format them. Using Django framework, I load the blog post HTML from an database into a content block of a subclassed template. I would like have an image in the blog post. How do I format the image src to load an image that is stored in my static folder.
The example that the Django tutorial (https://docs.djangoproject.com/en/2.1/intro/tutorial06/) gives:
<img src="{% static assets/img/some_img.jpg %}" />
does not work inside a content block. But, I've been successfully using the following syntax, which I found from here (django 1.5 - How to use variables inside static tag) hard coded in template pages:
<img src="{% with assets/img/some_img.jpg as img_url %}{% static img_url %}{% endwith %}" />
Here is a generic example of my setup:
Database Record
ID=1
Title="Blog Title"
Content="<p><img src="{% with assets/img/some_img.jpg as img_url %}{% static img_url %}{% endwith %}" /></p>
"
base_template.html
<html>
<body>
{% block content %}{% endblock %}
</body>
</html>
blogpost.html
{% extends 'base_template.html' %}
{% block content %}
{{database_record.Content}}
{% endblock %}
Expected Result:
<html>
<body>
<p><img src="/static/assets/img/some_img.jpg" /></p>
</body>
</html>
Actual Result:
<html>
<body>
<p><img src="{% with assets/img/some_img.jpg as img_url %}{% static img_url %}{% endwith %}" /></p>
</body>
</html>
The problem is that neither of my attempts to load static image URLs work when the HTML is loaded from a database. The tags are displayed in rendered HTML just as they are seen above. The framework must load content from the database after it converts the curly bracket tags.
How can I link to static images from HTML that gets loaded from a database?