将会话数据传递到 URL

发布于 2024-08-14 08:27:22 字数 376 浏览 3 评论 0原文

我在会话中设置了一些信息,我想知道是否可以将此信息传递到使用此会话数据的视图的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经暖 2024-08-21 08:27:22

正如您所说,在 url 上传播数据,而不是在会话中传播数据。但使用查询字符串 - 而不是路径,正如您在问题中建议的那样。

没有什么神奇的方法可以做到这一点 - 您必须手动将变量附加到所有网址。但是,您可以将 url 创建包装在函数中,以使其更易于管理。例如:

$GLOBALS['url_state'] = array();

function url($base, $params = array()) {
  global $url_state;
  $q = http_build_query(array_merge((array) $url_state, $params));
  return $q ? "$base?$q" : $base;
}

function define_url_state($name, $default = null) {
  global $url_state;
  if (isset($_GET[$name])) {
    $url_state[$name] = $_GET[$name];
  } elseif ($default !== null) {
    $url_state[$name] = "$default";
  }
}

如果您使用它来构建应用程序中的所有 url,您现在可以轻松地创建一个变量“粘性”。例如。在页面顶部,您可以像这样使用它:

define_url_state('page', 1);

在页面下方,您可以使用 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.:

$GLOBALS['url_state'] = array();

function url($base, $params = array()) {
  global $url_state;
  $q = http_build_query(array_merge((array) $url_state, $params));
  return $q ? "$base?$q" : $base;
}

function define_url_state($name, $default = null) {
  global $url_state;
  if (isset($_GET[$name])) {
    $url_state[$name] = $_GET[$name];
  } elseif ($default !== null) {
    $url_state[$name] = "$default";
  }
}

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:

define_url_state('page', 1);

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.

痴情换悲伤 2024-08-21 08:27:22

在 django 中,您不使用 $_GET,但 request.GET

可以说您的网址是 http://example.com?filter=filter1&filter=filter2&filter=filter5

您可以使用 getlist() 像这样:

def some_view(request):
    filters = request.GET.getlist('filter')

所以你 URL conf (urls.py) 看起来像这样:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    url(r'^filters/
, 'your_app.views.some_view', name='filter_view'),
)

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:

def some_view(request):
    filters = request.GET.getlist('filter')

so you URL conf (urls.py) will look something like this:

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    url(r'^filters/
, 'your_app.views.some_view', name='filter_view'),
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文