Why request.GET.get('tag', '') returns C rather than C++ in django GET request?

Viewed 69

This is my code in django template:

{% for tag in tags %}
          <a href="{% url 'tag_find' %}?tag={{ tag }}" > {{ tag }}</a>
{% endfor %}

in view.py:

def tag_find(request):
    page = request.GET.get('page', 1)
    tag = request.GET.get('tag', '')
    print('show_tag:', tag)
    ........

Some example are not working perfectly, like:

When tag is C++ then request.GET.get() shows C, when returning tag from template is Portraits&Caricatures then request.GET.get() shows Portraits. I have no idea why request.GET() cuts some part of text.

1 Answers

You need to |urlencode [Django-doc] the value for the tag, the + character is used for spaces:

{% for tag in tags %}
    <a href="{% url 'tag_find' %}?tag={{ tag|urlencode }}" > {{ tag }}</a>
{% endfor %}
Related