Unable to access view.kwargs inside base template

I have some child templates which extends one base template. I can access {{ view.kwargs }} inside the child template but when I use it inside the base template its value is empty. I have searched it on stack overflow and web but couldn’t find a solution.

view.kwargs.xyz contains the argument part in my url

My urls.conf
path('test/<str:xyz>/', SomeViewName, name='test-xyz'),

URL: https://somedomain/test/demovalue/

Base Template
basetemplate.html

<html>
...
{{ view.kwargs.xyz }} ---> This value is empty
...
</html>

Child Template

{% extends "basetemplate.html" %}
{% block nav %}
    {{ view.kwargs.xyz }} ---> This displays the expected value ie. demovalue
{% endblock %}

I need to access view.kwargs inside base template because there is a url in base template to which i need to send a parameter from view.kwargs

<a href="{% url 'test-xyz' view.kwargs.xyz %}">link</a>

This code is not working in base template as view.kwargs is empty.

Could anyone help me how to fetch view.kwargs inside base template or is there any work around.

Thanks.

Assuming there’s not anything strange going on here in other parts of your code, the most obvious work-around is to just define that field as a block.

So, for example:

<a href="{% url 'test-xyz' view.kwargs.xyz %}">link</a>

Becomes:

<a href="{% block linkurl %}{% endblock %}">link</a>

And then you include this in your child template:

{% block linkurl %}{{ view.kwargs.xyz }}{% endblock %}

(Note: Pure conjecture on my part here as I know absolutely nothing about the Django template internals - but I might make a guess that there’s some sort of caching going on with the parent templates that allow them to be “pre-rendered” with placeholders for the child templates.)

Thank you. I was able to do it using {% block %} tag and {% include %} tag with arguments.