将会话数据传递到 URL
我在会话中设置了一些信息,我想知道是否可以将此信息传递到使用此会话数据的视图的 URL 上。我希望它以这样的方式工作:如果用户从该视图为页面添加书签,则会话数据用于将变量传递到视图上。我该怎么做?
我有一个过滤器视图,所以我希望当前选择的过滤器显示在 URL 上...有点像 www.mysite.com/filter1/filter2/filter3/ 那么如果清除了 filter2,我将拥有 www.mysite.com/ filter1/filter3/
目前我的过滤器视图的 URLConf 如下所示:
(r'^filter/$', 'filter'),
(r'^filter/(?P<p>\d{2})/$', 'filter'),
I have some information that is set in the sessions, and I was wondering if it's possible to pass this info onto the URL for the view that uses this session data. I want this to be working in such a way that if the user bookmarks the page from that view, the session data is used to pass the variables onto the view. How can I do this?
I'm having a filter view so I want the currently selected filters displayed on the URL...sorta like www.mysite.com/filter1/filter2/filter3/ then if filter2 is cleared I'll have www.mysite.com/filter1/filter3/
Currently my URLConf for the filter view looks like this:
(r'^filter/
, 'filter'),
(r'^filter/(?P<p>\d{2})/
, 'filter'),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所说,在 url 上传播数据,而不是在会话中传播数据。但使用查询字符串 - 而不是路径,正如您在问题中建议的那样。
没有什么神奇的方法可以做到这一点 - 您必须手动将变量附加到所有网址。但是,您可以将 url 创建包装在函数中,以使其更易于管理。例如:
如果您使用它来构建应用程序中的所有 url,您现在可以轻松地创建一个变量“粘性”。例如。在页面顶部,您可以像这样使用它:
在页面下方,您可以使用
url()
生成 url。然后,您将获得默认值 (1) 或用户传递给页面的$_GET
的任何值。As you say, propagate the data on the url, rather than in session. But use the query-string - not the path, as you seem to suggest in your question.
There is no magic way to do this - you'll have to manually append the variables to all urls. You can however wrap the url-creation in a function, to make this more manageable. Eg.:
If you use this to build all your urls in the application, you can now easily make a variable "sticky". Eg. at the top of your page, you could use it like this:
And further down the page, you can generate urls with
url()
. You would then get either the default value (1) or whatever the user passed to the page's$_GET
.在 django 中,您不使用 $_GET,但 request.GET
可以说您的网址是 http://example.com?filter=filter1&filter=filter2&filter=filter5
您可以使用
getlist()
像这样:所以你 URL conf (
urls.py
) 看起来像这样:In django you don't use $_GET, but request.GET
lets say your url is http://example.com?filter=filter1&filter=filter2&filter=filter5
you can get the filter names in a view using
getlist()
like this:so you URL conf (
urls.py
) will look something like this: