<a href="#id> in django template

2,261 views
Skip to first unread message

PFL

unread,
Dec 5, 2008, 11:19:38 AM12/5/08
to Django users
How can I get Django templates to generate links to anchors that are
*local* to the document?
In a static HTML page, I have a table of contents with links to
internal sections within the same page:

<a href="#1">Section1</a>
<a href="#2">Section2</a>

These link to sections that that look like this:

<td id="1" class="section"> 111 </td>
<td id="2" class="section"> 222 </td>


Converting the above into a template I have:
{% for row in content.rows %}
<a href="#{{row.id}}">{{ row.label }}</a>


but....when the template loads it creates output that looks like:

<a href="http://localhost:8000/doc/#1">

That URL above now gets processed by URL conf...

I have tried various things but I cannot get the Django temlplate to
generate simply:
<a href="#1"> so that it will navigate to a section down the page....

thanks in advance for any advice.

-Peter

Rajesh Dhawan

unread,
Dec 5, 2008, 11:35:16 AM12/5/08
to Django users
> How can I get Django templates to generate links to anchors that are
> *local* to the document?
> In a static HTML page, I have a table of contents with links to
> internal sections within the same page:
>
> <a href="#1">Section1</a>
> <a href="#2">Section2</a>
>
> These link to sections that that look like this:
>
> <td id="1" class="section"> 111 </td>
> <td id="2" class="section"> 222 </td>
>
> Converting the above into a template I have:
> {% for row in content.rows %}
> <a href="#{{row.id}}">{{ row.label }}</a>
>
> but....when the template loads it creates output that looks like:
>
> <a href="http://localhost:8000/doc/#1">

How are you checking that it's generating that absolute URL? It is
almost certainly your web browser that's showing you the links in an
absolute form. Try to look at the source code for your page or use
curl/wget to fetch the source and you will see that your template code
is just fine.

-Rajesh D

PFL

unread,
Dec 5, 2008, 1:04:13 PM12/5/08
to Django users
The source generated by the templates is indeed correct; it looks
like:

<a href="#1">Section1</a>

However, when I click on the link, Django(?) tries to resolve it as:

http://localhost:8000/doc/#1 -- not --- http://localhost:8000/doc#1

So -- how do I get local links working with Django?


Daniel Roseman

unread,
Dec 5, 2008, 2:02:30 PM12/5/08
to Django users
On Dec 5, 6:04 pm, PFL <pe...@liske.org> wrote:
> The source generated by the templates is indeed correct; it looks
> like:
>
> <a href="#1">Section1</a>
>
> However,  when I click on the link, Django(?) tries to resolve it as:
>
> http://localhost:8000/doc/#1 -- not ---http://localhost:8000/doc#1
>
> So -- how do I get local links working with Django?

Django isn't doing anything. Have a look in the output from the
development server - I very much doubt that the click results in any
request being sent.

The address of the page is indeed /doc/, not /doc, so it's correct
that the local anchor would be /doc/#1.
-
DR.

PFL

unread,
Dec 5, 2008, 2:41:14 PM12/5/08
to Django users

Thanks for your replies --I am still stuck here.

>Have a look in the output from the development server - I very much doubt that the click results in any request being sent.

A request is definitely sent for each of these cases: "/doc/", "/
doc#", "/doc#1/". I can see each of these generating a hit on the
local dev server standard out.

I think this is a URL mapping mapping .

My url.py is:

(r'^doc/', 'doc.views.get_sections'),

Using the above, all of the following URLS map to the same
'doc.views.get_sections' view:
http://localhost:8000/doc/
http://localhost:8000/doc#1
http://localhost:8000/doc#1/

I am not sure what I need to do to either my url mappings or in my
view to handle the "#<n>" ?
What is the way to get local links working?


On Dec 5, 11:02 am, Daniel Roseman <roseman.dan...@googlemail.com>
wrote:

David Zhou

unread,
Dec 5, 2008, 2:47:44 PM12/5/08
to django...@googlegroups.com
On Fri, Dec 5, 2008 at 2:41 PM, PFL <pe...@liske.org> wrote:

> A request is definitely sent for each of these cases: "/doc/", "/
> doc#", "/doc#1/". I can see each of these generating a hit on the
> local dev server standard out.

Are you opening them in a new tab or anything of that nature?

--
---
David Zhou
da...@nodnod.net

Rajesh Dhawan

unread,
Dec 5, 2008, 2:58:20 PM12/5/08
to Django users


On Dec 5, 2:41 pm, PFL <pe...@liske.org> wrote:
> Thanks for your replies --I am still stuck here.
>
> >Have a look in the output from the development server - I very much doubt that the click results in any request being sent.
>
> A request is definitely sent for each of these cases: "/doc/", "/
> doc#", "/doc#1/". I can see each of these generating a hit on the
> local dev server standard out.

But you omitted the one that your template is actually resulting in:

/doc/#1

Incidentally, they will all generate a hit if you punch them into your
browser's URL bar. The # anchor implementation is a browser feature
not a web server feature. So, the way to test whether a new link is
being generated is to use your browser like this:

First point your browser to /doc/ to get Django to serve your request.
Now, click on the anchor link in your browser. You should not see a
new web server request resulting from that click. Your browser should
simply pan you down to where you have the element with id=1.

PFL

unread,
Dec 5, 2008, 4:49:58 PM12/5/08
to Django users
Rajesh -- THANK YOU very much! you are correct.

My issue was not on the <a> side -- it was on the target side -- i had
a typo that was messing up the text of the id="".
Once that was fixed, it worked exactly as you said it should; this was
a PIBKAC issue.

I did however learn something: I was not aware of the distinction
between browser/server functionality in resolving local links...of
course it seem obvious to me now.
Reply all
Reply to author
Forward
0 new messages