L
L
Leonid2018-07-21 20:19:37
Django
Leonid, 2018-07-21 20:19:37

How to generate a dynamic query string in a template in django?

There is some page with a list of articles based on ListView
I added some hyperlink blocks to sort and display articles for example:
Block 1
Detailed|Shortly
Block 2
Per day|Per week|Per month
Block 3 (number of articles per page)
10|20|50
I pass the output parameters in the query string:
article/?sort=table&pag=10&date=week
I tried to do it through

@register.simple_tag
def dinamic(value, str_filtre, str_value):

creating something like this in the template:
<a href="{% url 'article:list' %}{% dinamic request.get_full_path "sort" "list" %}">полно</a>|

but the goldfish does not come out ... the parameters are doubled when you click twice on one block, for example (by date)
article/?sort=table&pag=15&date=week&date=month
tell me how to generate such links. Throw an example .. I will be grateful

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Astrohas, 2018-07-21
@Astrohas

Hmm in dinamic-e will not be replaced? Use https://docs.python.org/3/library/urllib.parse.htm...

A
Alexander Bragin, 2018-07-22
@smiley

You can generate urls for the current page like this.

@register.simple_tag(takes_context=True)
def dinamic(context, **kwargs):
    request = context['request']
    qparams = request.GET.dict()
    qparams.update(**kwargs)
    return '{}?{}'.format(request.path_info,
                          urlencode(qparams))

{% dynamic param1='value1' %}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question