Viktor
Viktor

Reputation: 489

Render different templates for ListView

I have a ListView with some good functionality that i want to use from another app. The way i did it was using get_template_names.

def get_template_names(self):
        referer = self.request.META['HTTP_REFERER']
        if "/mwo/order/" in referer:
            return ['invent/use_part.html']
        return ['invent/part_list.html']

Which i access from two different apps:

path('inventory/', PartListView.as_view(), name='partlist'),
...
path('mwo/order/<int:pk>/add_part/', PartListView.as_view(),
            name='add_part'),

But it causes a bug if i use a direct link to 1st url from navbar and not from another app. Now i'm new to django and i'm pretty sure there should be a better way for this. What can i use instead of request referrer to render different template for ListView when i access it from another view.

Upvotes: 1

Views: 330

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476659

You can specify the template_name in the .as_view:

path('inventory/', PartListView.as_view(template_name='invent/part_list.html'), name='partlist'),
# …
path('mwo/order/<int:pk>/add_part/', PartListView.as_view(template_name='invent/use_part.html'), name='add_part'),

Then of course you should remove the get_template_names from the method, since otherwise you will override that behaviour.

Upvotes: 1

Related Questions