I have attempted to make a Full-Feature Web App, and this includes using WTForms as the databasing resource, I am stumped at this error,
jinja2.exceptions.UndefinedError: 'wtforms.fields.core.StringField object' has no attribute 'username'
Traceback (most recent call last):
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/flask/app.py", line 2328, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/flask/app.py", line 2314, in wsgi_app
response = self.handle_exception(e)
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/flask/app.py", line 1760, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/flask/_compat.py", line 36, in reraise
raise value
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/flask/app.py", line 2311, in wsgi_app
response = self.full_dispatch_request()
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/flask/app.py", line 1834, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/flask/app.py", line 1737, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/flask/_compat.py", line 36, in reraise
raise value
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/flask/app.py", line 1832, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/flask/app.py", line 1818, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/lkendon/flaskblog/flaskblog.py", line 36, in register
return render_template('register.html', title='Register', form=form)
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/flask/templating.py", line 135, in render_template
context, ctx.app)
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/flask/templating.py", line 117, in _render
rv = template.render(context)
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/lkendon/flaskblog/venv/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/Users/lkendon/flaskblog/templates/register.html", line 1, in top-level template code
{% extends "layout.html" %}
File "/Users/lkendon/flaskblog/templates/layout.html", line 41, in top-level template code
{% block content %}{% endblock %}
File "/Users/lkendon/flaskblog/templates/register.html", line 10, in block "content"
{{form.username.username(class="form-control form-control-lg")}}
jinja2.exceptions.UndefinedError: 'wtforms.fields.core.StringField object' has no attribute 'username'
What on earth is the issue here, I was following a tutorial and i have the same details inputted through the provided snippets.
Looking at the traceback, you're calling {{form.username.username(... which should be {{form.username(...
Related
I'm getting the following traceback message with my error:
Traceback (most recent call last):
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/benjamattesjaroen/helloPython/app.py", line 470, in nutritionrda
return render_template('nutritionrda.html', nutritiontotals=nutritiontotals)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/templating.py", line 140, in render_template
ctx.app,
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/flask/templating.py", line 120, in _render
rv = template.render(context)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/Users/benjamattesjaroen/helloPython/templates/nutritionrda.html", line 1, in top-level template code
{% extends 'base.html' %}
File "/Users/benjamattesjaroen/helloPython/templates/base.html", line 63, in top-level template code
{% block body %}{% endblock %}
File "/Users/benjamattesjaroen/helloPython/templates/nutritionrda.html", line 33, in block "body"
<td class="col-xs-1">{{ ((nutritiontotal.calories)|round|int)}}</td><!-- Calories -->
File "/Users/benjamattesjaroen/helloPython/env/lib/python3.7/site-packages/jinja2/filters.py", line 797, in do_round
return round(value, precision)
TypeError: type Undefined doesn't define __round__ method
It seems the issue starts as soon as I try and use the round method, here
<td class="col-xs-1">{{ ((nutritiontotal.calories)|round|int)}}</td><!-- Calories -->
I've printed the value of nutritiontotal.calories using a separate script and I get
1718.82
So why is this a 'type undefined' error? Isn't 1718.82 clearly a numeric value? (This script used to work but I was playing around with it and it broke in some way)
Have you checked that you are actually passing/rendering the value of that variable correctly? What happens if you do something like the following?
{% if nutritional.calories is defined %}
{{ nutritional.calories|round|int }}
{% else %}
no value for nutritional.calories
{% endif %}
I've tried adding {{ test|round|int }} to the template and passing different values for test (int, string, float) to render_template and the way to reproduce the error is to not pass the value to render_template at all.
When I create database from pgadmin 4, it always show error popup: Error saving properties.
My computer info:
Linux Mint 18.3
pgAdmin 4 2.1 from https://wiki.postgresql.org/wiki/Apt
Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609]
My error in pgadmin4.log:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 180, in run_wsgi
execute(self.server.app)
File "/usr/lib/python3/dist-packages/werkzeug/serving.py", line 168, in execute
application_iter = app(environ, start_response)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/lib/python3/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/lib/python3/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/lib/python3/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/lib/python3/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3/dist-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/lib/python3/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/lib/python3/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/lib/python3/dist-packages/flask/views.py", line 84, in view
return self.dispatch_request(*args, **kwargs)
File "/usr/share/pgadmin4/web/pgadmin/browser/utils.py", line 242, in dispatch_request
return method(*args, **kwargs)
File "/usr/share/pgadmin4/web/pgadmin/browser/server_groups/servers/databases/__init__.py", line 157, in wrapped
return f(self, *args, **kwargs)
File "/usr/share/pgadmin4/web/pgadmin/browser/server_groups/servers/databases/__init__.py", line 489, in create
data=data, conn=self.conn
File "/usr/lib/python3/dist-packages/flask/templating.py", line 128, in render_template
context, ctx.app)
File "/usr/lib/python3/dist-packages/flask/templating.py", line 110, in _render
rv = template.render(context)
File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 989, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python3/dist-packages/jinja2/environment.py", line 754, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/lib/python3/dist-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "/usr/share/pgadmin4/web/pgadmin/browser/server_groups/servers/databases/templates/databases/sql/default/create.sql", line 10, in top-level template code
ENCODING = {{ data.encoding|qtLiteral }}{% endif %}{% if data.datcollate %}
File "/usr/share/pgadmin4/web/pgadmin/utils/driver/psycopg2/__init__.py", line 2079, in qtLiteral
adapted.encoding = 'utf8'
AttributeError: attribute 'encoding' of 'psycopg2.extensions.QuotedString' objects is not writable
Commenting out two lines in python code resolves the issue:
sudo sed -i '2078,2079 s/^[^#]/#/'
/usr/share/pgadmin4/web/pgadmin/utils/driver/psycopg2/__init__.py
The lines are:
if hasattr(adapted, 'encoding'):
adapted.encoding = 'utf8'
Probably you need psycopg > 2.6.2. See https://github.com/psycopg/psycopg2/issues/331
Install the python-psycopg2 complement, in ubuntu:
sudo apt-get install python-psycopg2
That Works for me !
Regards !
I am trying to render the index.html template in my Flask app's templates folder. However, I get a TemplateNotFound error. The template exists. How do I render it?
#app.route('/')
def index():
return render_template('../../templates/index.html')
Traceback (most recent call last):
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "G:\Projects\Intellij\Python\HelloPython\controller\web\WebHomeController.py", line 10, in webIndex
return render_template('../../templates/index.html', message=message)
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\flask\templating.py", line 133, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\jinja2\environment.py", line 869, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\jinja2\environment.py", line 830, in get_template
return self._load_template(name, self.make_globals(globals))
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\jinja2\environment.py", line 804, in _load_template
template = self.loader.load(self, name, globals)
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\jinja2\loaders.py", line 113, in load
source, filename, uptodate = self.get_source(environment, name)
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\flask\templating.py", line 57, in get_source
return self._get_source_fast(environment, template)
File "G:\Settings\Windows\ProgramFiles\Python\Python35-32\lib\site-packages\flask\templating.py", line 85, in _get_source_fast
raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: ../../templates/index.html
render_template takes the name to be looked up by the Jinja env, which has the templates folder it its lookup path. Only specify the path after that.
# index.html is in the templates folder
render_template('index.html')
# users/detail.html is in a sub-folder under templates
render_template('users/detail.html')
I am trying to render a simple Jinja template. No matter what the template is, or what variables are passed to it, I get an error when trying to render. Why am I getting this error and how do I fix it?
frontend = Blueprint('frontend', __name__, template_folder='templates')
#frontend.route('/')
def index():
stuff = dict()
stuff['page_title'] = 'Something'
stuff['center_piece'] = 'Random'
return render_template('base.html', stuff=stuff)
Basic template, without variables, still causes error.
<html>
<head>
<title>Something</title>
</head>
<body>Something else</body>
</html>
File "/env/lib/python3.4/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/env/lib/python3.4/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/env/lib/python3.4/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/env/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/env/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/env/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/env/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/env/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/env/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/env/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/app/frontend/controller.py", line 12, in index
return render_template('base.html', stuff=stuff)
File "/env/lib/python3.4/site-packages/flask/templating.py", line 127, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "/env/lib/python3.4/site-packages/jinja2/environment.py", line 851, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "/env/lib/python3.4/site-packages/jinja2/environment.py", line 812, in get_template
return self._load_template(name, self.make_globals(globals))
File "/env/lib/python3.4/site-packages/jinja2/environment.py", line 786, in _load_template
template = self.loader.load(self, name, globals)
File "/env/lib/python3.4/site-packages/jinja2/loaders.py", line 125, in load
code = environment.compile(source, name, filename)
File "/env/lib/python3.4/site-packages/jinja2/environment.py", line 555, in compile
defer_init=defer_init)
File "/env/lib/python3.4/site-packages/jinja2/environment.py", line 515, in _generate
return generate(source, self, name, filename, defer_init=defer_init)
File "/env/lib/python3.4/site-packages/jinja2/compiler.py", line 62, in generate
generator.visit(node)
File "/env/lib/python3.4/site-packages/jinja2/visitor.py", line 38, in visit
return f(node, *args, **kwargs)
File "/env/lib/python3.4/site-packages/jinja2/compiler.py", line 816, in visit_Template
self.blockvisit(node.body, frame)
File "/env/lib/python3.4/site-packages/jinja2/compiler.py", line 492, in blockvisit
self.visit(node, frame)
File "/env/lib/python3.4/site-packages/jinja2/visitor.py", line 38, in visit
return f(node, *args, **kwargs)
File "/env/lib/python3.4/site-packages/jinja2/compiler.py", line 1250, in visit_Output
const = child.as_const(frame.eval_ctx)
File "/env/lib/python3.4/site-packages/jinja2/nodes.py", line 467, in as_const
return Markup(self.data)
AttributeError: 'TemplateData' object has no attribute 'data'
I got this error when I refactored a variable called data to data_repo in PyCharm and somehow changed my Jinja2 installation's "nodes.py" file as well. This is the function that throws the error:
class TemplateData(Literal):
"""A constant template string."""
fields = ('data',)
def as_const(self, eval_ctx=None):
eval_ctx = get_eval_context(self, eval_ctx)
if eval_ctx.volatile:
raise Impossible()
if eval_ctx.autoescape:
return Markup(self.data)
return self.data
My refactor changed the string in the fields tuple but not the return value, leading to an AttributeError with any template.
I am working on this tutorial.flask tutorial
However, I got the above error when I work with web forms.
Here is the error message generated by Flask.
File "C:\Python27\lib\site-packages\flask-0.10-py2.7.egg\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask-0.10-py2.7.egg\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python27\lib\site-packages\flask-0.10-py2.7.egg\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask-0.10-py2.7.egg\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask-0.10-py2.7.egg\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask-0.10-py2.7.egg\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask-0.10-py2.7.egg\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask-0.10-py2.7.egg\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\workspace\Learn-flask\app\views.py", line 17, in login
return render_template('login.html', title='Sign In',form=form)
File "C:\Python27\lib\site-packages\flask-0.10-py2.7.egg\flask\templating.py", line 128, in render_template
context, ctx.app)
File "C:\Python27\lib\site-packages\flask-0.10-py2.7.egg\flask\templating.py", line 110, in _render
rv = template.render(context)
File "C:\Python27\lib\site-packages\jinja2-2.7.1-py2.7.egg\jinja2\environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "C:\Python27\lib\site-packages\jinja2-2.7.1-py2.7.egg\jinja2\environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\workspace\Learn-flask\app\templates\login.html", line 1, in top-level template code
{% extends "base.html"%}
File "C:\workspace\Learn-flask\app\templates\base.html", line 17, in top-level template code
{% block content %}{% endblock %}
File "C:\workspace\Learn-flask\app\templates\login.html", line 9, in block "content"
<p>Open id :{{form.openid(80)}}<br/>
Here is the form class code
from flask_wtf import Form
from wtforms import StringField, BooleanField
from wtforms.validators import DataRequired
class LoginForm(Form):
openid = StringField('openid', validators=[DataRequired()])
remember_me = BooleanField('remember_me', default=False)
Where I have done the mistake?
wtforms.fields.Field.__call__ only takes keyword arguments, as the linked documentation states. If you want to pass 80 as maxlen then use the keyword argument:
{{ form.openid(maxlength=80) }}