【问题标题】:django update form with materialize modal带有物化模式的django更新表单
【发布时间】:2020-10-23 23:40:46
【问题描述】:

我的 django 项目中有这段代码,我正在尝试使用物化模式编辑记录,但它总是返回一个 id 为 1“cpemodels/edit/1/”的 url,因此我只能使用id 1,我在每一行都有一个带有编辑按钮的表格。 (使用外部页面编辑记录时我没有任何问题)。

urls.py

   path('cpemodel/edit/<int:pk>/', cpemodel_edit, name='cpemodel_edit')

views.py

def cpemodel_edit(request, pk=None):
cmodel = get_object_or_404(CPEMODEL, pk=pk)
if request.method == "POST":
    form = CPEMODELForm(request.POST, instance=cmodel)
    if form.is_valid():
        form.save()
        return redirect('cpemodel')
else:
    form = CPEMODELForm(instance=cmodel)

return render(request,'provision/cpemodel/edit.html',{'form': form, 'cmodel': cmodel })

cpemodel.html

<table class="responsive-table striped">
<thead>
<tr>
    <th>Modelo</th>
    <th>Descripción</th>
    <th>Vendor</th>
    <th>Tipo</th>
    <th>ETHs</th>
    <th>POTs</th>
    <th>TV</th>
    <th>WIFI</th>
    <th></th>
</tr>
{% for cpemodel in object_list %}
<tr>
    <td>{{cpemodel.model}}</td>
    <td>{{cpemodel.desc}}</td>
    <td>{{cpemodel.vendor}}</td>
    <td>{{cpemodel.type}}</td>
    <td>{{cpemodel.eth_ports}}</td>
    <td>{{cpemodel.pot_ports}}</td>
    <td>{{cpemodel.catv_port}}</td>
    <td>{{cpemodel.wifi}}</td>

    <td><a class="btn-floating waves-effect waves-light green light-3 hoverable" href="{% url 'cpemodel_edit' cpemodel.id %}"><i class="material-icons">edit</i></a></td>
    <td><a class="btn-floating waves-effect waves-light red light-3 hoverable" href="{% url 'cpemodel_delete' cpemodel.id %}"><i class="material-icons">delete</i></a></td>

    <td><button class="waves-effect waves-light btn modal-trigger" data-source="cpemodel/edit/{{cpemodel.id}}/"  href="#modal1">Modal</button></td>

    <div id="modal1" class="modal">
      <div class="modal-content"></div>
    </div>

javascript 代码

$(document).ready(function(){

$('.modal').modal(); 
$('.modal-trigger').click(function(){ 
var url = $('.modal-trigger').attr("data-source");
// use other ajax submission type for post, put ...
$.get( url, function( data ) {
    // use this method you need to handle the response from the view 
    // with rails Server-Generated JavaScript Responses this is portion will be in a .js.erb file  
    $( ".modal-content" ).html(data);
});

});

【问题讨论】:

    标签: jquery django materialize


    【解决方案1】:

    我刚刚解决了,注意到 .modal-trigger 函数总是先使用 .modal-trigger。

    <td><button class="waves-effect waves-light btn modal-trigger" onclick="edit('{% url 'cpemodel_edit' cpemodel.id %}')">Editar</button></td>
      function edit(url){ 
     $('.modal-content').load (url, function() {
      $('.modal').modal(); 
      $('#modal1').modal('open');
    });
    

    };

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 1970-01-01
      • 2010-10-12
      • 2020-11-28
      • 2019-07-03
      • 1970-01-01
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多