Using % include 'includes/some.html' % in django template

Using % include 'includes/some.html' % in django template



I have a template as under:-


<div class= "comment-form m-2">
% include "includes/comment_reply_form.html" %</div>



If the user is authenticated then the html shows the form. However if the user is not logged in I want to show a Login button. I am handling the same by the following javascript.


$(".comment-reply-btn").click(function(event)
event.preventDefault();
if (user_is_authenticated === false)
$(".comment-form").html('% include "includes/reply.html" %');
$(this).parent().next(".comment-reply").fadeToggle();


else
$(this).parent().next(".comment-reply").fadeToggle();


);



But, instead of including the html in from reply.html, it is showing % include "includes/reply.html" % in the webpage. There is no problem with the javascript and if I add


<p> You need to Login </p>



in the following line. It works fine :-


$(".comment-form").html('% include "includes/reply.html" %');



But since I need more options in my reply.html I want to add it as includes. I have tried using the escape character "/" in front of { but it didn't help.





Well the problem with this is: if the % include ... % renders a quote ('), then the string stops (according to JavaScript), so then we are in real trouble.
– Willem Van Onsem
Aug 19 at 19:50


% include ... %


'





But it is showing % include "includes/reply.html" % in the html, but html is not picking it up as a django template may be javascript is adding this as "% include "includes/reply.html" % "
– Udit Hari Vashisht
Aug 19 at 19:52





Is this JavaScript in the template, or a "static" .js file. Django templates are rendered at server side, so at the client, the % include ... % has no meaning at all...
– Willem Van Onsem
Aug 19 at 19:54


% include ... %





it is in my base.js file. And as I have mentioned if I replace include statement with a simple paragraph tag in my javascript it works fine.
– Udit Hari Vashisht
Aug 19 at 19:56





of course it works fine, since, as said, the base.js is not interpreted by the Django engine. The client sees this % include % tag, and thus will not replace it, since a browser can not read Django template language (and even if it could, it has no access to the templates, files, models, views, etc.)
– Willem Van Onsem
Aug 19 at 19:59


base.js


% include %




2 Answers
2



not familiar but try


<div class= "comment-form m-2">
<script>
document.write('% include "includes/comment_reply_form.html" %')
</script>
</div>





The first template works fine, because it is in the html. The problem is when I am trying to add some other include statement using javascript.
– Udit Hari Vashisht
Aug 19 at 19:49





Maybe you cannot do it dynamically trough javascript, try some server side commands maybe or do some bad workaround like include it in a hidden div element and then just unhide it...
– Tomi Jurukovski
Aug 19 at 19:54



Instead of putting the value from include in your javascript function just put it in the html with with display = none for example


<div id="comment-reply-form-template">
% include "includes/comment_reply_form.html" %



Then in your javascript you can always clone the already rendered form when you need to...



Or you could make a custom template tag that will print the html in javascript string style with a bunch of '' + '' + ''



or prehaps look into the new es6 template strings
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/template_strings



I think would work like




`% include "includes/comment_reply_form.html" %`



using the ` character






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.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

ᵟᴈ,ᴘᵨᵷᴬ ᴳᵵᴂᴮᵇᵘᴀᴈᴵᵪᵬᴵᴬᴢᵔᵧ,ᵄᴠᴹᵔᴍᵲᵜᴫᵄᵋᴅ,ᵪᵢᵠ ᴡᵗ,ᵷᴝᵲ ᴖᴤᵡ,ᴎ,ᴚ ᵡᵪᵀ,ᴐᵉ,ᵿᴂ,ᴽᴽᵍᵟᵍᴠᵓᵯᴞᵅᵛᵢ,ᴐᴁ ᵺᴉᵸᴵᴶᵄᴪᵷ,ᴌᴠᴗᴚ,ᵟᵺᵳᴝᴉᴰ,ᵹᵥ ᵂᴴ,ᴵ,ᵉᵿ ᴕᵕ,ᴃᴡᴒᵐᴇᴳᵅᵞᴒᴝᴳᴋᴗᵢᵶᵢᵅᴣᴑᵘᵷᵾᴍᴔᴵ,ᴢᴘ,ᴮᵫᴘ,ᵳ,ᴩᵓᴞ