# django url in template with template in alpinejs

**URL:** <https://forum.djangoproject.com/t/django-url-in-template-with-template-in-alpinejs/19559>\
**Category:** Templates & Frontend\
**Created:** [March 16, 2023, 12:20pm UTC](https://forum.djangoproject.com/t/django-url-in-template-with-template-in-alpinejs/19559 "2023-03-16T12:20:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [March 16, 2023, 12:20pm UTC](https://forum.djangoproject.com/t/django-url-in-template-with-template-in-alpinejs/19559/1 "2023-03-16T12:20:25Z")

</div>

```auto
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 :

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

```

How do I combine python in alpineJS to bypass this ?

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

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 16, 2023, 12:43pm UTC](https://forum.djangoproject.com/t/django-url-in-template-with-template-in-alpinejs/19559/2 "2023-03-16T12:43:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [March 16, 2023, 12:52pm UTC](https://forum.djangoproject.com/t/django-url-in-template-with-template-in-alpinejs/19559/3 "2023-03-16T12:52:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anjanesh](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/anjanesh/32/4583_2.png) [@anjanesh](https://forum.djangoproject.com/u/anjanesh)\
**Post date:** [March 28, 2023, 3:50am UTC](https://forum.djangoproject.com/t/django-url-in-template-with-template-in-alpinejs/19559/4 "2023-03-28T03:50:56Z")

</div>

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.

```auto
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 ?

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

```

---

<div class="post-metadata">

**Author:** ![leandrodesouzadev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/leandrodesouzadev/32/8981_2.png) [@leandrodesouzadev](https://forum.djangoproject.com/u/leandrodesouzadev)\
**Post date:** [March 28, 2023, 11:09am UTC](https://forum.djangoproject.com/t/django-url-in-template-with-template-in-alpinejs/19559/5 "2023-03-28T11:09:03Z")

</div>

You must create this id / url dict on your view, and then you can use the `json_script` [template tag](https://docs.djangoproject.com/en/4.1/ref/templates/builtins/#json-script).
