0

I've created a serializer and ViewSet for my model and added a template for a list view. To get to see the web page (template) the ordering render classes must be correct and one needs to add the TemplateHTMLRenderer to the list of renderers.

This now leads to the issue that when wanting to browse to a specific record like

/mymodel/5

in the browser, I get shown the list view too.

The goal is to have 1 url that serves the api (json) or the web page both for list and detail views. (/mymodel = list, /mymodel/5 = detail)

The question is: how can I have multiple templates (list/detail) based on one ViewSet?

beginner_
  • 7,230
  • 18
  • 70
  • 127

1 Answers1

0

The solution is to override the get_template_names method and return the template according to the action being performed.

def get_template_names(self):
    if self.action == 'list':            
        return ['list.html']
    elif self.action == 'retrieve':            
        return ['details.html']
beginner_
  • 7,230
  • 18
  • 70
  • 127