首页 > 解决方案 > 如何将数据从 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
        )

标签: djangodatatablesdjango-urlsdjango-listview

解决方案


推荐阅读