簡體   English   中英

阻止Django模態重定向到新頁面

[英]Stop Django modal from redirecting to a new page

我正在嘗試通過使用模式(彈出窗口進行編輯)來實現編輯表單。 我有一個項目列表,每個項目旁邊都有一個編輯鏈接。 彈出的表單將由對象id填充。

我遇到的問題是,當我單擊“編輯”時,會打開一個新頁面,但我希望在同一頁面上打開一個模式窗口。

我遇到的另一個問題是“取消”和“ X”按鈕不起作用,而“提交”按鈕卻起作用。 非常感謝您提供有關解決此問題的任何想法。

detail.html(項目列表)

<h1>{{ inventory.id }} {{ inventory.inventory_name }} created on {{ inventory.pub_date }}</h1>

<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    </div>
    <table cellpadding="1" cellspacing="1" id="detailTable" class="table table-striped table-bordered">
    <thead>
        <th>ID</th>
        <th>NAME</th>
        <th>STATUS</th>
        <th>DERIVATIVES</th>
        <th>SUBSYSTEMS</th>
        <th>TIME ADDED</th>
        <th>TIME EDITED</th>
        <th>EDIT</th>
    </thead>
    <tbody>{% for block in inventory.block_set.all %}
        <tr>
            <td>{{ block.id }}</td>
            <td>{{ block.block_name }}</td>
            <td>{{ block.block_status }}</td>
            <td>{{ block.block_derivatives }}</td>
            <td>{{ block.block_subsystems }}</td>
            <td>{{ block.added }}</td>
            <td>{{ block.updated }}</td>
            **<!-- LINK TO THE EDIT FORM MODAL -->**
            **<td><a class="fa fa-pencil" data-toggle="modal" href="/inventory/{{ inventory.id }}/editblock/{{ block.id }}/" data-target="#modal" title="edit item" data-tooltip>Edit</a>
            </td>**
            **<!-- LINK TO THE EDIT FORM MODAL -->**
        </tr>{% endfor %}</tbody>
</table>{% if user.is_authenticated and user.is_active %}
<div display="inline">
    <p>Make a request: <a href="{% url 'inventory:requests' inventory.id %}">Request Form</a> 
    </p>{% else %}
    <div display="inline">
        <p>You must be authorized to make a request.</p>
        <form id="login_form" method="post" action="" autocomplete="off">{% csrf_token %}
            <input style="display:none;" type="text" name="somefakename" />
            <input style="display:none;" type="password" name="anotherfakename" />
            <input type="text" name="username" value="" size="50" placeholder="Username" />
            <br />
            <input id="passwrd" type="password" name="password" value="" size="50" placeholder="Password" />
            <br />
            <input id='login' type="submit" value="login" />
            <p style="display:inline; padding:10px"></p>
        </form>
        {% endif %}
    </div>

editForm.html:

{% load staticfiles %}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<script src="{% static " inventory/js/googleAPIJquery.js " %}" type="text/javascript"></script>
<div class="modal-dialog modal-md">
    <div class="modal-content">
    <form id="block_update_form" method='post' class="form" role="form" action='.'>
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                 <h4 class="modal-title" id="myModalLabel">Block {{ block.id }}</h4> 
        </div>
        <div class="modal-body">{% csrf_token %} {{ form.non_field_errors }}
            <div class="form-group">{% for field in form %}
                <div class="form-group">{% if field.errors %}
                    <ul class="form-errors">{% for error in field.errors %}
                        <li><span class="fa fa-exclamation-triangle"></span>  <strong>{{ error|escape }}</strong>

                        </li>{% endfor %}</ul>{% endif %} {{ field.label_tag }} {{ field }} {% if field.help_text %}
                    <div class="form-helptext">{{ field.help_text }}</div>{% endif %}</div>{% endfor %}</div>
            <div class="modal-footer">
                <input type="button" class="btn btn-default" data-dismiss="modal" value="Cancel" />
                <input type="submit" class="btn btn-primary" value="save" style="margin-bottom: 5px;" />
            </div>
    </form>
    <script>
        jQuery('.modal-content .calendar').datepicker({
            dateFormat: "yy-mm-dd"
        });
        var form_options = {
            target: '#modal',
            success: function() {}
        }
        $('#block_update_form').ajaxForm(form_options);
    </script>
    </div>
    <!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->

<td><a class="fa fa-pencil" data-toggle="modal" href="/inventory/{{ inventory.id }}/editblock/{{ block.id }}/" data-target="#modal" title="edit item" data-tooltip>Edit</a>
</td>

<div class="modal-dialog modal-md">
    <div class="modal-content">
    {% include 'editForm.html' with form=form %}

</div>
</div>

為您的html:

http://getbootstrap.com/javascript/#modals

和:

{% include 'editForm.html' with form=form, any_parameters = any_parameters %}

例如:views.py:

context['form'] = CommentForm(request.POST or None)

在main.html中:

{% include 'area/comment-form.html' with form=form %}

在comment-form.html中:

<form method="post" class="form-horizontal" action='{% url 'add-comment' id=platform.id %}'>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM