6.2. Using Function Based Views (FBV)

Using function-based views, the way is somewhat manual but it is quite simple to use the results in the same way.

6.2.1. List

from folder_project.clients import api_client

...

def get_orders(request):
  response = api_client.order.orders.list()
  template = 'fbv/list.html'
  context = {"object_list": response.as_obj().results}
  return render(request, template, context)

Note

The usage example assumes that the endpoint /order/orders/ Waiting the response {‘count’: , ‘next’: None, ‘previous’: None, ‘results’: []}

6.2.2. Detail

from folder_project.clients import api_client

...

def get_order(request, pk):
  response = api_client.order.orders.get(id=pk)
  template = 'fbv/detail.html'
  context = {"object": response.as_obj()}
  return render(request, template, context)

Note

The usage example assumes that the endpoint /order/orders/

6.2.3. Create

from folder_project.clients import api_client

...

def create_order(request, pk):
  template = 'fbv/form.html'

  form = TestForm(request.POST or None)
  if form.is_valid():
      response = api_client.order.orders.create(data=form.cleaned_data)
      return redirect('fbv:index')

  context = {"form": form}
  return render(request, template, context)

Note

The usage example assumes that the endpoint /order/orders/

6.2.4. Update

from folder_project.clients import api_client

...

def update_order(request, pk):
  template = 'fbv/form.html'
  response = api_client.order.orders.get(id=pk)

  form = TestForm(request.POST or None, initial=response.as_dict())
  if form.is_valid():
      response = api_client.order.orders.update(id=pk, data=form.cleaned_data, partial=False)
      return redirect('fbv:index')

  context = {
    "object": response.as_obj(),
    "form": form
   }
  return render(request, template, context)

Note

The usage example assumes that the endpoint /order/orders/

6.2.5. Delete

from folder_project.clients import api_client

...
def delete_order(request, pk):
    template = 'fbv/delete.html'
    response = api_client.order.orders.get(id=pk)
    if request.method == 'POST':
        api_client.order.orders.delete(id=pk)
        return redirect('fbv:index')
    context = {"object": response.as_obj()}
    return render(request, template, context)

Note

The usage example assumes that the endpoint /order/orders/