Search code examples
pythondjangopermalinks

How does a permalink work?


I have the following code in my models.py file and I'm wondering how it's used in the urls.py/rest of the django framework. What benefits does it give me?

    @models.permalink
    def get_absolute_url(self):
        return('catalog_category',(), {'category_slug':self.slug})

How do I use this method?


Solution

  • This can be used to return a url with your current object. If you have a model and url associated with your @models.permalink.

    An example may be something like this {% for offer in offers %} {{offer.title}} {{offer.get_absolute_url}}

    In the urls.py file look at your related_names there should be one in related to .get_absolute_url function.

    However, if you have return multiple urls using the same models, but are on different views .get_absolute_url can still be used. just in a different syntax such as {% url url_related_name id=id %} id would be in your instance you'd want slug=offer.slug