How can I create text forms with jinja?

半世苍凉 提交于 2020-01-25 11:58:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!