I am generating a view to search for products and I want to modify it to also search for users when the name starts with '@'.
Right now I have this view:
class BusquedaView(FilterView):
model = Producto
filterset_class = BusquedaFilter
template_name = 'frontend/filtrado.html'
def get_context_data(self, **kwargs):
context = super(BusquedaView, self).get_context_data(**kwargs)
if self.request.GET.get('nombre')[0] == '@':
perfiles = Perfil.objects.filter(
usuario__username__istartswith = self.request.GET.get('nombre')[1:])
context['perfiles'] = perfiles
else:
return context
If you enter the @ character at the beginning, modify the variable template_name
with a different template in which the user's presentation is.
I do not know if there is any way to do that or directly I would have to send it to another URL in which the function get_context_data
is personalized.