app.py 这是我的app.py
from flask import Flask, url_for, request, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/check", methods = ["POST"])
def check():
food = request.form.get("food")
return render_template("check.html",food = food)
layout.html 这是我的html的布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
index.html 索引是从layout.html扩展而来的,其格式为文本
{% extends 'layout.html' %}
{% block title %}Shazam for Food{% endblock %}
{% block body %}
<div class="form">
<form action="{{url_for('check')}}" method="POST">
<input type="text" name="food" placeholder="Enter a food name">
<button type="submit">submit</button>
</form>
</div>
{% endblock %}
check.html 检查文本是否为热狗,是否提供类似图片
{% extends 'layout.html' %}
{% block title %}Shazam for Food{% endblock %}
{% block body %}
<div class="result">
{% if food is "hotdog" %}
<figure>
<img src="{{url_for('static', filename='hotdog.jpg')}}" alt="hotdog">
<figcaption>Hot Dog</figcaption>
</figure>
{% else %}
<figure>
<img src="{{url_for('static', filename='nothotdog.jpg')}}" alt="nothotdog">
<figcaption>Not Hot Dog</figcaption>
</figure>
{% endif %}
</div>
{% endblock %}
代码显示 jinja2.exceptions.TemplateSyntaxError:预期令牌'name',得到了'string'