Bottle框架中Select控件值返回None的问题解决
问题:Bottle框架中Select控件提交值始终为None
我基于Bottle框架开发网站,通过SQLAlchemy将网页表单数据存入数据库。由于“Status”字段仅接受特定字符串(Onloan、Available,区分大小写),为避免输入错误使用了Select控件,但调试时该控件提交的值始终为None,其他表单输入(如Value字段)可正常获取数据。
相关Python代码
@route('/savenew', method='post') def savenew(): pass # Gathers all of the information from the html inputs add_Assetid = request.forms.get('Assetid') add_AssetName = request.forms.get('AssetName') add_category = request.forms.get('Category') add_borrower = request.forms.get('Borrower') add_status = request.forms.get('Status') add_value = request.forms.get('Value') # open a session db_engine = create_engine('sqlite:///db/MusicAssets.db') # creates a sessionmaker class for dynamic class creation Session = sessionmaker(bind=db_engine) # object creation session = Session() try: # this adds all the new varibles inputted to the database new_music = Music( Assetid=add_Assetid, AssetName=add_AssetName, Category=add_category, Borrower=add_borrower, Status=add_status, Value=add_value ) session.add(new_music) # commits to the changes being made. session.commit() # lets user use the changes were made and successful message = "Successfully added entry" # Error handling code any error stops ALL changes except Exception as e: # error message trying to give guidence to what could be the problem error = ("Please make sure you have used correct values e.g only " "using numbers for Value or inputting status correctly, " "Onloan and Avaliable are the only accepted inputs " "and are cap sensitive") message = f"Error adding entry: " + error finally: # Goes to messgae template to display if the changes were sucessful return template('changemsg.tpl', message=message)
原HTML中Select控件代码
<div class="form-group"> <label for="exampleInputPassword1">Status</label> <select class="form-select form-select-sm" aria-label=".form-select-sm example"> <option selected>Open this select menu</option> <option name="Status">Onloan</option> <option name="Status">Available</option> </select> </div>
正常工作的Value字段代码
<div class="form-group"> <label for="exampleInputPassword1">Value</label> <input type="number" name="Value" class="form-control" /> </div>
解决方案
错误根源
表单提交时,是通过<select>标签的name属性来识别字段的,而你错误地将name="Status"添加到了<option>标签上,导致后端无法通过request.forms.get('Status')获取到值,返回None。
修正后的Select控件代码
将name="Status"移到<select>标签上,同时优化默认选项(避免提交无意义的文本):
<div class="form-group"> <label for="exampleInputPassword1">Status</label> <select name="Status" class="form-select form-select-sm" aria-label=".form-select-sm example"> <option selected value="">请选择状态</option> <option>Onloan</option> <option>Available</option> </select> </div>
额外优化建议
- 强制必填校验:如果Status是必填字段,给
<select>添加required属性,强制用户选择合法值:<select name="Status" required class="form-select form-select-sm" aria-label=".form-select-sm example"> - 显式设置value属性:虽然选项文本和提交值一致时可省略,但显式设置
value更清晰,避免后续修改文本时影响提交值:<option value="Onloan">Onloan</option> <option value="Available">Available</option> - 数据库层二次校验:在SQLAlchemy模型中给Status字段添加约束,确保即使前端校验失效,数据库也能拦截非法值:
from sqlalchemy import CheckConstraint class Music(Base): __tablename__ = 'music' # 其他字段定义... Status = Column(String, CheckConstraint("Status IN ('Onloan', 'Available')"), nullable=False)
内容的提问来源于stack exchange,提问作者Xtra_Cold
相关产品推荐
相关产品推荐

