从用户界面删除csv文件中的列表项

时间:2018-11-07 15:44:27

标签: python csv flask

我有此代码,该代码使我可以将用户输入保存在csv文件中,然后从csv文件中将其显示为Web应用程序上的列表。我还希望允许用户删除他们选择的这些项目。反正有这样做吗? 附言我是python初学者

这是下面的代码

class ReusableForm(Form):
name = TextField('Name:', validators=[validators.required()])

@app.route("/addchannel", methods=['GET', 'POST'])
def form_data():

    form = ReusableForm(request.form)
    print(form.errors)

    if request.method == 'POST':
        name = request.form['name']
        fieldname = ['name']
        if form.validate():

            flash('Channel successfully added! ')
        else:
            flash('Error: All the form fields are required. ')

        with open('channelname.csv', 'a') as file:
            writer = csv.DictWriter(file, fieldnames= fieldname)
            writer.writerow({'name': name})

    reader = csv.reader(open('channelname.csv'))
    csv_data = [row for row in reader]

    return render_template('addchannel.html', form=form , data= csv_data)

0 个答案:

没有答案