【发布时间】: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