Reputation: 26558
I am implementing a directory service and as you know listing URLs appear on a lot of places. I am aware of using the {% url %}
tag, it's still not bullet proof for cases like global listing url structure changes, say I had {% url id=listing.id %}
and had to add slug to the URL like {% url id=listing.id slug=listing.slug %}
While global search & replace is an option, however I am curious if there is a canonical way to approach this issue.
Currently my approach is have a listingurl.html
which only has {% url id=listing.id slug=listing.slug %}
and wherever needs to have the url will just include listingurl.html
, however I am not sure if implementing a custom filter would be more efficient?
Upvotes: 2
Views: 143
Reputation: 239430
The best way to handle it IMHO is to add a get_absolute_url
method to your model. Then, instead of working about reversing the URL in the template, you can just call that method:
@models.permalink
def get_absolute_url(self):
return ('listing_view_name', {'id': self.id, 'slug': self.slug})
Then:
<a href="{{ listing.get_absolute_url }}">{{ listing }}</a>
Upvotes: 1
Reputation: 2445
Not 100% sure this works and if it is the most elegant solution:
some_template.html
{% include 'listing.html' with url_thing %}
listing.html
{% url url_thing id=object.id %}
Upvotes: 2