5

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'.

2
  • 2
    the bad indentation in the line Calculator.time_required(initial,rate,total)is just a typo, right? Commented Apr 13, 2018 at 17:55
  • 1
    Yup it is!Edited! Commented Apr 13, 2018 at 17:57

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Hi there.Now the error is 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'.
It is now showing error message as said in the edit section of the question above.There seems to be two errors including this now.
Every {% block content %} segment must be closed with a {% endblock %}. Add one at the end. Also, why are you putting an ENTIRE web page into the block? Is your base.html empty? base.html should (in most cases) be a complete html document, into which you insert content using {% block ... %}...{% endblock %}
@GAEfan Thanks!!Just realized the problem.As for the base.html,Its suppose to just contain the header title for the file as part of the specifications.
@Harris, answer updated after your edit. If it solved your problem, feel free to accept it : )
2

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.

1 Comment

Thank you for this, I was going crazy. I couldn't find this mentioned anywhere but here
0

My half a day gone just breaking my head around it. Such a sad state, Jinja really sucks...the message was not clear, I literally had to run through the entire code to check for matching if/for what else... Now I removed all the comments from the page and it worked.. There were no blocks inside such comments..even then it worked... Angry with Jinja...

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.