gpt4 book ai didi

python - 如何在django中保存多个同名的html输入?

转载 作者:行者123 更新时间:2023-12-01 09:26:55 25 4
gpt4 key购买 nike

大家好,我想使用 django 将动态输入的 html 数组保存到数据库中,我的表单上有三个动态输入,这是我的代码:

View .py

def generatescripts(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = GenerateScriptsForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:

Step = request.POST.getlist('Step')

for el in Step:
form.save()

return HttpResponseRedirect('/thanks/')

# if a GET (or any other method) we'll create a blank form
else:
form = GenerateScriptsForm()

return render(request, 'page/generatescripts.html', {'form': form})

表单.py

from django import forms
from .models import Steps

class GenerateScriptsForm(forms.ModelForm):

class Meta:
model= Steps
fields = "__all__"

模型.py

from django.db import models

# Create your models here.
class Steps(models.Model):

Step = models.CharField(max_length=200,default='0')
Description = models.CharField(max_length=200,default='0')
Result = models.CharField(max_length=200,default='0')

多个输入具有相同的名称,我无法使用我的代码保存,此代码仅保存数组中的一个值生成脚本.html

<input type="button" value="Preview form" class="add" id="preview" />
<input type="button" value="Add a Step" class="add" id="add" />

<script>
$(document).ready(function() {
$("#add").click(function() {
var lastField = $("#buildyourform div:last");
var intId = (lastField && lastField.length && lastField.data("idx") + 1) || 1;
var fieldWrapper = $("<div class=\"fieldwrapper\" id=\"field" + intId + "\"/>");
fieldWrapper.data("idx", intId);
var step = $("<input type=\"text\" name= \"Step\" placeholder= \"Step\" class=\"fieldname\" />");
var description = $("<input type=\"text\" name= \"Description\" placeholder= \"description\" class=\"fieldname\" />");
var expectedresult = $("<input type=\"text\" name= \"Result\" class=\"fieldname\" />");

var fType = $("<select class=\"fieldtype\"><option value=\"checkbox\">Checked</option><option value=\"textbox\">Text</option><option value=\"textarea\">Paragraph</option></select>");
var removeButton = $("<input type=\"button\" class=\"remove\" value=\"-\" />");
removeButton.click(function() {
$(this).parent().remove();
});
fieldWrapper.append(step);
fieldWrapper.append(description);
fieldWrapper.append(expectedresult);
fieldWrapper.append(fType);
fieldWrapper.append(removeButton);
$("#buildyourform").append(fieldWrapper);
});
$("#preview").click(function() {
$("#yourform").remove();
var fieldSet = $("<fieldset id=\"yourform\"><legend>Your Form</legend></fieldset>");
$("#buildyourform div").each(function() {
var id = "input" + $(this).attr("id").replace("field","");
var label = $("<label for=\"" + id + "\">" + $(this).find("input.fieldname").first().val() + "</label>");
var input;
switch ($(this).find("select.fieldtype").first().val()) {
case "checkbox":
input = $("<input type=\"checkbox\" id=\"" + id + "\" name=\"" + id + "\" />");
break;
case "textbox":
input = $("<input type=\"text\" id=\"" + id + "\" name=\"" + id + "\" />");
break;
case "textarea":
input = $("<textarea id=\"" + id + "\" name=\"" + id + "\" ></textarea>");
break;
}
fieldSet.append(label);
fieldSet.append(input);
});
$("body").append(fieldSet);
});
});
</script>
<input type="submit" />

</form>

{% endblock %}

最佳答案

我刚刚读了你的 View 函数,将其更改为:

def generatescripts(request):
if request.method == 'POST':
Steps = request.POST.getlist('Step')
Results = request.POST.getlist('Result')
Descriptions = request.POST.getlist('Description')

# FIXME: number of each field should equal
c = min([len(Steps), len(Results), len(Descriptions)])
for i in range(c):
# create a form instance and populate it with data from the request:
form = GenerateScriptsForm({'Step': Steps[i], 'Result': Results[i], 'Description': Descriptions[i]})
# check whether it's valid:
if form.is_valid():
form.save()
return HttpResponseRedirect('/thanks/')

else:
form = GenerateScriptsForm()

return render(request, 'page/generatescripts.html', {'form': form})

关于python - 如何在django中保存多个同名的html输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50313153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com