Hello,
I am trying to implement the render_to_string function in a Django project.
I have an HTML template (strategy.html) containing many Django Template Language variables and ‘if conditions’ and would like to save the rendered output in a DB model.
The output is saved in the db model but the context variables are ignored.
The action has to be triggered once the ‘Save’ button is clicked (see code below).
Important note: the DTL variables and ‘if conditions’ are correctly displayed when using the render function by clicking the ‘Generate’ button.
Here is the views.py file:
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.template.loader import render_to_string
from strategiesAPI.models import strategiesList
@login_required
def index(request):
if request.method == "POST":
# Get form information
starttime = request.POST.get("starttime")
endtime = request.POST.get("endtime")
trigger = request.POST.get("trigger")
entrieslimit = request.POST.get("entrieslimit")
direction = request.POST.get("direction")
pattern = request.POST.get("pattern")
stoploss = request.POST.get("stoploss")
takeprofit = request.POST.get("takeprofit")
tradeliquid = request.POST.get("tradeliquid")
# Context variables for the render and render_to_string function
content = {
"starttime": starttime,
"endtime": endtime,
"trigger": trigger,
"entrieslimit": entrieslimit,
"direction": direction,
"pattern": pattern,
"stoploss": stoploss,
"takeprofit": takeprofit,
"tradeliquid": tradeliquid
}
# Action based on the button pressed inside the index.html template
# GENERATE button
if request.POST['button'] == 'Generate':
# Write strategy on the right side of the page using the user's inputs
return render(request, "composer/index.html", content)
# SAVE button
if request.POST['button'] == 'Save':
# Save the strategy DTL output into the db model using the render_to_string function
strategy_html = render_to_string("composer/strategy.html", content)
strategiesList.objects.create(script=strategy_html)
return render(request, "composer/index.html")
else:
return render(request, "composer/index.html")
Below I will paste what is saved in the db model followed by the strategy.html file (which is part of the main page index.html) containing the DTL variables and ‘if conditions’ (which are ignored):
<!-------------------------------------- Right-side EasyLanguage & DTL output -------------------------------------->
<div class="right" id="output" style="font-size: 11px;">
// EasyLanguage code composer by ©SOACM. All rights reserved // <br>
<br>
<!-------------------------------------- Breakout -------------------------------------->
<!-------------------------------------- Breakout end ----------------------------------->
<!-------------------------------------- Reversal --------------------------------------->
<!-------------------------------------- Reversal end ------------------------------------->
</div>
<!-------------------------------------- Right-side EasyLanguage & DTL output -------------------------------------->
<div class="right" id="output" style="font-size: 11px;">
// EasyLanguage code composer by ©SOACM. All rights reserved // <br>
<br>
<!-------------------------------------- Breakout -------------------------------------->
{% if trigger == "breakout" %}
input: StartTime({{ starttime }}), EndTime({{ endtime }}), MyStopLoss({{ stoploss }}), MyTakeProfit({{ takeprofit }}); <br>
var: bool MyTime(False), int MP(0); <br>
MP = MarketPosition; <br>
MyTime = False; <br>
if Time >= StartTime and Time <= EndTime and EntriesToday(date[0]) < 1 then MyTime = True; <br>
<br>
{% if pattern == "none" %}
// Levels <br>
if date <> date[1] then begin <br>
var: highd1(0), lowd1(0); <br>
highd1 = HighS(1); <br>
lowd1 = LowS(1); <br>
end; <br>
{% endif %}
<br>
Thank you in advance.
EDIT: can the issue be caused by Django’s security features like the make_safe function?