Fel
Fel

Reputation: 61

How to save a django "with" template tag value and reuse it in other templates?

I have a template tag that reads values from the URL. For example the searched term is cancer. After searching, the next page that appears would have a with Searched terms: Cancer. And I would like the value cancer to appear in all of my webpages until the user does a new search.

Pages I have work this way: Search.html > display.html > explore.html

Search.html is where the user enters what they want to search for.

I have a searchedterms.html which is included into all 3 templates and contains the Javascript code to read from the URL.

By using a JavaScript code, I managed to display searched terms: cancer in display.html but in explore.html the value is empty. I want to be able to save "cancer" to the tag and use it in other templates.

?conditions=cancer

in search.html:

<input required="" type="text" name="conditions">

in display.html:

{% with searched_terms='searchedterms.html' %}
{% include searched_terms %}
{% endwith %}

in explore.html:

{% with searched_terms='searchedterms.html' %}
{% include searched_terms %}
{% endwith %}

in searchedterms.html:

<div id="searched" class="container"></div>
<script type="text/javascript">
 var urlParams = new URLSearchParams(window.location.search);

  function getUrlParameter(name) {
      name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
      var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
      var results = regex.exec(location.search);
      return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
  };
  var condition = getUrlParameter('conditions');

  document.getElementById("searched").innerHTML = "Searched Terms: " + condition;

</script>

Actual results: Cancer appears in display.html but not in explore.html. When display.html is refreshed "cancer" also disappears.

Desired Results: Cancer appears in display.html and explore.html until the user starts a new search.

Upvotes: 1

Views: 819

Answers (1)

Dipesh Bajgain
Dipesh Bajgain

Reputation: 839

I think Django Inclusion tag is what you are looking for.

Register your tag in templatetags as below:

@register.inclusion_tag('searchedterms.html')
def searched_terms(query):
    return {
        'query': query
    }

and now in your searchedterms.html file:

<div>
your searched query is: {{ query }}  {# display your query here #}
</div>

For Class Based View:

in your Search.html > display.html > explore.html files:

{% load tags %}

{% block content %}

{% searched_terms view.kwargs.conditions %} {# here you pass your kwargs from url #}
<div>some of your existing code. </div>

{% endblock %}

how view.kwargs.conditions work is explained here Access kwargs from a URL in a Django template

For Functional View:
If you want url kwargs in your template from functional views then you can get url kwargs in view and pass it as context data:

def search_view(request, **kwargs):
    """
    your existing codes
    """
    context = {'conditions': kwargs.get('conditions')}
    return render(request, 'search.html', context)

Using request in the template to access from url:
If you want to access the data from url in template then you can also use request.GET or request.POST based on how you want to access the data as in your Search.html > display.html > explore.html files:

{% load tags %}

{% block content %}

{% searched_terms request.GET.conditions %} {# here you access data from url #}
<div>some of your existing code. </div>

{% endblock %}

you can look for django documentation HttpRequest objects for what you can have access with request.

Upvotes: 2

Related Questions