使用 Bottle 将 <textarea> 的内容保存到文件中
Saving the content of a <textarea> to file with Bottle
我正在编写一个小型门户网站,使用户能够为计算语言学项目注释一些文本,并将注释保存到文件中。
我在保存修改后的文本时遇到问题。
我的页面是:
from bottle import template
from project import app
from bottle import request
from bottle import redirect
import random
@app.route('/')
def index():
notices = 'This is a placeholder text for Whosebug'
return template('annotator/index', package=notices)
@app.route('/annotator/submit', method=['GET'])
def submit():
with open('output.txt', 'w') as outfile:
package = str(request.GET.get('package'))
outfile.write(str(package))
redirect('/')
我的页面布局是:
<!doctype html>
<head>
<link rel="stylesheet" type="text/css" href="/css/style.css">
<title>My App</title>
</head>
<body>
<div class="page">
<h1>NM Annotator Demo V 0.1</h1>
% if package is not '':
<form action='annotator/submit', method="GET">
<textarea name="package" ROWS=20 COLS=70>{{package}}</textarea>
<td><INPUT TYPE=SUBMIT name="package" VALUE="Submit"></td>
</form>
%end
%include
</div>
</body>
提交页面为:
<form action="annotator/submit" method="post">
<dl>
Thank you
</dl>
</form>
%rebase layout/layout
但是,只有单词 'submit' 被保存在文件中 - 而不是我想要保存的内容,因为它将是注释的结果。
这是我的第一个网络应用程序,我有点困惑。
您的文本区域和提交表单项都命名为 "package"。
将您的按钮更改为此,看看是否有帮助:
<INPUT TYPE=SUBMIT name="submit" VALUE="Submit">
编辑:解释
具有两个同名表单项的问题在于您的应用程序会在查询字符串中同时接收到它们。例如,
http://yourserver.com/annotator/submit?package=sometext&package=Submit
在您的应用中,您将有效地获得查询参数的字典,它看起来像 {'package': 'sometext'}
或 {'package': 'Submit'}
。您得到哪个完全取决于应用程序 (Bottle),但最可能的实现——按顺序处理查询参数——会导致第二个值优先,因为它会覆盖第一个。
大多数网络框架都公开了一种获取给定名称的所有查询参数的方法;在 Bottle 中,它是 request.query.getall。所以 request.query.getall('package')
会 return ['sometext', 'Submit']
。但在你的情况下,首先避免名称冲突更有意义,而不是保留它然后检索多个值。
希望对您有所帮助!
我正在编写一个小型门户网站,使用户能够为计算语言学项目注释一些文本,并将注释保存到文件中。
我在保存修改后的文本时遇到问题。
我的页面是:
from bottle import template
from project import app
from bottle import request
from bottle import redirect
import random
@app.route('/')
def index():
notices = 'This is a placeholder text for Whosebug'
return template('annotator/index', package=notices)
@app.route('/annotator/submit', method=['GET'])
def submit():
with open('output.txt', 'w') as outfile:
package = str(request.GET.get('package'))
outfile.write(str(package))
redirect('/')
我的页面布局是:
<!doctype html>
<head>
<link rel="stylesheet" type="text/css" href="/css/style.css">
<title>My App</title>
</head>
<body>
<div class="page">
<h1>NM Annotator Demo V 0.1</h1>
% if package is not '':
<form action='annotator/submit', method="GET">
<textarea name="package" ROWS=20 COLS=70>{{package}}</textarea>
<td><INPUT TYPE=SUBMIT name="package" VALUE="Submit"></td>
</form>
%end
%include
</div>
</body>
提交页面为:
<form action="annotator/submit" method="post">
<dl>
Thank you
</dl>
</form>
%rebase layout/layout
但是,只有单词 'submit' 被保存在文件中 - 而不是我想要保存的内容,因为它将是注释的结果。
这是我的第一个网络应用程序,我有点困惑。
您的文本区域和提交表单项都命名为 "package"。
将您的按钮更改为此,看看是否有帮助:
<INPUT TYPE=SUBMIT name="submit" VALUE="Submit">
编辑:解释
具有两个同名表单项的问题在于您的应用程序会在查询字符串中同时接收到它们。例如,
http://yourserver.com/annotator/submit?package=sometext&package=Submit
在您的应用中,您将有效地获得查询参数的字典,它看起来像 {'package': 'sometext'}
或 {'package': 'Submit'}
。您得到哪个完全取决于应用程序 (Bottle),但最可能的实现——按顺序处理查询参数——会导致第二个值优先,因为它会覆盖第一个。
大多数网络框架都公开了一种获取给定名称的所有查询参数的方法;在 Bottle 中,它是 request.query.getall。所以 request.query.getall('package')
会 return ['sometext', 'Submit']
。但在你的情况下,首先避免名称冲突更有意义,而不是保留它然后检索多个值。
希望对您有所帮助!