I started using a macro for rendering fields, with errors, if I fail to validate the form, I get this error. If I use the correct credentials I get redirected to where I should and no errors are shown, if I fail to validate it tries to redirect to this same route but raises this error.
"C:\Users\ulman\PycharmProjects\database_website_optimised\source\database_website\applications\users\views.py", line 43, in post
return render_template(self.template_name, title=self.title, form=self.form_class)
File "C:\Users\ulman\PycharmProjects\database_website_optimised\venv\lib\site-packages\flask\templating.py", line 140, in render_template
ctx.app,
File "C:\Users\ulman\PycharmProjects\database_website_optimised\venv\lib\site-packages\flask\templating.py", line 120, in _render
rv = template.render(context)
File "C:\Users\ulman\PycharmProjects\database_website_optimised\venv\lib\site-packages\jinja2\environment.py", line 1090, in render
self.environment.handle_exception()
File "C:\Users\ulman\PycharmProjects\database_website_optimised\venv\lib\site-packages\jinja2\environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "C:\Users\ulman\PycharmProjects\database_website_optimised\venv\lib\site-packages\jinja2\_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "C:\Users\ulman\PycharmProjects\database_website_optimised\source\database_website\applications\users\templates\users\login.html", line 3, in top-level template code
{% from 'macros/_formhelpers.html' import render_field %}
File "C:\Users\ulman\PycharmProjects\database_website_optimised\source\database_website\templates\base.html", line 69, in top-level template code
{% block content %}{% endblock %}
File "C:\Users\ulman\PycharmProjects\database_website_optimised\source\database_website\applications\users\templates\users\login.html", line 9, in block "content"
{{ form.hidden_tag() }}
TypeError: hidden_tag() missing 1 required positional argument: 'self'
127.0.0.1 - - [31/May/2020 11:29:52] "POST /login/ HTTP/1.1" 500 -
Here is my login.html file:
{% extends "base.html" %}
{% from 'macros/_formhelpers.html' import render_field %}
{% block content %}
<div class="container">
<div class="content-section">
<form method="POST" action="">
{{ form.hidden_tag() }}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Log In to your account</legend>
<div>
{{ render_field(form.email) }}
</div>
<div>
{{ render_field(form.password) }}
</div>
<div>{{ form.remember }} {{ form.remember.label }}</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
</div>
</form>
</div>
<div class="border-top pt-3">
<small class="text-muted">
Have not registered yet? <a href="{{ url_for('users.register') }}">Sign Up</a>
</small>
</div>
</div>
{% endblock %}
And here is the macro _formhelpers.html file:
{% macro render_field(field) %}
{{ field.label(class="form-control-label") }}
{{ field(class="form-control form-control-lg") }}
{% if field.errors %}
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{% endmacro %}
Turns out it had nothing to do with my html file, instead when I was returning my login template again instead of
return render_template(self.template_name, title=self.title, form=self.form_class)
I needed to write this:
return render_template(self.template_name, title=self.title, form=self.form_class())
Got the solution idea from this post: Error: "TypeError: hidden_tag() missing 1 required positional argument: 'self' " in Flask, python