DevGuyAhnaf
DevGuyAhnaf

Reputation: 149

Flask - Jinja2 not rendering placeholder (variable) in form input

I'm trying to render a placeholder of form input on my website. I'm using Python Flask including Jinja 2. I have a list of strings in the python script and I randomly select one to render using Jinja2.

It's name is cplace (variable). But when I use this variable in Jinja2 like {{cplace}}, it doesn't render the value. It only renders '{{cplace}}'.

from flask import Flask, render_template
from flask_wtf import Form 
from wtforms import StringField
from random import choice
from wtforms.validators import InputRequired, Email, Length, AnyOf
import random
from flask_bootstrap import Bootstrap

app = Flask(__name__)
Bootstrap(app)
app.config['SECRET_KEY'] = 'iamahnaf'



random_placeholders = ['What is Minecraft?', 'Minecraft VS Roblox', 'Why PUBG sucks..', 'Python VS C++', 'Bootstrap VS Semantic']



@app.route("/")
def hello():
    cplace = str(random.choice(random_placeholders))
    print(cplace)
    return render_template('index.html', cplace = cplace)


if __name__ == "__main__":
    app.run()
{% import "bootstrap/wtf.html" as wtf %}
<!DOCTYPE html>

<html lang="en">
    <head>
        <title>Welcome to Ahnaf Zamil's Website</title>
        <meta charset="utf-8">
        <link rel="shortcut icon" href="../static/images/favicon.ico" sizes="64x64">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script type="text/javascript" src="../static/js/bootstrap.min.js"></script>

    </head>
    <body>

    <form class="form-inline my-2 my-lg-0">
      <input type="text" class="form-control" id="srch" name="search" placeholder={{cplace}}>
      <button class="hvr-radial-out" type="submit">Search</button>
    </form>

    </body>
</html>

Upvotes: 0

Views: 1975

Answers (1)

Fabrice Fabiyi
Fabrice Fabiyi

Reputation: 361

his lack of quotes to the placeholder. Try it.

<input type="text" class="form-control" id="srch" name="search" placeholder="{{ cplace }}">

Upvotes: 4

Related Questions