Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Flask environment using HTML:receiving error message of expected else statement

I am getting an error message as follows: TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'.

I am using a Linux environment and this code should calculate interest rates.There are seperate files for running the scripts and the function.However once running the program i am getting an error message at the page stating that theres an expected else statement before the following code:

return render_template('interest_form.html', calc_total=True).

Below is the full code:

from server import app, valid_time
from flask import request, render_template
from Calculator import Calculator


@app.route("/", methods=["POST", "GET"])
def interest_total():
    if request.method == 'POST':
        initial=float(request.form["initial"])
        rate=float(request.form["rate"])
        time=float(request.form["time"])
        Calculator.total_interest(initial,rate,time)
        total=Calculator.total_interest()
        return render_template('interest_form.html', total=total)
    return render_template('interest_form.html', calc_total=True)


@app.route('/time', methods=["POST", "GET"])
def time_interest():
    if request.method == 'POST':
        initial=float(request.form["initial"])
        rate=float(request.form["rate"])
        total=float(request.form["total"])
        Calculator.time_required(initial,rate,total)
        time=Calculator.time_required()
        return render_template('interest_form.html', time=time)
    return render_template('interest_form.html', calc_time=True)



@app.route('/credits', methods=['GET'])
def credits():
    return render_template('credits.html')

I am trying using a html form to send the input:

<!doctype.html>

<head><h2>Interest</h2><head>
<html>
<body>
<div>
    <form action="routes.py" method='POST'>
         <div style="margin: 10px 0px">
            <label>Amount Invested ($): </label><br/>
            <input  name="initial" placeholder="Amount Invested"/>
        </div>
        <div style="margin: 10px 0px">
            <label>Interest Rate (%): </label><br/>
            <input  name="rate" placeholder="Amount Invested"/>
        </div>
         <div style="margin: 10px 0px">
            <label>Time Investment (Years): </label><br/>
            <input name="time" placeholder="Amount Invested"  {%if calc_time %}disabled{% endif %}/>
        </div>
         <div style="margin: 10px 0px">
            <label>Total Interest ($): </label><br/>
            <input  name="total" placeholder="Amount Invested"  {%if calc_total %}disabled{% endif %}/>
        </div>
            <div style="margin: 10px 0px">
            <button type="submit">Submit</button>
        </div>
        <div><p>{{initial}} and {{time}}</p></div>

        {% if total %}<h4>Total amount is<h4>
        <textarea name="Amount" rows="5" cols="20"> {{total}} </textarea>
    </form>
</div>
<div>
<a href="file:///tmp_amd/cage/export/cage/4/project/templates/interest_form.html/time">Time Form</a>
<a href="file:///tmp_amd/cage/export/cage/4/project/templates/interest_form.html">Total Form</a>
<br/><a href="/credits">Credits Page</a>
</div>
</body>
</html>

I have used a different template code that has a similar function and return format and when running it there seems to be no problem with it.This,however,seems to be showing the abovementioned error message.

(EDIT:I have updated the following code):

                {% if total %}<h4>Total amount is</h4>
                <textarea name="Amount" rows="5" cols="20"> {{total}}</textarea>
                {% endif %}
                </form>

Howver the error message is now showing: jinja2.exceptions.TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: 'endblock'. The innermost block that needs to be closed is 'block'.

like image 236
Harris Avatar asked Mar 07 '23 10:03

Harris


2 Answers

You need to close the if block in your template:

 {% if total %}
        <h4>Total amount is<h4>
        <textarea name="Amount" rows="5" cols="20"> {{total}</textarea>
    </form>
 {% endif %}

Updated answer after the edit:

You need also an {% endblock %}at the end of the file for closing the {% block content %}at the top of the file.

like image 179
floatingpurr Avatar answered Mar 11 '23 18:03

floatingpurr


I was getting a similar error - cause was a {% block content %} which I thought would not be affecting as it was commented out in HTML - turns out it was the culprit.

details:

jinja2.exceptions.TemplateSyntaxError: Unexpected end of template. Jinja was looking for the following tags: 'endblock'.The innermost block that needs to be closed is 'block'.

I know that each {% block content %} needs an {% endblock %} but what I didn't know was that if you have HTML comments around a {% block content %} , it doesn't work(i.e. it doesn't really get commented)

just putting it here in case anyone struggles the way I did for long before figuring it out. below in my html code, although this was commented, it was the culprit that was giving me that exception. `

<!-- 
 {% block content %}
<table>
    <tr valign='top'>
        <td> <img src="{{ user.avatar(128) }}"></td>
 -->

removing the {% block content %} from commented code solved it.

like image 26
awkward101 Avatar answered Mar 11 '23 18:03

awkward101