Django表单提交后,如何获取相同name的不同value值

现代前端表单提交:Fetch API与FormData的黄金组合实践 在Web开发中,表单提交是基础且高频的操作,其核心在于客户端与服务器之间的数据交互。传统方式如原生表单提交或jQuery.ajax,常伴随页面刷新与回调地狱问题,影响用户体验与代码维护。现代前端技术通过Fetch API提供了基于Promise的异步请求方案,它支持链式调用与async/await,能更精细地控制请求与响应流程。结合FormData对象,开发者可以轻松构建符合multipart/form-data格式的请求体,尤其简化了文件上传等复杂场景的数据处理。这种组合不仅提升了前端对数据验证、乐观更新 阅读详情


i前端:nput_test.html

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Search</title>
    <script>

    </script>
</head>
<body>
    <div><p>{{ result }}</p></div>
    <form action="" method="post">{% csrf_token %}
        <input type="text" name="key">
        <input type="text" name="key">
        <input type="submit" value="Search" >
    </form>
</body>
</html>

两个<input >标签name都是'key'


后台:Django views.py

def setting(request):
    if request.method == 'POST':
        result_list = request.POST.getlist('key', '')
        result = str(result_list)
        return render_to_response('input_test.html', locals(), context_instance=RequestContext(request))
    else:
        return render_to_response('input_test.html', locals(), context_instance=RequestContext(request))


运行后,在浏览器表单中输入:

打开chrome ,提交表单,看FormData,其实浏览器已经向后台提交了两个name 为'key '的值。


后台也成功接收到一个列表 ['1', '2']。

主要是这个API request.POST.getlist(),可以接收到所有共享同一个name的value,
QueryDict.getlist(key, default)¶

    Returns the data with the requested key, as a Python list. Returns an empty list if the key doesn’t exist and no default value was provided.
 It’s guaranteed to return a list of some sort unless the default value was no list.

参考:http://stackoverflow.com/questions/14348321/getting-distinct-values-from-inputs-that-share-the-same-name

django之从html页面表单获取输入的数据实例 主要介绍了django之从html页面表单获取输入的数据实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 立即下载

相关推荐

svn 分支(branch)和标签(tag)管理

svn的分支管理

caohongxing的博客 3万+

Django——表单/admin站点详解

{{fom.字段名.field}};例如,{{form.name.field.max_length}}。{{fom.字段名.lable_tag}} :封装在HTML-label元素中的label文本,包含表单的label_suffix。{{fom.字段名.lable}}:字段的label文本,例如,“姓名”。{{form.字段名.errors}} :字段未通过验证时的错误信息。{{form.字段名.help_text}} :字段的帮助文本。{{form.字段名.value}}:字段值。

