我在我的django项目中有这段代码,我正在尝试用materialize模式编辑记录,但它总是返回一个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);
});});
发布于 2020-10-25 01:05:03
我刚刚解决了这个问题,注意到.mode-trigger函数总是首先使用.mode-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');
});};
https://stackoverflow.com/questions/64508546
复制相似问题