问题
Background
I would like to create a login form with jinja, Python, and HTML.
The official document introduced an example of text forms using jinja.
Problem
However, I have two problems to create text forms with jinja. I cannot find "forms.html" document mentioned on the web page. There is no Python example to execute the code written in HTML and jinja.
How can I fix my current code to execute the sample program of text form in jinja?
Program
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login Page</title>
</head>
<body>
{% from 'forms.html' import input as input_field, textarea %}
<dl>
<dt>Username</dt>
<dd>{{ input_field('username') }}</dd>
<dt>Password</dt>
<dd>{{ input_field('password', type='password') }}</dd>
</dl>
<p>{{ textarea('comment') }}</p>
{% endcall %}
</body>
</html>
回答1:
forms.html is a template file which will give the properties of the textbox and textarea.
You have to include the template file in the project directory and load them in the python file.
As per your referring forms.html file should contain the below code.
{% macro input(name, value='', type='text') -%}
<input type="{{ type }}" value="{{ value|e }}" name="{{ name }}">
{%- endmacro %}
{%- macro textarea(name, value='', rows=10, cols=40) -%}
<textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols
}}">{{ value|e }}</textarea>
{%- endmacro %}
来源:https://stackoverflow.com/questions/58553044/how-can-i-create-text-forms-with-jinja