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 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
CodePudding user response:
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.
CodePudding user response:
you can look at
Now let us learn how we can implement this in Django.
How to get parameter from URL in Django Django URL pass parameter to view Django URL parameters in template Get parameters from URL in Django example Path converters Django Django URL parameters string Django URL slug parameter
the link - https://pythonguides.com/get-url-parameters-in-django/
