Question seen on Zhihu: Can python flask’s wtforms handle variable-length forms?
Problem description
form中的元素会变多。 比如有一个表格: 我喜欢的东西: 可以增加任意个物品(这几个物品填在不同的框),然后提交。 实现这个需求,需要用到FieldList
A simple example:
from wtforms import Form from wtforms.fields import FieldList, StringField class MyForm(Form): names = FieldList(StringField('名称'), label='物品列表', min_entries=1)
Submit form data:
names-0=苹果 names-1=梨 names-2=香蕉
Submit json data:
{"names": ["苹果", "梨", "香蕉"]}
Output result display:
print(form.names.data) # ['苹果', '梨', '香蕉']
The following is a more complicated example:
from wtforms import Form from wtforms.fields import FieldList, FormField, StringField, IntegerField class ProductForm(Form): name = StringField('名称') count = IntegerField('数量') class MyForm(Form): products = FieldList(FormField(ProductForm), label='产品列表', min_entries=1)
Submit form data :
products-0-name=Iphone6 products-0-count=1 products-1-name=小米手机 products-1-count=2
Submit json data:
{"products": [{"name": "Iphone6", "count": 1}, {"name": "小米手机", "count": 2}]}
The output result shows:
print(form.products.data) # [{'name': 'Iphone6', 'count': 1}, {'name': '小米手机', 'count': 2}]
Then the question is, what is the key to dynamics?
That’s right, it’s the number starting with 0 in the field name you see
What should I do if I want to add an item?
The largest number plus 1, that’s it!
The js code in HTML is the key to achieving dynamics. The relevant code will not be shown. Here we only focus on the python part.

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

Useanarray.arrayoveralistinPythonwhendealingwithhomogeneousdata,performance-criticalcode,orinterfacingwithCcode.1)HomogeneousData:Arrayssavememorywithtypedelements.2)Performance-CriticalCode:Arraysofferbetterperformancefornumericaloperations.3)Interf

No,notalllistoperationsaresupportedbyarrays,andviceversa.1)Arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,whichimpactsperformance.2)Listsdonotguaranteeconstanttimecomplexityfordirectaccesslikearraysdo.

ToaccesselementsinaPythonlist,useindexing,negativeindexing,slicing,oriteration.1)Indexingstartsat0.2)Negativeindexingaccessesfromtheend.3)Slicingextractsportions.4)Iterationusesforloopsorenumerate.AlwayschecklistlengthtoavoidIndexError.

ArraysinPython,especiallyviaNumPy,arecrucialinscientificcomputingfortheirefficiencyandversatility.1)Theyareusedfornumericaloperations,dataanalysis,andmachinelearning.2)NumPy'simplementationinCensuresfasteroperationsthanPythonlists.3)Arraysenablequick

You can manage different Python versions by using pyenv, venv and Anaconda. 1) Use pyenv to manage multiple Python versions: install pyenv, set global and local versions. 2) Use venv to create a virtual environment to isolate project dependencies. 3) Use Anaconda to manage Python versions in your data science project. 4) Keep the system Python for system-level tasks. Through these tools and strategies, you can effectively manage different versions of Python to ensure the smooth running of the project.

NumPyarrayshaveseveraladvantagesoverstandardPythonarrays:1)TheyaremuchfasterduetoC-basedimplementation,2)Theyaremorememory-efficient,especiallywithlargedatasets,and3)Theyofferoptimized,vectorizedfunctionsformathematicalandstatisticaloperations,making


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools
