Issue
I've written some HTML and Flask code to run, but it doesn't work with templates. It needs to show the results of the interview of the astronaut. The information is given in the variable data, which is dictionary with keys, referring to name, surname, sex and education of the astronaut.
My structure of files: file "finder.py" and directory "templates" with two templates - "auto_answer.html" and "base.html".
finder.py:
from flask import Flask
from flask import render_template
app = Flask(__name__)
@app.route("/")
def main():
return "nothing"
@app.route("/answer")
@app.route("/auto_answer")
def answer():
data = {
"title": "Анкета",
"surname": "Anchikov",
"name": "Timothy",
"education": "среднее",
"profession": "экзобиолог",
"sex": "male",
"motivation": "Хочу стать одним из первых колонизаторов Марса!",
"ready": True
}
return render_template(["base.html", "auto_answer.html"], title=data["title"], data=data)
if __name__ == "__main__":
app.run(port=8080, host="127.0.0.1")
base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl"
crossorigin="anonymous">
<style>
h3 {
margin-left: 60px;
margin-top: 20px;
}
ul {
margin-left: 90px;
}
ol {
margin-left: 90px;
}
</style>
</head>
<body>
<h1>Миссия Колонизация Марса</h1>
<h4>И на Марсе будут яблони цвести!</h4>
<div id="main-block">
{% block content %}{% endblock %}
</div>
</body>
</html>
auto_answer.html:
{% extends 'base.html' %}
{% block content %}
<p>Фамилия: {{data["surname"]}}</p>
<p>Имя: {{data["name"]}}</p>
<p>Образование: {{data["education"]}}</p>
<p>Профессия: {{data["profession"]}}</p>
<p>Пол: {{data["sex"]}}</p>
<p>Мотивация: {{data["motivation"]}}</p>
<p>Готовы остаться на Марсе? {{data["ready"]}}</p>
{% endblock %}
What's wrong?
Solution
Inside your render_template()
, you are passing two HTML pages.
For auto_answer.html
to load its base elements, it does so via {% extends 'base.html' %}
(which you already have), and nothing more. Meaning, you don't need to render both HTML pages.
return render_template("auto_answer.html", title=data["title"], data=data)
Answered By - Jake Jackson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.