如何使用jinja \ wtform取回对选择字段所做的更改

时间:2015-02-25 19:35:42

标签: python html flask jinja2 wtforms

我3天前发现了python,jinja / wtform昨天。很容易理解,但我有一些问题。

我正在努力做一个简单的Web应用程序,允许用户更改文件中的某些配置。此文件用于其他应用程序。

我需要以DropDownList的形式显示选项,为此,我首先阅读包含所有选择(名为'config.ini')的文件并显示包含所有选项的页面,并按钮提交更改并将其保存在相同的'config.ini'文件中,格式相同。

我想用纯文本文件,而不是炼金术或任何数据库。只是文字。我不需要会话,登录表单,没有。是运行非常慢的资源计算机的单个用户系统。 (覆盆子)

我的“config.ini”文件如下所示:

choice_1 alpha alpha betha gamma
choice_2 3 1 2 3
choice_3 red blue yellow red white black

第一个单词是选择名称,第二个单词是选择/默认选项,其他所有选项都是可供选择的列表。

这是我的view.py:

from flask import render_template, flash, redirect, url_for
from app import app
from wtforms import SelectField, SubmitField
from flask_wtf import Form


class ConfigForm(Form):
    choice_1= SelectField('choice_1')
    choice_2= SelectField('choice_2')
    choice_3= SelectField('choice_3')
    submit = SubmitField('SaveChanges')

variables = []

@app.route('/')


@app.route('/index', methods=['GET','POST'])
def index():
    form = ConfigForm()
    myconfigfile = {'name':'config.ini'}      

    if form.is_submitted():
            savefileid = open('config.ini','w')

            for variable in variables:
                    savefileid.write(variable['_name']+ ' ')
                    savefileid.write( variable['_value']+ ' ')

                    for option in variable['_options']:
                            savefileid.write(option + ' ')

                    savefileid.write("\n")

            savefileid.close()
    else:
            readfileid = open('config.ini')                                                         

            lineas = readfileid.readlines()                                                         
            for linea in lineas:                                                                    
                    mystrings = linea.split()                                                       
                    myset = set( mystrings [2:] )                                                   
                    mydict = dict({'_name':mystrings[0], '_value':mystrings [1], '_options':myset})  
                    variables.append(mydict)                                                        


            readfileid.close()                                                                      


    return render_template('index.html',                                                            
                    title='Home',
                    myconfigfile=myconfigfile,
                    variables=variables,
                    form=form
                    )

这个是我的“index.hmtl”文件。

<!-- extend base layout -->
{% extends "base.html" %}

<!-- {% from "_formhelper.html" import render_field %} -->

{% block content %}

<h1>Archivo: {{ myconfigfile.name }}</h1>

<form action="{{ url_for('index') }}" method="post">
<div style="width:300; height:300">

<table style="width:300px; height:300">
<tr>
    <td align="center"><strong>Choice</strong></th>
    <td align="center"><strong>value</strong></th>
</tr>

{% for avar in variables %}
<tr>
    <td width=120 align="left"><b>{{ avar._name }}</b></td>
    <td width=100 ><select name="{{ avar._name }}">

    {% for opcion in avar.opciones %}
            {% if opcion == avar._value %}
                    <option value="{{ avar._name }}" selected> {{ opcion }}</option>
            {% else %}
                    <option value="{{ avar._name }}"> {{ opcion }}</option>
            {% endif %}
    {% endfor %}


    </select></td>
</tr>

{% endfor %}

</table>
</div>

    {{ form.submit }}

</form>
{% endblock %}

另一个是我的布局“base.html”

<html>
  <head>
    {% if title %}
    <title>{{ title }} - Configuration</title>
    {% else %}
    <title>Configuration</title>
    {% endif %}

</head>
  <body>
    <div>Configuração : <a href="/index">configuration parameters</a></div>
    <hr>
    {% with messages = get_flashed_messages() %}
    {% if messages %}
    <ul>
    {% for message in messages %}
        <li>{{ message }} </li>
    {% endfor %}
    </ul>
    {% endif %}
    {% endwith %}

    {% block content %}{% endblock %}

        <hr>
        <p></p>

  </body>
</html>

我的问题是,我该怎么做?如何设置允许更改它的表单,单击提交按钮后,将更改保存回配置文件中。

请原谅我的英文,并提前致谢。

0 个答案:

没有答案