Reputation: 19983
I have a comment_form.html
template, which is used in multiple places in my app, and I'd like to be able to pass the endpoint's url into that template from a parent template. Normally I would do this using the with
tag:
{% with endpoint='/comments' %}
{% include 'comment_form.html' %}
{% endwith %}
The problem is that I can't use a string literal '/comments'
here, but instead I need a url tag, like so: {% url 'blog:comments:comments' username=post.user.username object_id=post.id %}
. The with
template tag seems to expects a literal or a context variable and doesn't seem to be able to comprehend "use the result of another template tag".
One solution would be to pass the strings 'blog:comments:comments', post.user.username, post.id all separately. But this is a problem because different uses of the comment form may require different arguments to uniquely define the endpoint.
How can I use with
with the result of another template tag?
Upvotes: 1
Views: 512
Reputation: 599778
You can't, but you don't need to. The url
tag has an alternative syntax that injects is result into the context:
{% url 'blog:comments:comments' username=post.user.username object_id=post.id as endpoint %}
Upvotes: 3