Reputation: 55
I'm trying to send a parameter by URL and trying to use it in a listview. But I cant GET it in my view. I know I'm doing some stupid mistake, but can't find it.
This is the link I'm using in my template:
<a class="dropdown-item" href="{% url 'listarmateriales' tipolista='metales' %}">Ver metales
This is my url.py:
path('listarmateriales/<tipolista>',listarmateriales.as_view(), name='listarmateriales'),
And this is where I try to get the "tipolista" in my views.py:
class listarmateriales(ListView):
template_name = 'materiales/materialeslist.html'
def get_queryset(self):
if self.request.GET.get('tipolista') is not None:
lista=Materiales.objects.all()
tipolista=self.request.GET.get('tipolista' '')
if tipolista=='metales':
Do whatever...
else:
I have tried some different sintaxis, but the result is always the same, I cant read "tipolista", I have spent a lot of time with this, too much. I know it must be something easy, but, I'm new at this. Thanks in advance
Upvotes: 3
Views: 9350
Reputation: 267
For the urls.py to recognize tipolista as a string variable, you should pass tipolista as a view paramater. So, your view function should be something like this:
def get_queryset(self, tipolista):
if self.request.GET.get('tipolista') is not None:
lista=Materiales.objects.all()
if tipolista=='metales':
#...
else:
#...
And in your url pattern, tipolista must be preceded by 'str':
path('listarmateriales/<str:tipolista>',listarmateriales.as_view(), name='listarmateriales'),
Upvotes: 1
Reputation: 477533
These are not GET parameters, these are the URL parameters. These are stored in self.kwargs
, so you access these with:
def get_queryset(self):
lista = Materiales.objects.all()
tipolista = self.kwargs['tipolista']
if tipolista=='metales':
# …
else:
# …
these can not be None
, since these exist in order to "fire" the view.
Upvotes: 4