Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
What used @models.permalink and get_absolute_url? Please an example to better understand this. An example of the documentation is not clear to me
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
enemytet  
View profile  
 More options Nov 16 2012, 10:03 am
From: enemytet <dominikan...@gmail.com>
Date: Fri, 16 Nov 2012 07:03:08 -0800 (PST)
Local: Fri, Nov 16 2012 10:03 am
Subject: What used @models.permalink and get_absolute_url? Please an example to better understand this. An example of the documentation is not clear to me

How (and why) to use @models.permalink and get_absolute_url?

Please an example to better understand this. An example of the
documentation is not clear to me


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tomas Ehrlich  
View profile  
 More options Nov 17 2012, 9:27 am
From: Tomas Ehrlich <tomas.ehrl...@gmail.com>
Date: Sat, 17 Nov 2012 15:25:52 +0100
Local: Sat, Nov 17 2012 9:25 am
Subject: Re: What used @models.permalink and get_absolute_url? Please an example to better understand this. An example of the documentation is not clear to me
Hi there,
the key idea is: *Let's have URLs defined at one and only one place.*

So, when you have urls.py like this:

urlpatterns = patterns('',
        '^blog/(?P<slug>\w+)', BlogDetail.as_view(), name='blog-detail',
        '^articles/(?P<slug>\w+)', ArticleDetail.as_view(),
                                                name='article-detail'
        '^$', HomepageView.as_view(), name='homepage')

Your urls are defined:

/ -- Homepage
/blog/<slug> -- Blog detail (eg. one entry)
/artices/<slug> -- Article detail (eg. one article)

Imagine you want to point some link to one of these pages. Hard-coded
links, like:

<!-- navigation.html -->
<a href="/">Homepage</a>
<a href="/blog/{{ blog.slug }}">{{ blog.title }}</a>
<a href="/articles/{{ article.slug }}">{{ article.title }}</a>

are bad. Because now are your urls defined at two places -- urls.py and
navigation.html. When you change urls.py, you have to update
navigation.html also. Right now, it's not such a problem, just two
files. Imagine that your project contains lot of templates, lot of
views and you have to change everything everytime when you change
urls.py. That's 1) boring and 2) very fragile. You'll probably spend a
huge amount of time with fixing bugs.

So, there's a solution:

Reverse url from urls.py for given parameters. Let's say you want link
to blog entry and you know the slug:

url = reverse('blog-detail', [], {'slug': slug})

So, our template could look like this:

<!-- navigation.html -->
{% load url from future %}
<a href="{% url "homepage" %}"></a>
<a href="{% url "blog-detail" slug=blog.slug %}">{{ blog.title }}</a>
<a href="{% url "article-detail" slug=article.slug %}">{{ article.title }}</a>

Now all links are "reversed" from urls.py. That's good, but again: When
you change urls.py, let's say you change blog url to:

/blog/<year>/<month>/<slug>

you'll have to change all your templates again! And that's boring.

So, another improvement: Every model instance should have it's own url
(every model instance should be identified somehow). Therefore it's
convenient to define get_absolute_url method:

def get_absolute_url(self):  # return url which points to this instance
        return reverse('blog-detail', [], {'slug': slug})

But usually we use 'reverse' function every time, so it's probably
easier to use method decorator:

@models.permalink
def get_absolute_url(self):
        return ('blog-detail', [], {'slug': slug})

and return only arguments for 'reverse' function.

Now, in you template, you can use:

<!-- navigation.html -->
{% load url from future %}
<a href="{% url "homepage" %}"></a>
<a href="{{ blog.get_absolute_url }}">{{ blog.title }}</a>
<a href="{{ article.get_absolute_url }}">{{ article.title }}</a>

and be happy ever after :)

Note: For me the documentation was clear. Could you please suggest any
improvement?

Link:
https://docs.djangoproject.com/en/1.5/ref/models/instances/#get-absol...

Cheers,
 Tom

http://www.tomas-ehrlich.cz/en/

"Půjdu kamkoliv, pokud je to kupředu." - J. London

Dne Fri, 16 Nov 2012 07:03:08 -0800 (PST)
enemytet <dominikan...@gmail.com> napsal(a):


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Victor Rocha  
View profile  
 More options Nov 17 2012, 9:42 am
From: Victor Rocha <vicoro...@gmail.com>
Date: Sat, 17 Nov 2012 06:42:35 -0800 (PST)
Local: Sat, Nov 17 2012 9:42 am
Subject: Re: What used @models.permalink and get_absolute_url? Please an example to better understand this. An example of the documentation is not clear to me

In order to use get_absolute_url, first of all you need to have named urls.
Lets imagine we have the following named url.
....
  url(r'^accounts/(P?<pk>d+)/$',
    DetailView.as_view(
      template_name='accounts/details.html',
    ), name='account_details')
....
In order to referece this url in a template you have to alternatives,
actually 3:
  1) You can hardcode the url, eg. /accounts/1
  2) Do a reverse url lookup, eg. {% url account_details pk=1%}
  3) Use the get_absolute_url method on your instance, eg.
account.get_absolute_url

As you can see, using the get_absolute_url makes for a less hardcoded url;
changin parameters in your view wouldn't mean having to go back in your
templates and updating each one of the references to the url.

In order to define a get_absolute_url in your model you need to use the
@permalink decorator.

@models.permalink
def get_absolute_url(self):
  return ('account_details', #name of the view you are reversing
      (), # a tuple of all the positional parameter your view takes
      {'pk':self.id}, # a dict of all the named arguments your view takes
    )


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »