我创建了一个简单的表单,请求用户输入,然后将其发送到数据库。现在,我希望用户确认表单中的数据是正确的,为此,我使用了Bootstrap modal。
如何在按下模式上的'OK' button时将数据从表单发送到视图。
我是Django framework的新手,也许有另一种更好的方法而不使用引导模态。
表格:
class ReportForm(forms.Form):
report_title = forms.ChoiceField(choices=get_report_titles())
report_link = forms.CharField(widget=forms.Textarea, required=False)Html文件:
<form class="w-100" action="" method="post">
{% csrf_token %}
<label class="pl-0 mt-auto mr-2">Report Name</label>
<select name="report_title" class="form-control report-name">
<option selected>Select report</option>
{% for name in report_names %}
<option>{{ name }}</option>
{% endfor %}
</select>
<div class="my-1 col-lg-12 float-left pl-0">
<label>Report Link</label>
<input class="form-control bg-white" type="text" id="report" name="report_link">
</div>
<input id="confirm" value="Save" type="button" data-toggle="modal" data-target="#exampleModalCenter" class="btn btn-outline-success" />
</form>
<!-- Modal -->
<div class=" modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Confirmation</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
Make sure you have the right title and link.
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>查看:
def report_view(request):
if request.method == 'POST':
form = ReportForm(request.POST)
if form.is_valid():
report_title = form.cleaned_data['report_title']
report_link = form.cleaned_data['report_link']
new_report = Report(title = report_title, link = report_link)
new_report.save()发布于 2019-06-26 16:07:27
只需使用这些代码行更新您的代码,添加id="exampleForm"。
表单标签的开头为
<form class="w-100" action="" id="exampleForm" method="post">将保存按钮替换为(添加id="save"):
<button type="button" id="save" class="btn btn-primary">Save</button>最后,在底部添加此脚本,以便在单击save时提交表单:
<script>
$("#save").on("click", function(e) {
$("#exampleForm").submit();
});
</script>另外,我觉得你的view写得不正确。它应该是这样的,我不确定你想要做什么:
def report_view(request):
if request.method == 'POST' and form.is_valid():
form = ReportForm(request.POST)
report_title = form.cleaned_data['report_title']
report_link = form.cleaned_data['report_link']
new_report = Report(title = report_title, link = report_link)
new_report.save()如果你还需要帮助,请告诉我
发布于 2019-06-26 15:58:22
这需要改变,因为你没有给出任何值。
<select name="report_title" class="form-control report-name">
<option selected>Select report</option>
{% for name in report_names %}
<option>{{ name }}</option>
{% endfor %}
</select>尝试:
<select name="report_title" class="form-control report-name">
<option selected>Select report</option>
{% for name in report_names %}
<option value="{{name.pk}}">{{ name }}</option>
{% endfor %}
</select>其中,name.pk是与选项相关的值。
此外,如果您将表单更改为ModelForm,则只需执行以下操作:
if form.is_valid():
new_report = form.save()https://stackoverflow.com/questions/56766616
复制相似问题