django - 如何将数据从 javascript-datatable 发送到 Django 并在重定向到其他 url 后,人们说 ajax 但我不知道如何很好地使用它
问题描述
请帮忙,我有一个带有可检查选项的 Jquery 数据表,通过列表视图显示,我想将选定的选项发送到 Django 并在此之后更改当前 URL,大多数人告诉我通过 ajax 发送数据,但我不知道该怎么做
这是我的桌子
<table class="table display" id="table_selecting_formularios">
<thead>
<tr>
<!-- <th scope="col">#</th> -->
<th></th>
<th scope="col">Nombre</th>
<th scope="col">Descripcion</th>
<th scope="col">Subsistema</th>
</tr>
</thead>
<tbody>
{% for formulario in formularios %}
<tr>
<td></td>
<td style="text-transform: uppercase;">
{{formulario.nombre_formulario}}
</td>
<td>{{formulario.descripcion_formulario}}</td>
<td>{{formulario.subsistema_estadistico}}</td>
</tr>
{% endfor %}
</tbody>
</table>
这是我的基于类的视图,在 post 方法中我想从表中获取检查的数据,然后重定向到其他 url
class SelectingFormulariosView(LoginRequiredMixin, generic.ListView):
#? Y que modelo pongo aqui
model = Formulario
template_name = 'forms/publicacion/selecting_formularios.html'
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
def get_queryset(self):
return Formulario.objects.all()
def get(self, request, *args, **kwargs):
context = self.get_context_data()
return self.render_to_response(context)
def post(self, request, *args, **kwargs):
# print(request.POST['formularios[]'])
# names = request.POST['formularios[]']
# # data = Indicador.objects.filter(formulario__nombre_formulario__in=names)
# request.session['formularios'] = names
# return HttpResponseRedirect('/handler/seleccionar-indicadores/')
return reverse_lazy('handler:seleccionar-indicadores')
def get_context_data(self, **kwargs):
self.object_list = super().get_queryset()
context = super(SelectingFormulariosView, self).get_context_data(**kwargs)
try:
subsistemas = [nombre.get('nombre') for nombre in self.request.session.get('publicacion').get('subsistema')]
except Exception as e:
print(e, 'No hay una session con ese nombre')
context['formularios'] = Formulario.objects.filter(subsistema_estadistico__nombre__in=subsistemas)
return context
def render_to_response(self, context, **response_kwargs):
"""
Return a response, using the `response_class` for this view, with a
template rendered with the given context.
Pass response_kwargs to the constructor of the response class.
"""
response_kwargs.setdefault('content_type', self.content_type)
return self.response_class(
request=self.request,
template=self.get_template_names(),
context=context,
using=self.template_engine,
**response_kwargs
)
解决方案
推荐阅读
- python - 如何在 Excel 中替换千位分隔符并在 Python 或 VBA 中保留其他逗号
- python - AWS ECS 中的 Python 应用程序。如何链接到外部应用程序配置?
- c++ - 错误:(-215:断言失败)m.dims <= 2 in function 'FormattedImpl' in cv::dnn
- android - MotionLayout:转换后水平recyclerview不显示第一个元素
- python - 如何在不使用集合的情况下在 Python 中的两个字典值列表中找到公共元素?
- css - 为什么动画持续时间属性不起作用
- mysql - 如何组合仅存在于另一个表中的列中的数据?
- xcode - xcode打开时xed不能跳线
- javascript - 如何在纯JS上排列?
- wordpress - 如何根据 woocommerce 订单中使用的优惠券更改订单状态