How to use ViewSet to return templates in django

0

I'm new to python, I've reviewed how class-based views work but I find it tedious to have a generic view for each action, for example to get the ListView list, to create the CreateView and so on.

class CrearUsuarioView(CreateView):
model = User
template_name = 'user/create_form.html'
form_class = CrearUsuarioForm

def post(self, request, *args, **kwargs):
    if not request.is_ajax():
        return HttpResponseBadRequest("<h1>{}</h1>".format("Bad Request"))
    form = CrearUsuarioForm(request.POST)
    if form.is_valid():
        form.save()
        return JsonResponse({'status': 'success', 'message': 'Guardado correctamente'})
    else:
        return JsonResponse({'status': 'error', 'error_list': form.errors})


class EditarUsuario(UpdateView):
model = User
template_name = 'user/form.html'
context_object_name = 'data'
form_class = EditarUsuarioForm

def get_context_data(self, **kwargs):
    data = super().get_context_data(**kwargs)
    data['pageUrl'] = "/general/usuarios"
    data['pageModule'] = "usuarios"
    return data

def post(self, request, *args, **kwargs):
    form_user = User.objects.get(pk=kwargs.get('pk'))
    user_form = EditarUsuarioForm(request.POST, request.FILES, instance=form_user)
    if user_form.is_valid():
        user_form.save(request=request)
        return JsonResponse({'status': 'success', 'message': 'Los cambios fueron guardados correctamente.'})
    else:
        return JsonResponse({'status': 'error', 'error_list': user_form.errors})



class ListadoUsuarios(ListView):
paginate_by = 10
template_name = 'user/index.html'
model = User

def get_queryset(self):
    search = self.request.GET.get('s', '')
    if search != '':
        object_list = self.model.objects.filter(
            Q(username__icontains=search) | Q(first_name__icontains=search) | Q(last_name__icontains=search) | Q(
                email__icontains=search)).order_by('username')
    else:
        object_list = self.model.objects.all().order_by('username')
    return object_list

def get_context_data(self, **kwargs):
    data = super().get_context_data(**kwargs)
    data['pageTitle'] = _('Listado de Usuarios')
    data['pageNote'] = _('Mostrando todos los registros')
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
    data['list_display'] = list_display
    return data

I have the urls this way but I would like to have only one that contains all the methods crud something similar to how Laravel works through Route :: resource ('users') would create all the methods create, update, destroy, show

path('usuarios/<int:pk>/edit', EditarUsuario.as_view()),
path('usuarios/create', CrearUsuarioView.as_view()),

I am interested in having a single view that simplifies the use of all these generic views, I have read a bit about it and it seems that ViewSet is the solution but this gives me an API and I can not return the renderings with the respective templates in the case of creating for example.

    
asked by Ricardo Orellana 08.05.2018 в 23:52
source

2 answers

0

You can use Django rest Framework here is an example link

    
answered by 10.05.2018 в 17:00
0

Being able to manage everything in the same view can be done, I usually create, update and delete use a single view, inheriting from "UpdateView". Everything depends on what you need. If you want to learn more how class-based views work internally, I invite you to look at the different repository files "django / views / generic"

    
answered by 18.05.2018 в 18:21