这是我的views.py,其中包含删除功能和templateview
def delete_curricula_info(request):
curricula_info = request.GET.get('curricula_info')
print(curricula_info)
if request.is_ajax:
CurriculaInfo.objects.get(curricula_info_id=curricula_info).delete()
else:
pass
data = {
'result': 'Successfully Deleted'
}
return JsonResponse(data)
class CurriculumClasses(TemplateView):
template_name = 'architect/assignclasses.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = AssignClassForm(self.request.GET or None)
context['classes'] = Classes.objects.all()
context['curricula_infos'] = CurriculaInfo.objects.all()
context['course_abbr'] = self.kwargs.get('course_abbr')
context['year'] = self.kwargs.get('year')
context['semester'] = self.kwargs.get('semester')
context['module_abbr'] = self.kwargs.get('module_abbr')
context['curricula'] = Curricula.objects.get(
course_period__course__course_abbreviation=self.kwargs.get('course_abbr'),
module_period__module__module_abbreviation=self.kwargs.get('module_abbr'),
course_period__period__academic_year=self.kwargs.get('year'),
course_period__period__semester=self.kwargs.get('semester')).pk
return context
我的urls.py都在这里显示
path('courses/<str:course_abbr>/<str:module_abbr>/<int:year>/<int:semester>/classes/',
views.CurriculumClasses.as_view(), name='curriculum_classes'),
path('ajax/delete-curricula-info', views.delete_curricula_info, name='delete_curricula_info')
这是我的模板,其中包含html列表元素和ajax的javascript:
<ul class="list-group t20">
{% for curricula_info in curricula_infos %}
<a href="{% url 'delete_curricula_info' %}">
<li class="list-group-item" id="delete_curricula_info"><b>{{ curricula_info.classes }}</b> taught
by: <b>{{ curricula_info.designation }}</b></li>
</a>
{% endfor %}
</ul>
<script>
$('#delete_curricula_info').click(function () {
let curricula_info = $("#{{ curricula_info.curricula_info_id }}").val();;
$.ajax({
url: '{% url 'delete_curricula_info' %}',
data: {
'curricula_info': curricula_info
},
dataType: 'json',
success: function (data) {
swal({
title: data.result,
toast: true,
position: 'top-end',
type: 'success',
showConfirmButton: false,
allowEscapeKey: false,
allowOutsideClick: false,
backdrop: false,
timer: 1500,
})
},
error: function () {
swal({
title: "Error",
toast: true,
position: 'top-end',
type: 'error',
showConfirmButton: false,
allowEscapeKey: false,
allowOutsideClick: false,
backdrop: false,
timer: 1500,
})
}
});
});
所以基本上,我试图让我的删除功能使用ajax来工作,但是当我运行它时,删除功能中的“ curricula_info”具有“无”值,我该如何解决这个问题,请帮忙