Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

780
Views
¿Cómo devuelvo la respuesta JSON en las vistas basadas en clases, en lugar de la respuesta HTTP?

Tengo una vista basada en clases. Estoy usando Ajax en un modal de arranque. Para evitar la actualización de la página, quiero devolver la respuesta JSON en lugar de la respuesta HTTP con esta vista basada en clases, pero solo he visto cómo devolver la respuesta JSON para las vistas basadas en funciones.

vistas.py:

 from django.contrib.auth.mixins import LoginRequiredMixin class TaskCreateView(LoginRequiredMixin, CreateView): template_name = 'create.html' form_class = TaskForm success_url = reverse_lazy('task_list') def get_form_kwargs(self): kwargs = super().get_form_kwargs() kwargs.update(user=self.request.user) return kwargs
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Prueba esto: funciona para mí. en realidad, debe anular el método de publicación si se apega a las vistas basadas en clases.

 import json class TaskCreateView(LoginRequiredMixin, CreateView): form_class = TaskForm success_url = reverse_lazy('task_list') ... def post(self, request, **kwargs): response_data = 'your data' return HttpResponse(json.dumps(response_data),content_type="application/json")
over 4 years ago · Santiago Trujillo Report

0

Pruebe el código a continuación:

 from django.contrib.auth.mixins import LoginRequiredMixin from django.http import JsonResponse class TaskCreateView(LoginRequiredMixin, CreateView): template_name = 'create.html' form_class = TaskForm success_url = reverse_lazy('task_list') def get_form_kwargs(self): kwargs = super().get_form_kwargs() kwargs.update(user=self.request.user) return kwargs def form_valid(self, form): return JsonResponse({'foo':'bar'})
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!