2

I'm doing a slideshow and each slide has a url format like this: articles/1234#slide=5. I want to retrive the slide=5 part from the url in my url.py file and then pass it to the corresponding view function and finally, pass it to the template and render the right slide. The url settings is as follows:

url(r'^(?P<article_id>\d+)#slide=(?P<current_slide>\d{1,2})$', 'articles.views.show_article')

But it seems that it cannot get the current_slide variable from the url. I guess it has something to do with the anchor part cause it's not transferred to the server. But if I ignore the anchor part in my url settings and use javascript to handle this hashtag, it seems that everytime I enter the url in browser, it first renders the page without the anchor part and then jumps to the right slide I want. It cannot render the right slide directly. How could I fix this?

Joe
  • 46,419
  • 33
  • 155
  • 245
chaonextdoor
  • 5,019
  • 15
  • 44
  • 61

3 Answers3

5

You hit on it in your question. The anchor part of the URL is not passed to the server, it is only used client side. Why not use standard get parameters:

articles/1234?slide=5

Since you are stuck with this url format, you might want to use a animated scroll of some kind which might make this less annoying, checkout the answers to this question jquery smooth scroll to an anchor?

Community
  • 1
  • 1
Rob Osborne
  • 4,897
  • 4
  • 32
  • 43
  • I'm not doing it from scratch, I'm working on someone else's project, it already has some js file associated with this hashtag thing, So I'd rather stick with this hashtag format and find some way around it. Is there any way to do achieve this with hashtag? – chaonextdoor Feb 14 '13 at 22:22
  • You should be aware that 'hashtag' is the wrong word for this. I have answered your question (see below). – Joe Feb 14 '13 at 22:29
3

No browser sends the "hashtag" to the server.

one of the common ways around this is to have a javascript capture the hashtag on load/read , and call a function to initialize the page (via ajax).

Also, the common term for this is "hashbang url". If you search on that term, you'll find a lot more relevant information.

The page is jumping around, because # is used to point to page anchors in the HTML specification. http://example.com/gallery/1234#slide5 tells the browser to go to the slide5 anchor on http://example.com/gallery/1234

Jonathan Vanasco
  • 15,111
  • 10
  • 48
  • 72
  • How to read if the Server sends a response with a hashtag to the client ? without using javascript ? – not 0x12 Sep 13 '16 at 03:57
0

The anchor (bit after the #) isn't part of the bit of the URL that's matched. They are used for an anchor (i.e. link) within a page. They are generally for the benefit of the browser (so the browser can scroll to that bit of the page) not the server but people seem to be abusing them these days. It's not surprising that Django doesn't pick them up because they are not considered a useful part of the URL for the server.

Here is documentation from an older spec of HTML but it is still valid: http://www.w3.org/TR/html4/struct/links.html#h-12.2.3

How to do it:

If you want to get this in your view, look in the Request object. Look in the request.path_info or request.path. That will give you the full URL. You can use a regular expression to extract it from there.

import re

input = "articles/1234#slide=5"

m = re.search("#slide=([0-9]*)", input)
try:
    print int(m.group(1))
except ValueError:
    print "didn't get a number"

As Rob said, you should use a get parameter for this.

Joe
  • 46,419
  • 33
  • 155
  • 245
  • 2
    It's not available in the request object at all, because it usually isn't sent to the server. – Daniel Roseman Feb 14 '13 at 22:32
  • I don't have an environment to check, but if you visit a given URL (possibly including an anchor fragment) the full URL gets passed to the server. Can't check Django, but I would be very surprised. – Joe Feb 14 '13 at 22:35
  • @DanielRoseman You are right. I just made a test and cannot see the anchor part in the request object. – chaonextdoor Feb 14 '13 at 22:45
  • Try the answer to this: http://stackoverflow.com/questions/3367194/configure-django-urls-py-to-keep-anchors-in-url-after-it-rewrites-it-with-a-end – Joe Feb 14 '13 at 22:48