0

I have a page with a simple text and a button in a form

<html>
<head>
</head>
<body>
    <h1>JASON</h1>
    <form>
      <button type="submit" formmethod="POST">Activate</button>
      <br>
      <input type="hidden" value="act.12344" name="sub" />
    </form>
</body>
</html>

And this python script

from flask import Flask, render_template, request, redirect

app = Flask(__name__)

@app.route('/atdt', methods=['GET', 'POST'])
def atdt():
    if request.method == 'POST':
        print('post')
        requested = request.form['sub']
        ver = str(requested).split('.')
        if ver[0] == 'act':
            print('act')
            modif(ver[1])                        #this func modifies the index page
            return render_template('index.html')

    else:
        return render_template('index.html')

The point of the script is to change the name jason in something else...and it works well, the page is changed and its all good

But my flask program wont show it...the '.html' page its changed, and if I open it manually, outside the program it works!

But if i give python the line return render_template('index.html') but it wont render it If i try to refresh manually it will just show me the old page

Any help ?

rapidDev
  • 109
  • 2
  • 13

1 Answers1

0

You are not modifying the html, You are just calling a function that returns modified version of an input!

First of all you have to use tempalte engine

Your HTML Should be something like this:

<html>
<head>
</head>
<body>
    <h1>{{name}}</h1>
    <form>
      <button type="submit" formmethod="POST">Activate</button>
      <br>
      <input type="hidden" value="act.12344" name="sub" />
    </form>
</body>
</html>

And your view should look like this:

@app.route('/atdt', methods=['GET', 'POST'])
def atdt():
    if request.method == 'POST':
        print('post')
        requested = request.form['sub']
        ver = str(requested).split('.')
        if ver[0] == 'act':
            print('act')
            name = modif(ver[1])                        #this func modifies the index page
            return render_template('index.html', name=name)

    else:
        return render_template('index.html', name="JASON")

The template engine will handle The name change

Flask uses Jinja2 Template Engine, You can read more about it here

DarkSuniuM
  • 2,523
  • 2
  • 26
  • 43