要求确认后如何从表格发送数据以进行查看?

时间:2019-06-26 06:38:06

标签: python django bootstrap-modal

我创建了一个简单的表单,要求用户输入然后将其发布到数据库。现在,我希望用户确认表格中的数据正确无误,为此,我正在使用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&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>

查看:

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()

2 个答案:

答案 0 :(得分:1)

使用这些行简单地更新代码,添加abstract class Subscribable<T> { private SubscriptionManager<T> subscriptionManager; protected Subscribable(SubscriptionManager<T> subscriptionManager) { this.subscriptionManager = subscriptionManager; } void addSub(Subscription<T> sub) { subscriptionManager.addSub(sub); } } class A extends Subscribable<A> { public A(SubscriptionManager<A> subscriptionManager) { super(subscriptionManager); } } class B extends Subscribable<B> { public B(SubscriptionManager<B> subscriptionManager) { super(subscriptionManager); } }

使用

开始表单标签
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

让我知道您是否仍然需要帮助

答案 1 :(得分:0)

这需要更改,因为您没有提供任何值。

  <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()