K. K.
K. K.

Reputation: 572

Python Flask render_template returns HTML script instead of redirecting to the HTML page

I have a Python script that uses Flask web framework to let users ask a question and depending on some certain questions, the application should ask back some questions to the user on a second webpage. The answers to the questions are evaluated based on the questions and displayed on the initial webpage.

model.py

### Importing Flask ###
from flask import Flask, render_template, request, session, redirect, url_for

### Initializing Flask ###
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('init.html')

@app.route('/handle_data', methods = ['POST', 'GET'])
def handle_data():

    ### User Inputs Question ###
    userQuestion = request.form['userQuestion']

    def model():

        message = "Depends on User"

        if message == "Depends on User":

            return render_template('user_information.html', userQuestion = userQuestion)

        else:

            message = "Your answer is ABC."

        return message

    message = model()

    return render_template('init.html', userQuestion = userQuestion, message = message)


@app.route('/user_information', methods = ['POST', 'GET'])
def user_information():

    userLevel = request.form['userLevel']
    userDOJ = request.form['userDOJ']
    userType = request.form['userType']

    message = "Answer for Eligibility."

    return render_template('init.html', userLevel = userLevel, userDOJ = userDOJ, userType = userType, message = message)

if __name__ == '__main__':
   app.run()

These are my two HTML files:

init.html (initial webpage)

<!DOCTYPE html>
<html>
<head>
<title>Human Resources</title>
<!-- for-mobile-apps --> 
</head>
<body>
    <div class="main">
        <div class="w3_agile_main_grid">
            <h2>Human Resource Portal</h2>
            <br>
            <p>Hi</p>

            <form action="{{ url_for('handle_data') }}" method="post" class="agile_form">
                <input type="text" name="userQuestion" placeholder="Ask your question..." required="">
                <input type="submit" value="Submit">
            </form>

            <p>{{ message }}</p>


        </div>

    </div>
</body>
</html>

user_information.html (second webpage)

<!DOCTYPE html>
<html>
<head>
<title>Human Resources</title>
</head>
<body>
    <div class="main">
        <div class="w3_agile_main_grid">
            <h2>Human Resource Portal</h2>

            <form action="{{ url_for('user_information') }}" method="post" class="agile_form">
                <!--<input type="text" name="userName" placeholder="Enter your name." required="">-->
                <input type="text" name="userLevel" placeholder="What is your level?" required="">
                <input type="text" name="userDOJ" placeholder="What is your date of joining?" required="">
                <input type="text" name="userType" placeholder="Are you on sabbatical or specialist?" required="">
                <input type="submit" value="Submit">
            </form>

        </div>

    </div>
</body>
</html>

When I execute my script and enters a question, what I get is the HTML code for user_information.html as my answer which is not what I want.

Ouput after I click Submit: https://ibb.co/cwhRpk

Expected output after I click Submit: https://ibb.co/c7CFh5

https://ibb.co/dX9T25

I can get the desired output if I remove the model() construct but that will make my code inefficient because in my actual application I have to call model() multiple times with different parameters.

Can anyone please suggest me what approach should I take? I'm totally stuck in this part. Thanks, any help is appreciated!

Upvotes: 0

Views: 2199

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599470

Your nested model() function does not make any sense at all. It returns the result of render_template, which is a complete response including HTTP headers etc. If you try and insert that into another template, Jinja will be forced to try and convert it to a string, which gives the result you see.

This is not at all the way to compose templates. Jinja supports template inheritance; you should call render_template once only, using a child template that inherits from a common base.

Upvotes: 1

Related Questions