首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >django使用物化模式更新表单

django使用物化模式更新表单
EN

Stack Overflow用户
提问于 2020-10-24 07:40:46
回答 1查看 138关注 0票数 0

我在我的django项目中有这段代码,我正在尝试用materialize模式编辑记录,但它总是返回一个id为1 "cpemodels/ edit /1/“的url,所以我只能编辑id为1的记录,我有一个表,每一行都有编辑按钮。(我没有任何问题,当编辑记录与外部页面)。

urls.py

代码语言:javascript
复制
   path('cpemodel/edit/<int:pk>/', cpemodel_edit, name='cpemodel_edit')

views.py

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

代码语言:javascript
复制
<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>
代码语言:javascript
复制
            {% for cpemodel in object\_list %}      
代码语言:javascript
复制
<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代码

代码语言: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);
});

});

EN

回答 1

Stack Overflow用户

发布于 2020-10-25 01:05:03

我刚刚解决了这个问题,注意到.mode-trigger函数总是首先使用.mode-trigger。

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

};

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64508546

复制
相关文章

相似问题

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