首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在确认后将数据从表单发送到视图?

如何在确认后将数据从表单发送到视图?
EN

Stack Overflow用户
提问于 2019-06-26 14:38:07
回答 2查看 87关注 0票数 0

我创建了一个简单的表单,请求用户输入,然后将其发送到数据库。现在,我希望用户确认表单中的数据是正确的,为此,我使用了Bootstrap modal

如何在按下模式上的'OK' button时将数据从表单发送到视图。

我是Django framework的新手,也许有另一种更好的方法而不使用引导模态。

表格:

代码语言:javascript
复制
class ReportForm(forms.Form):
    report_title = forms.ChoiceField(choices=get_report_titles())
    report_link = forms.CharField(widget=forms.Textarea, required=False)

Html文件:

代码语言:javascript
复制
<form class="w-100" action="" method="post">
    {% csrf_token %}
    <label class="pl-0 mt-auto mr-2">Report&nbsp;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">&times;</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>

查看:

代码语言:javascript
复制
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()
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-26 16:07:27

只需使用这些代码行更新您的代码,添加id="exampleForm"

表单标签的开头为

代码语言:javascript
复制
<form class="w-100" action="" id="exampleForm" method="post">

将保存按钮替换为(添加id="save"):

代码语言:javascript
复制
<button type="button" id="save" class="btn btn-primary">Save</button>

最后,在底部添加此脚本,以便在单击save时提交表单:

代码语言:javascript
复制
<script>
$("#save").on("click", function(e) {
    $("#exampleForm").submit();
});
</script>

另外,我觉得你的view写得不正确。它应该是这样的,我不确定你想要做什么:

代码语言:javascript
复制
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()

如果你还需要帮助,请告诉我

票数 1
EN

Stack Overflow用户

发布于 2019-06-26 15:58:22

这需要改变,因为你没有给出任何值。

代码语言:javascript
复制
  <select name="report_title" class="form-control report-name">
     <option selected>Select report</option>
     {% for name in report_names %}
     <option>{{ name }}</option>
     {% endfor %}
  </select>

尝试:

代码语言:javascript
复制
  <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,则只需执行以下操作:

代码语言:javascript
复制
if form.is_valid():
    new_report = form.save()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56766616

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档