Title: Django listview capture value from url then pass to template
Tags: django
Question: I captures value from url using

```def get_queryset(self):
    name= self.request.GET.get('name')
```

Now want to pass this varible to template. Just like:
your have searched: {{ name }}

Comment: This is *not* what should be implemented in the `get_queryset` function.

Comment: I don't see why you do that in `get_queryset`. That's for, well, getting a queryset. You should do this in `get_context_data` and add it to the context dict there.

Here is the accepted answer: In a class-based view, the ```get_queryset``` is used to - like the name suggests - construct a queryset of objects, for example in a ```ListView``` this queryset is than passed to the template such that one can render the items in the queryset.

Here you want to add something to the context dictionary, in that case you need to patch the ```get_context_data``` [Django-doc] function, so:

```def get_context_data(self, *args, **kwargs):
    context = super().get_context_data(*args, **kwargs)
    context['name'] = self.request.GET.get('name')
    return context```

Note that you should return a dictionary, so here we alter the dictionary that we obtain from a super call. Calling the ```super().get_context_data(..)``` method is not required, you could for example return a dictionary with only the attributes you want. But it is typically not good design, since a view typically already adds a lot of data to the context dictionary, and now you can no longer use that in the template.


Here is another answer: Use this

```def get_context_data(self, **kwargs):
    name = self.request.GET.get('name')
    context = super().get_context_data(**kwargs)
    context["name"] = name
    return context
```

now the name variable is available in template

in template you can use {{name}}


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:10/31/18', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<django>', 'Title': 'Django listview capture value from url then pass to template', 'ViewCount': 226, 'id': 53079888, 'user_AboutMe': '', 'user_AccountId': 6006585.0, 'user_DisplayName': 'nikolas', 'user_Reputation': 1460.0, 'user_Views': 204.0, 'user_id': 4718335.0}}