django - django表单提交后触发引导模式

标签 django modal-dialog bootstrap-modal

提交 django 表单后如何触发引导模式弹出?

在我的 index.html 模板中,我有一个像这样的标准模式

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
              <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
              </button>
            </div>
            <div class="modal-body">
              ...
            </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 changes</button>
            </div>
          </div>
        </div>
      </div>

在同一个index.html下面我有一个django表单

  {{ form.non_field_errors }}
                {% csrf_token %}
                    <ul id="robul">
                      <div class="form-group">
                        <div class="col-xs-6">
                        <li id="name" name="{{ form.name.name }}" class="form-control">{{ form.name }}</li>
                        </div>
                      </div>
                      <div class="form-group">
                        <div class="col-xs-6">
                        <li id="email" class="form-control">{{ form.email }}</li>
                      </div>
                    </div>
                      <div class="form-group">
                        <div class="col-xs-6">
                        <li id="contactmessage" class="form-control">{{ form.contactmessage }}</li>
                      </div>
                    </div>
                    </ul>

在我的 view.py 中看起来像这样:

if request.method == 'POST':
        form = forms.FormName(request.POST)

        if form.is_valid():
            contact_name = request.POST.get(
                'name', '')
            contact_email = request.POST.get(
                'email', '')
            form_content = request.POST.get('contactmessage', '')

            template = get_template('contact_template.txt')
            context = {'name': contact_name,
            'email': contact_email,
            'contactmessage': form_content,}
            content = template.render(context)
            mail = EmailMessage("New contact form submission", content, "Some Name" +'', ['somegmail@gmail.com'],
            headers = {'Reply-To': "noreply@gmail.com" })
            mail.send()
    return render(request, 'index.html', {'form': form})

最佳答案

用于触发它的模态和 JS 代码与 Django 表单提交代码存在于不同的上下文中。渲染 'index.html'当您从表单提交返回时,基本上是重新开始,因此它基本上相当于在页面加载时显示模式。但是,也许您只想在成功提交后显示它。您需要做的是有一些 JS 代码在页面加载时显示模式,并在模板渲染上下文中有一些东西来有条件地渲染它。这是我的想法:

index.html :

{% if successful_submit %}
    <script type="text/javascript">
        $(document).ready(function(){
            $("#exampleModal").modal('show');
        });
    </script>
{% endif %}

在您的 View 函数中,添加 successful_submit您返回的上下文变量:

return render(request, 'index.html', {'form': form, 'successful_submit': True})

现在,<script>只有在 successful_submit 时才会呈现标签是 True , 只有在表单成功后才会设置 POST .

关于django - django表单提交后触发引导模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48327301/

相关文章:

javascript - Twitter Bootstrap 2.0.4 Modal - 如何将模式淡入淡出效果应用于新创建的模式类样式?

jQuery BlockUI : Is it possible to run a function after a user hits the "enter" key?

javascript - 模态对话框解析忽略传递的属性值

database - 如何将正在使用的数据库复制到django中的其他数据库?

python - 根据移动设备与桌面优化 Django WebApp 的显示

javascript - AngularJS 出错

twitter-bootstrap - 模态中的模态 - 如何仅关闭内部模态

bootstrap-modal - 在页面上移动 Bootstrap 模态

python - Django 中的过滤器值显示奇怪

python - Django 和 fcgi - 日志记录问题