django url in template with template in alpinejs

path('view/<customer_id>', views.view, name='customers_view'),

This is the right approach : href="{% url 'customers_view' customer_id='123' %}"

But I am generating this in a template loop in alpineJS :

:href="'{% url 'customers_view' %}/' + cust_id"

How do I combine python in alpineJS to bypass this ?

Reverse for 'customers_view' with no arguments not found. 1 pattern(s) tried: ['customers/view/(?P<customer_id>[^/]+)\\Z']

AlpineJS is running in the browser, not in the server.

The server is rendering the url tag.

The simplest recommendation is to not use the url tag here. Specify the literal url:
href="view/" + cust_id

Otherwise, you would need to define another url entry with a different name to render “view/” in the template and use the url tag with that alternate name.

If you really want to use it the url tag, there’s another hacky way to approach this. Even though it’s not the best, neither i would recommend doing it, for the sake of solving you can:

Hacky definition below

Define a templatetag named for example “lazyurl” that takes the view name as an argument, then resolves the view name with an placeholder value for the id, (0 for example) and returning the url without that placeholder value.

Only solution I’ve found is to put this in the javascript array that gets loaded dynamically in a django template loop. This is the result of the JavaScript array.

let customers = [
    {"id":"1","cust_name":"test 1","cust_id":"2020-08-ABCDFE-00013456","status":"3","date":"2016-03-01 00:00:00", url: "{% url 'customers_view' customer_id='2020-08-ABCDFE-00013456' %}" },
    {"id":"2","cust_name":"test 2","cust_id":"2016-06-TURNER-0001","status":"7","date":"2016-06-01 00:00:00", url: "{% url 'customers_view' customer_id='2016-06-TURNER-0001' %}"},

EDIT : How do I do something like this ?

let customers = [
    {% for customer in customers %}
    {
        "id": "{{ customer.id }}",
        ...
        url: "{% url 'customers_view' customer_id='customer.id' %}"
    },    
    {% endfor %}
];

You must create this id / url dict on your view, and then you can use the json_script template tag.