[ 「今でもあなたは私の光。」❤️如今你依旧是我的光。 1490

VM与欧姆龙PLC通讯设置实战:Fins TCP/UDP协议详解

本文详细解析了VM(VisionMaster)与欧姆龙PLC通过Fins TCP/UDP协议进行通讯的实战设置。内容涵盖网络配置、协议选择(TCP可靠性与UDP实时性)、VM端地址映射与数据点添加、以及数据接收解析流程,并提供了常见错误排查与高级调试技巧,帮助视觉与自动化工程师快速实现稳定可靠的工业通讯集成。

weixin_29172963的博客 329

Django xadmin 后台自定义action 动作

adminx.py from xadmin import views from .models import * # 自定义动作所需 from django import forms, VERSION as django_version from django.core.exceptions import PermissionDenied from django.db import route...

长门有希的博客 5291

主流双目相机技术参数横向评测:从ZED到INDEMIND的深度解析

本文对主流双目相机进行了深度横向评测,涵盖ZED、Bumblebee、Leap Motion、DUO 3D及INDEMIND等国内外产品。文章详细解析了分辨率、帧率、视场角、基线等核心参数,并结合科研、机器人导航、无人机、工业检测等典型应用场景,提供了实战选型指南,帮助读者根据具体需求挑选最合适的双目视觉解决方案。

weixin_29212867的博客 640

告别色差!用Python+TCS34725颜色传感器实现精准色彩还原(附完整校准代码)

本文详细介绍了如何利用Python和TCS34725颜色传感器实现专业级色彩还原方案。通过双阶校准法(白光校准和黑电平校准)和进阶的光照自适应技术,解决传感器原始数据不准的问题,并提供完整的校准代码和实战案例,帮助开发者实现ΔE<3的高精度色彩识别。

weixin_30836759的博客 209

Django获取checkbox勾选框的值(转载)

<formaction=""method="POST"> <inputtype="checkbox"value="1"name="check_box_list"/>1 <inputtype="checkbox"value="2"name="check_box_list"/>2 <in...

weixin_30781107的博客 917

汇丰外包Java岗面试复盘:Zoom面1.5小时,从英语、粤语到现场敲代码的极限挑战

本文详细记录了汇丰银行外包Java岗位的面试全过程,包括多语言切换(英语、粤语)的技术沟通挑战和现场编码测试的实战经验。从高并发编程到JVM调优,面试不仅考察技术深度,更注重实战问题解决能力。文章分享了应对跨国银行技术面试的实用策略,帮助Java开发者在跨文化环境中提升竞争力。

weixin_42524824的博客 363

Django学习手册 - 前端input数据获取

view获取数据方法归纳: #请求meta数据 request.mata(...) request.method(POST,GET,PUT)  #从meta里面获取数据 request.path_info request.COOKIES #请求body数据 request.POST(从body里面获取数据) request....

dixian4894的博客 2596

数据库崩溃恢复机制:REDO日志与UNDO日志详解

在数据库管理系统中,事务的持久性和一致性是至关重要的。为了保证在系统崩溃或发生故障时数据不会丢失,数据库引入了恢复机制。其中,REDO日志和UNDO日志是两种关键的恢复技术。本文将详细描述这两种日志的作用、工作原理以及它们在数据库崩溃恢复中的应用。

My_wife_QBL的博客 1411

基于STM32L4XX、HAL库的BQ25713RSNR充电芯片驱动程序设计

define BQ25713_I2C_ADDR (0x6B << 1) // 7位地址为 0x6B,HAL库需要左移一位。// 示例:配置 CHARGE_OPTION_0 寄存器:使能充电,使能 ICO(输入电流优化)reg_data |= (EN_CHG_MASK | EN_ICO_MASK);

zhusl6688的博客 406

django request 获取多个相同name的value值

原文:http://blog.youkuaiyun.com/yes1cpp/article/details/42405251

小青蛙 2616

20251121更新纷玩岛

【代码】20251121更新纷玩岛。

猫敷雪 1019

django全选checkbox、post获取相同name的value

就不详细介绍了,简单的代码 全选checkbox(JS函数实现) &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Download&lt;/title&gt; &lt;script type="text/javascript"&gt; //页面加载的时候,所有的复选框都是未选中的状态 function checkO

abcd147147的博客 1509

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

django获取多个checkbox复选框post的数组数据

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34255793的博客 672

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Django 3.0 文档笔记——表单处理和代码优化 tutorial 04

文档: https://docs.djangoproject.com/zh-hans/3.0/intro/tutorial04/ 编写你的第一个 Django 应用,第 4 部分 写一个表单 表单创建 在上一个教程中编写的投票详细页面的模板 ("polls/detail.html") ,添加表单(HTML<form>) <h1>{{ question.ques...

zhr1030635594的博客 465

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

HOWTO get multiple value from the same name checkbox elements or radiobution elements

HOWTO get multiple value from the same name checkbox elements or radiobution elements usually,we userequest.POST.get("aaa","") to fet...

79

Django框架整理

Django是一个功能强大的web框架。 前言:框架模式简介 1、MVC和MTV框架模式:分层级进行管理 说到框架模式我们有必要简单的说下设计模式,了解下设计模式这个概念,因为有人对设计模式和框架模式的概念经常混淆 设计模式: 是一套被反复使用,多数人知道并经过分类的代码设计经验总结,是为了解决一些通用性问题的 目的:重用代码并保证代码的可靠性 设计模式分类:单例,抽象工厂 等等 23种模式 一句话总结:解

有用的话关个注,点个赞,收个藏🫶。 1713

Django Forms深度解析:从表单校验到生产级钩子函数

Django Forms并非简单的HTML表单Python化,而是一套贯穿请求生命周期的数据守门人系统,涵盖前端渲染、数据解析与业务校验三层能力。其核心原理在于字段定义、clean()表单级校验、clean_<field>字段级钩子的严格执行时序,以及BoundField对象对数据流的精准控制。技术价值体现在防御性编程(如邮箱唯一性查库校验)、错误分级提示(字段级/非字段级)、副作用隔离(校验与保存分离)等工程实践上。典型应用场景包括用户注册、后台管理、多模型关联(FormSet)等高可靠性表单需求。本文聚

weixin_34414196的博客 322

django中HttpRequest请求

视图的第一个参数必须是HttpRequest对象 在视图函数中,接收的request有如下属性: path:一个字符串,表示请求的页面的完整路径,不包含域名。 method:一个字符串,表示请求使用的HTTP方法,常用值包括:'GET'、'POST'。 在浏览器中给出地址发出请求采用get方式,如超链接。 在浏览器中点击表单的提交按钮发起请求,如果表单的method设置为post...

weixin_30336061的博客 82

Django请求处理:GET与POST的核心原理与安全实践

HTTP请求是Web开发的基础,其中GET和POST是最常用的两种方法。GET请求通过URL传递参数,具有可见性和可缓存性,适合数据查询;POST请求通过请求体传输数据,适合提交敏感信息或大量数据。在Django框架中,HttpRequest对象自动处理这两种请求方式,开发者可以通过request.GET和request.POST轻松访问参数。从安全角度看,GET请求需防范XSS攻击,POST请求则需关注CSRF防护。Django提供了完善的中间件和模板系统来应对这些安全问题,如自动转义用户输入和CSRF令

diaohuyi6830的博客 401

Django CSRF验证失败:Referer头缺失的排查与解决方案

跨站请求伪造(CSRF)是Web应用常见的安全威胁,其防护机制通常依赖于服务器验证请求的合法来源。Django框架内置了CSRF防护,通过结合会话Cookie中的令牌与请求体中的Token进行双重验证。然而,这套机制的有效性部分依赖于浏览器自动发送的HTTP Referer或Origin头,用于执行同源检查,确保请求源自同一站点。当Referer头因浏览器隐私设置、HTTPS到HTTP的协议降级、广告拦截插件或前端代码策略而缺失时,Django的CSRF中间件会拒绝合法请求,导致“403 Forbidden

weixin_34367257的博客 522

Django CSRF防护机制详解与实践指南

CSRF(跨站请求伪造)是Web开发中常见的安全威胁,攻击者利用用户已登录状态执行非预期操作。Django框架通过令牌验证机制有效防御此类攻击,其核心原理是生成随机密钥并验证请求来源。在Web安全体系中,CSRF防护常与HTTPS、Cookie安全属性设置等技术配合使用,构建多层次防御。本文以Django实现为例,详解CSRF令牌的生成验证流程,包括中间件工作逻辑、模板集成方式以及AJAX请求处理技巧。针对高并发场景,还提供了性能优化建议和安全增强措施,帮助开发者在保证安全性的同时提升系统性能。

diaoju3333的博客 437

Django Rest Framework的使用整理

Django Rest Framework 一、Rest Framework的基本介绍 程序的客户端有很多:硬件设备,游戏,APP,软件,其他的外部服务端。 1. Web应用模式 在开发Web应用中,有两种应用模式: 前后端不分离[客户端看到的内容和所有界面效果都是由服务端提供出来的。] 这种情况下,前端页面中会出现很多涉及到服务端的模板语法。 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-neat7OGD-1610381584965)(E:/Download/8-3

有用的话关个注,点个赞,收个藏🫶。 1507

Django Session机制与Web安全实践解析

会话管理是Web开发中用户状态保持的核心机制,通过服务端存储和客户端Cookie的协同工作实现跨请求数据持久化。Django框架内置了完善的Session系统,支持数据库、缓存等多种存储引擎,并采用加密技术保障数据安全。在电商等高并发场景下,合理的Session存储方案选择能显著提升系统性能,如缓存方案可将响应时间控制在50ms内。同时,CSRF防护机制通过令牌验证和SameSite Cookie等策略,有效防御跨站请求伪造攻击。本文结合Django实战经验,深入剖析Session管理原理与安全实践,为开发

weixin_34274029的博客 426
上一篇: failed to find target android-7 Please install the missing platform
下一篇: Python:用 ElementTree工具解析Maven,Pom.xml文件
yes1cpp
博客等级 码龄14年 15粉丝 21原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值