Hi Praveen
I personally would prefer naming the urls rather than using the
get_absolute_url.
Both can accomplish your goal, but naming the urls is a more robust
and in other ways a better solution.

If you in a week decide that the url should not be /category/... but
instead /whatever/... you would have to recode your get_absolute_url
method. However, if you use the names in urls, everything would still
work after changing the urls. The reason is that when using the name
of an url, Django will find what the absolute url will be given the
args you use, in your example the id.
In a way, you could say that when using named urls, django is both
writing and running get_absolute_url for you.

Another plus when using named urls, is that you can give your urls
name that give meaning. That will make it a lot easier to understand
what's going on in the template when you read your own code 6 months
from now. Especially if you have several views of one model.

Say you had a model for blog posts.
Then you might want to have an url for your own blogs, another one for
your friend's blogs ect. You could still use get_absolute_url, but
this time, it would be harder to make and use, as you now would need
to know what url to get. Also when reading the template it would be
hard to see which url is being displayed. Instead using tags like
{% url friends_blog args=x %}
{% url your_blog args=x %}
{% url all_blog args=x %}
would be easy to understand after not working with the code for a long
time.

-Briel

Praveen wrote:
> Hi Briel i am totally confused now.
> My senior told me to write get_absolute_url
>
> so i wrote get_absolute_url like this
> class Listing_channels(models.Model):
>     list_channel = models.CharField(max_length = 20)
>     visibility = models.BooleanField()
>
>     def __unicode__(self):
>         return self.list_channel
>
>     def get_absolute_url(self):
>       return u'/category/listing_view/%i/' % self.id
>
> and in html template i am calling
>
> {%if listing_result %}
>     {% for n in listing_result %}
>         <li> <a href="{{n.get_absolute_url}}">{{n.list_channel}}</a></
> li>
>     {% endfor %}
> {% else %}
>     <p>not available</p>
> {% endif %}
> </ul>
>
> so its working fine.
>
> so now i have two mechanism one is your as you told me to write and
> second one is
> get_absolute_url() function.
>
> Now my confusion is if we have another same class event_channel and
> event and many more classes like this then i will have to write
> get_absolute_url() for each class
> and if go with you then there i need to change only in urls.py that i
> think fine.
>
> so i should go with get_absolute_url? give me the best and solid
> reason so i could make understand to senior, and you know the senior
> behave..
>
>
> Briel wrote:
>
> > Using urls with names will solve your problem.
> >
> > Basically if you change your urls.py like this:
> >
> >     url(
> >         r'^category/listing_view/(?P<id>\w+)/$',
> >        'mysite.library.views.listing_view',
> >        name = 'name_for_this_view'
> >     ),
> >
> > What I have done is to add a name to the url for convenience I also
> > used the url() function. This name can now be used instead of the link
> > to your view. So if you were to change the site structure, when
> > changes would be made to your urlconf, django would then be able to
> > figure things out for you. In this version your new link would look
> > like this:
> >
> > <li><a href="
> >            {% url name_for_this_view n.id %}
> >         ">{{n.list_channel}}</a></li>
> >
> >
> > In the docs you can read about it at
> > url(): http://docs.djangoproject.com/en/dev/topics/http/urls/#url
> > naming: http://docs.djangoproject.com/en/dev/topics/http/urls/#id2
> >
> > Good luck.
> > -Briel
> >
> > On 8 Jan., 12:41, Praveen <praveen.python.pl...@gmail.com> wrote:
> > > Hi Malcolm i am very new bie of Django. i read through
> > > get_absolute_url but do not know how to use.
> > > What you have given the answer i tried with that and its working fine
> > > but my senior asked me what will happen if i change the site name then
> > > every where you will have to change url
> > > mysite.library.views.listing_view.
> > >
> > > so they told me to use get_absolute_url
> > >
> > > i wrote get_absolute_ul in models.py
> > > � � def get_absolute_url(self):
> > > � � � � return "/listing/%i/" % self.id
> > > and i am trying to use in my html page template but i don't know how
> > > to use
> > >
> > > <li> <a href="{{get_absolute_url}}{{n.id}}">{{n.list_channel}}</a></
> > > li>
> > >
> > > then again same problem. first time when some one click on link it
> > > works fine but second time it appends the link 
> > > likehttp://127.0.0.1:8000/category/listing_view/3/3
> > >
> > > Please give me some idea
> > >
> > > On Jan 8, 3:37�pm, Praveen <praveen.python.pl...@gmail.com> wrote:
> > >
> > > > Thank you so much Malcolm.
> > > > every one gives only the link and tell to read but your style of
> > > > solving the problem is amazing. how you explained me in a nice way
> > > > that i can never ever find in djangoproject.com.
> > > > Thanks you so much malcom
> > >
> > > > On Jan 8, 3:23�pm, Malcolm Tredinnick <malc...@pointy-stick.com>
> > > > wrote:
> > >
> > > > > I'm going to trim your code to what looks like the relevant portion of
> > > > > the HTML template, since that's where the easiest solution lies.
> > >
> > > > > On Thu, 2009-01-08 at 02:02 -0800, Praveen wrote:
> > >
> > > > > [...]
> > >
> > > > > > � � list_listing.html
> > >
> > > > > > <div id="leftpart">
> > > > > > <h3>Sight Seeings</h3>
> > > > > > <ul>
> > > > > > {%if listing_result %}
> > > > > > � � {% for n in listing_result %}
> > > > > > � � � � <li><a href="{{n.id}}">{{n.list_channel}}</a></li>
> > > > > > � � {% endfor %}
> > > > > > {% else %}
> > > > > > � � <p>not available</p>
> > > > > > {% endif %}
> > > > > > </ul>
> > > > > > </div>
> > >
> > > > > [...]
> > >
> > > > > > I am displaying Listing_channels and Listing on same page. if some 
> > > > > > one
> > > > > > click on any Listing_channels the corresponding Listing must display
> > > > > > on same page. that is why i am also sending the Listing_channels
> > > > > > object to list_listing.html page. if some one click first time on
> > > > > > Listing_channels it shows the 
> > > > > > urlhttp://127.0.0.1:8000/category/listing_view/1/
> > > > > > but second time it appends 1 at the end and the url becomes
> > > > > >http://127.0.0.1:8000/category/listing_view/1/1
> > >
> > > > > The above code fragment is putting an element in the template that 
> > > > > looks
> > > > > like
> > >
> > > > > � � � � <a href="1">...</a>
> > >
> > > > > That is a relative URL reference and will be relative to the URL of 
> > > > > the
> > > > > current page (which is .../category/listing_view/1/). In other words, 
> > > > > it
> > > > > will be appended to that URL. One solution is to change the relative
> > > > > reference to look like
> > >
> > > > > � � � � <a href="../1">...</a>
> > >
> > > > > or, in template language:
> > >
> > > > > � � � � <li><a href="../{{n.id}}">{{n.list_channel}}</a></li>
> > >
> > > > > That assumes you will only be displaying this template
> > > > > as ..../listing_view/1/ (or with a different number as the final
> > > > > component), since it will *always* remove the final component and
> > > > > replace it with the id value.
> > >
> > > > > The alternative, which is a little less fragile, is to use the "url"
> > > > > template tag to include a URL that goes all the way back to the 
> > > > > hostname
> > > > > portion. You could write
> > >
> > > > > � � � � <li><a href="
> > > > > � � � � � �{% url mysite.library.views.listing_view n.id %}
> > > > > � � � � ">{{n.list_channel}}</a></li>
> > >
> > > > > (I've put in some line breaks just to avoid unpleasant line-wrapping).
> > > > > The {% url ... %} portion will return "/category/listing_view/1/" -- 
> > > > > or
> > > > > whatever the right n.id value is -- which will always be correct.
> > >
> > > > > Have a read 
> > > > > ofhttp://docs.djangoproject.com/en/dev/ref/templates/builtins/#urlif
> > > > > you're not familiar with the URL tag.
> > >
> > > > > Regards,
> > > > > Malcolm
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to