jinja2.exceptions.TemplateNotFound: /register
Posted
技术标签:
【中文标题】jinja2.exceptions.TemplateNotFound: /register【英文标题】: 【发布时间】:2020-03-20 20:17:57 【问题描述】:即使我要渲染模板,我也会收到此错误。我也尝试更改函数名称。
我认为这是由于烧瓶(错误)而发生的
Traceback(最近一次调用最后一次): 调用中的文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/app.py”,第 2463 行 return self.wsgi_app(environ, start_response) wsgi_app 中的文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/app.py”,第 2449 行 响应 = self.handle_exception(e) 文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/app.py”,第 1866 行,在 handle_exception reraise(exc_type, exc_value, tb) 文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/_compat.py”,第 39 行,在 reraise 升值 wsgi_app 中的文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/app.py”,第 2446 行 响应 = self.full_dispatch_request() 文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/app.py”,第 1951 行,位于 full_dispatch_request rv = self.handle_user_exception(e) 文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/app.py”,第 1820 行,在 handle_user_exception reraise(exc_type, exc_value, tb) 文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/_compat.py”,第 39 行,在 reraise 升值 文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/app.py”,第 1949 行,位于 full_dispatch_request rv = self.dispatch_request() 文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/app.py”,第 1935 行,在 dispatch_request 返回 self.view_functionsrule.endpoint 文件“/Users/ayyagari/Documents/DeepNews/web_2/website/main.py”,第 57 行,在 register_user 返回渲染模板(url_for('main.register_form')) 文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/templating.py”,第 138 行,在 render_template ctx.app.jinja_env.get_or_select_template(template_name_or_list), 文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/jinja2/environment.py”,第 869 行,在 get_or_select_template return self.get_template(template_name_or_list, parent, globals) 文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/jinja2/environment.py”,第 830 行,在 get_template return self._load_template(name, self.make_globals(globals)) _load_template 中的文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/jinja2/environment.py”,第 804 行 模板 = self.loader.load(self, name, globals) 加载中的文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/jinja2/loaders.py”,第 113 行 源,文件名,更新 = self.get_source(环境,名称) 文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/templating.py”,第 60 行,在 get_source return self._get_source_fast(环境,模板) 文件“/Users/ayyagari/Documents/DeepNews/web_2/venv/lib/python3.7/site-packages/flask/templating.py”,第 89 行,在 _get_source_fast 提高 TemplateNotFound(模板)
Here is my code
from flask import (
Blueprint, redirect, render_template, request, url_for,
flash)
from website.db import get_db
bp = Blueprint('main', __name__)
@bp.route('/home')
def index():
return render_template("index.html")
@bp.route('/register')
def register_form():
return render_template("form.html")
@bp.route('/success')
def success():
return render_template("response.html")
@bp.route('/register_user', methods=["POST"])
def register_user():
if request.method == 'POST':
email_id = request.form['reg-email']
company = request.form['reg-company']
type_of_business = request.form['industry_type']
checkbox = request.form['reg-check']
db = get_db()
error = None
if not email_id:
error = 'Email ID is required.'
elif not company:
error = 'Company is required.'
elif not type_of_business:
error = 'Type of Business is required.'
elif not checkbox:
error = 'Accept the Terms and Conditions'
elif db.execute(
'SELECT id FROM beta_users WHERE email_id = ?', (email_id,)
).fetchone() is not None:
error = 'User is already registered.'.format(email_id)
if error is None:
db.execute(
'INSERT INTO beta_users (email_id, company, type_of_org, checkbox) VALUES (?, ?, ?, ?)',
(email_id, company, type_of_business, checkbox)
)
db.commit()
return redirect(url_for('main.success'))
flash(error)
return render_template(url_for('main.register_form'))
@bp.route('/error')
def errors():
return render_template("404.html")
API 还显示 500 内部服务器错误
【问题讨论】:
请格式化您的代码,它不可读。如果您对如何格式化有疑问,请阅读此内容:meta.stackexchange.com/a/22189 【参考方案1】:问题出在这里:
return render_template(url_for('main.register_form'))
url_for
返回一个通过 Internet 访问路由的 url,这不是 render_template
使用的。 render_template
需要一个 html/文本文件。
所以,要么将其替换为:
return render_template('register.html')
或将用户重定向到另一条路线,让它发挥作用:
from flask import redirect
...
return redirect(url_for('main.register'))
【讨论】:
如果我想闪现我的错误怎么办?因为如果我想抛出任何提到的错误消息,它就不起作用! 闪现的消息将堆积起来,直到模板处理堆栈——所以只要你在重定向之前闪现错误——你应该可以在 /register 路由模板中访问它们。跨度>以上是关于jinja2.exceptions.TemplateNotFound: /register的主要内容,如果未能解决你的问题,请参考以下文章