David542
David542

Reputation: 110163

How to build url in model similar to template

I have the following url in my template:

<a href="{% url titles order_item.title.id order_item.id 'assets' %}
         ?expanded=chaptering">Link
</a>

However, what I need to do is build the url in my model, so I can do something like this in my template:

{{ task.get_task_url }}

How would I get {% url titles order_item.title.id order_item.id 'assets' %} (for example, "/tasks/2/4/") in a model?

Can I use redirect and capture the url value or something?

Upvotes: 0

Views: 61

Answers (1)

Peter DeGlopper
Peter DeGlopper

Reputation: 37319

This is what the reverse function does.

https://docs.djangoproject.com/en/dev/ref/urlresolvers/#django.core.urlresolvers.reverse

from django.core.urlresolvers import reverse

def get_task_url(order_item):
    return reverse('titles', args=(order_item.title.id, order_item.id, 'assets'))

Or kwargs, as appropriate.

Upvotes: 2

Related Questions