get_absolute_url and project name path

14 views
Skip to first unread message

Donn

unread,
Aug 7, 2008, 10:03:46 AM8/7/08
to django...@googlegroups.com
Hi,
I am looking at writing a tag that will deliver my project 'name' (directory
name) so that I can get around problems.

1. At the moment, my site is on a URL like http://blah/hdl/....

2. /hdl/search/ displays result. They include a link to the flatpage that
matched:
<a href="{{ page.get_absolute_url }}">

3. This produces HTML like: <a href="/about/">

4. But, the link to the actual page is either
<a href="about/"> or <a href="/hdl/about/">

5. So, I want to preface that with something like:
<a href="{% projectname %}{{ page.get_absolute_url }}">

Is this sensible, or is there some already done method? I suspect I can
override the get_absolute_url method somehow...

\d

Rajesh Dhawan

unread,
Aug 7, 2008, 5:09:15 PM8/7/08
to Django users
Hi,

> I am looking at writing a tag that will deliver my project 'name' (directory
> name) so that I can get around problems.
>
> 1. At the moment, my site is on a URL likehttp://blah/hdl/....
>
> 2. /hdl/search/ displays result. They include a link to the flatpage that
> matched:
> <a href="{{ page.get_absolute_url }}">
>
> 3. This produces HTML like: <a href="/about/">
>
> 4. But, the link to the actual page is either
> <a href="about/"> or <a href="/hdl/about/">
>
> 5. So, I want to preface that with something like:
> <a href="{% projectname %}{{ page.get_absolute_url }}">
>
> Is this sensible, or is there some already done method? I suspect I can
> override the get_absolute_url method somehow...

If <a href="about/"> works for you then it's the same as <a href="./
about/">.

So, all you need is this:

<a href=".{{ page.get_absolute_url }}">

-Rajesh D

Steve Holden

unread,
Aug 7, 2008, 5:14:17 PM8/7/08
to django...@googlegroups.com
Unfortunately this only works at root directory level. It will fail if
the page is several levels down the URL hierarchy. For example, if that
code appeared in two different pages with URLs

http://server/thing

and

http://server/dir/thing

in the first page it would reference http://server/about but in the
second it would reference http://server/dir/about.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Rajesh Dhawan

unread,
Aug 7, 2008, 5:21:12 PM8/7/08
to Django users
>
> Unfortunately this only works at root directory level. It will fail if
> the page is several levels down the URL hierarchy.

Right...it only solves the problem if the case that the OP mentions
above is the only level at which he needs this to work.

Donn

unread,
Aug 7, 2008, 6:51:15 PM8/7/08
to django...@googlegroups.com
On Thursday, 07 August 2008 23:09:15 Rajesh Dhawan wrote:
> <a href=".{{ page.get_absolute_url }}">
Cunning. As mentioned it has problems if it's deeper down, but it's really
elegant.

Is this not a recurring theme in Django? I mean links and whatnot are stock in
trade for web apps and getting around seems to be quite hard if you want to
keep DRY and plugable.

\d

Rajesh Dhawan

unread,
Aug 7, 2008, 6:54:55 PM8/7/08
to Django users
Hi,

> Cunning. As mentioned it has problems if it's deeper down, but it's really
> elegant.
>
> Is this not a recurring theme in Django? I mean links and whatnot are stock in
> trade for web apps and getting around seems to be quite hard if you want to
> keep DRY and plugable.

Not at all. When you write your own models, you get to define your own
get_absolute_url() on it that should use absolute paths. Similarly all
URLs defined in your own urls.py files may be referenced elegantly
with the {% url %} tag[1] or the the URL reverse API[2]. That let's
you stay DRY.

Your problem above arises from the fact that you are using a contrib
app that doesn't have a good way of letting you override the flatpage
instance's url field representation with your own pattern because the
flatpage.url field is intended to represent an absolute URL path (/hdl/
about/ in your case instead of /about/).

[1] - http://www.djangoproject.com/documentation/templates/#url
[2] - http://www.djangoproject.com/documentation/url_dispatch/#utility-methods

-RD

Donn

unread,
Aug 7, 2008, 7:07:26 PM8/7/08
to django...@googlegroups.com
On Friday, 08 August 2008 00:54:55 Rajesh Dhawan wrote:
> Your problem above arises from the fact that you are using a contrib
> app that doesn't have a good way of letting you override the flatpage
> instance's url field representation with your own pattern
Okay, I was just surprised because surely web sites that use flatpages need to
link between them. Perhaps this is a case for a feature request.

Thanks for the input on this.

\d

Reply all
Reply to author
Forward
0 new messages