如何在Django中的url中找到一个锚点? [英] How to identify an anchor in a url in Django?

查看:220
本文介绍了如何在Django中的url中找到一个锚点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做幻灯片,每张幻灯片都有这样的网址格式: articles / 1234#slide = 5 。我想从我的url.py文件中的url中读取 slide = 5 部分,然后将其传递给相应的视图函数,最后将其传递给模板并呈现右滑块网址设置如下:

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

但是似乎它不能从url获取 current_slide 变量。我想这与锚部分有关,因为它不会传输到服务器。但是,如果我忽略了我的URL设置中的锚点部分,并使用javascript来处理这个标签,似乎每次我在浏览器中输入url,它首先渲染没有锚点的页面,然后跳转到我想要的右侧幻灯片。它不能直接渲染正确的幻灯片。我如何解决这个问题?

解决方案

URL的锚点部分不会传递给服务器,它只用于客户端。为什么不使用标准参数:

  articles / 1234?slide = 5 
/ pre>

由于您被卡住了此网址格式,您可能需要使用某种动画滚动,这样可能会使此烦恼更加令人厌烦,检查此问题的答案 jquery平滑滚动到锚点


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?

解决方案

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?

这篇关于如何在Django中的url中找到一个锚点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