What is @permalink and get_absolute_url in Django? When and why to use it?
Please a very simple example (a real practical example). Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
@permalinkis a python decorator, whileget_absolute_urlis a method on a django model.Both are concerned with allowing you to reverse the URL for a particular object and should be used together. They are used anytime you need to provide a link to a particular object or want to display that object’s specific URL (if it has one) to the user
You could simply write your
get_absolute_urlmethod to return a hard coded string, but this wouldn’t adhere to Django’s philosophy of DRY (don’t repeat yourself). Instead, there is the@permalinkto make things more flexible.If you read the docs on the subject you will see how they relate to each other. the
@permalinkdecorator hooks into django’s URLconf’s backend, allowing you to write much more portable code by using named url patterns. This is preferable to just usingget_absolute_urlon it’s own: your code becomes much DRYer as you don’t have to specify paths.and in urls.py