Cannot pass current URL as parameter to the component
anil-dewani opened this issue · comments
I've created a component named 'disqus' which accepts two parameters, namely 'url' and 'identifier'.
These paramters are dynamic in nature and value of the same will arrive from request object inside a django template.
I'm using the following code to execute the component but it does not parse the value of the variables request.build_absolute_uri
and request.resolver_match.url_name
. It just sends the plain string as the value, instead of getting actual dynamic value from django.
{% disqus url="{{ request.build_absolute_uri }}" identifier='{{ request.resolver_match.url_name }}' %}
Following is the html code i've setup as the disqus component:
<div id="disqus_thread"></div>
<script>
/**
* RECOMMENDED CONFIGURATION VARIABLES: EDIT AND UNCOMMENT THE SECTION BELOW TO INSERT DYNAMIC VALUES FROM YOUR PLATFORM OR CMS.
* LEARN WHY DEFINING THESE VARIABLES IS IMPORTANT: https://disqus.com/admin/universalcode/#configuration-variables */
var disqus_config = function () {
this.page.url = '{{ url }}';
this.page.identifier = '{{ identifier }}';
};
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = 'https://{{ global_preferences.disqus__site_id }}.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
You are passing a string into the component as an argument.
Try this:
{% disqus url=request.build_absolute_uri identifier=request.resolver_match.url_name %}
I had the case (which is unrelated to your problem, just mentioning it here) where I wanted to pass in a URL determined via the url
tag. That only worked by defining the URL as a variable first and then passing that variable to the component:
{% url 'some:url' as the_url %}
{% foo url=the_url %}
Perfect, this works. Thanks a lot.
Lesson learned.