我在烧瓶视图功能中有以下内容(_I' m使用python 3.6):
@login_required
@main.route('/dashboard', methods=['GET', 'POST'])
def dashboard():
if request.method =='POST':
dash_data = namedtuple('dash_data',[('posting', str), ('folder', str)])
print(request.form.get('posting'))
posted_data= dash_data(posting=request.form.get('posting'),folder=request.form.get('folder'))
我得到了:
Traceback (most recent call last):
File "...\lib\site-packages\flask\app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "...\lib\site-packages\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "...\lib\site-packages\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "...\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "...\lib\site-packages\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "...\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "...\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "...\lib\site-packages\flask\_compat.py", line 33, in reraise
raise value
File "...\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "...\lib\site-packages\flask\app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "E:\ENVS\r3\posterizer2\app\main\views.py", line 387, in cl_dash
Cl_dash_data = namedtuple('cl_dash_data',[('posting', str), ('folder', str)])
File "...\lib\collections\__init__.py", line 403, in namedtuple
'identifiers: %r' % name)
ValueError: Type names and field names must be valid identifiers: "('posting', <class 'str'>)"
我做错了什么?
答案 0 :(得分:3)
namedtuple
个对象不接受(attribute, type)
元组的列表,它们只是期望可迭代的属性(或单个字符串):
namedtuple('dash_data', ('posting', 'folder'))
或者:
namedtuple('dash_data', 'posting folder')
有关如何使用namedtuple
的信息,请参阅the documentation。
答案 1 :(得分:1)
也许您可以在输入模块中使用NamedTuple 使用类似跟随:
from typing import NamedTuple
dash_data = NamedTuple('dash_data',[('posting', str), ('folder', str)])