Home How to use autoescape off AND static variables inside django template
Reply: 0

How to use autoescape off AND static variables inside django template

user47635
1#
user47635 Published in September 19, 2018, 9:20 am

I'm using auto generated HTML which has been saved to a file and then read in again to use as part of a page in a django template.

In order to do this I have used:

{% autoescape off %}
  {{ my_html }}
{% endautoescape %}

However, in the my_html variable, I have some static content. This comes in the form of something like this:

<img src="{% static "img/tree_report.png" %}" width="12px" height="12px"/>

My issue is that the static content is not displayed. I get:

http://127.0.0.1:8000/%7B%%20static 404 (in the browser error report)

I read something about get_static_prefix in another question but that doesn't solve my problem because I just get this instead:

GET http://127.0.0.1:8000/%7B%%20get_static_prefix%20%%7Dimg/tree_report.png 404 (Not Found)

I also tried endautoscape turning on and off periodically in my_html in the saved HTML variable. That also didn't work.

Should I be autogenerating the development and production static files paths for my_html or is there a more elegant solution to this problem?

Any suggestions are most welcome. Thanks.

share|improve this question
  • If I could lose the inverted commas around my html variable which is the default before using autoescape off then I wouldn't need to use autoescape off which would be another alternative but I don't know any way of doing this. – Rodent Feb 12 at 11:07
  • Try using {{ my_html|safe }} instead of putting {% autoescape off %} – Aaron Garton Feb 12 at 11:12
  • Thanks for the suggestion @user2556381, I was not aware of that possibility; however, unfortunately I still get: GET 127.0.0.1:8000/%7B%%20static 404 (Not Found) – Rodent Feb 12 at 11:23
  • Have you tried setting STATIC_URL = '/static/' in your settings.py file? And then add {% load staticfiles %} above all links and scripts – Aaron Garton Feb 12 at 11:31
  • Naturally. Normal static files which aren't embedded in the autoescape off variable work fine. FWIW I have this in settings.py: STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) STATIC_ROOT = os.path.join(BASE_DIR, 'static_files') and in the template I have {% load static %} in the header. I have another webapp which is already deployed which works fine with static files that this one is modelled on so I am sure that my static files config in general is fine. – Rodent Feb 12 at 11:37

active oldest votes

Your Answer

StackExchange.ifUsing("editor", function () { StackExchange.using("externalEditor", function () { StackExchange.using("snippets", function () { StackExchange.snippets.init(); }); }); }, "code-snippets"); StackExchange.ready(function() { var channelOptions = { tags: "".split(" "), id: "1" }; initTagRenderer("".split(" "), "".split(" "), channelOptions); StackExchange.using("externalEditor", function() { // Have to fire editor after snippets, if snippets enabled if (StackExchange.settings.snippets.snippetsEnabled) { StackExchange.using("snippets", function() { createEditor(); }); } else { createEditor(); } }); function createEditor() { StackExchange.prepareEditor({ heartbeatType: 'answer', convertImagesToLinks: true, noModals: false, showLowRepImageUploadWarning: true, reputationToPostImages: 10, bindNavPrevention: true, postfix: "", onDemand: true, discardSelector: ".discard-answer" ,immediatelyShowMarkdownHelp:true }); } });
 
StackExchange.ready( function () { StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48744971%2fhow-to-use-autoescape-off-and-static-variables-inside-django-template%23new-answer', 'question_page'); } );

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged python html django templates tags or ask your own question.

StackExchange.ready(function(){$.get('/posts/48744971/ivc/0431');});
StackExchange.ready(function () { StackExchange.responsiveness.addSwitcher(); }) (function(i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function() { (i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m); })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga'); StackExchange.ready(function () { StackExchange.ga.init({ sendTitles: true, tracker: window.ga, trackingCodes: [ 'UA-108242619-1' ] }); StackExchange.ga.setDimension('dimension2', '|python|html|django|templates|tags|'); StackExchange.ga.setDimension('dimension3', 'Questions/Show'); StackExchange.ga.trackPageView(); }); /**/ var _qevents = _qevents || [], _comscore = _comscore || []; (function() { var ssl = 'https:' == document.location.protocol, s = document.getElementsByTagName('script')[0], qc = document.createElement('script'); qc.async = true; qc.src = (ssl ? 'https://secure' : 'http://edge') + '.quantserve.com/quant.js'; s.parentNode.insertBefore(qc, s); _qevents.push({ qacct: "p-c1rF4kxgLUzNc" }); /**/ var sc = document.createElement('script'); sc.async = true; sc.src = (ssl ? 'https://sb' : 'http://b') + '.scorecardresearch.com/beacon.js'; s.parentNode.insertBefore(sc, s); _comscore.push({ c1: "2", c2: "17440561" }); })();
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.340716 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO