English 中文(简体)
Django: 使用 urlresolver 设置 cookie 路径
原标题:Django: use urlresolver to set cookie path

我在对齐书店上写了一个网络服务应用程序。这个应用程序可以作为我们服务器上的几个实例找到。

例如:

http://server1/testapp/
http://server2/sandboxapp/
http://server2/productionapp/

应用程序实例通过 Mod_wsgi 以 apache2 托管 。

我对所有这些实例都使用 cookie 认证, 希望每个实例的 cookie 不与其他实例重叠。 我需要设定会话_ COOKIE_ PATH 值 。

发生的情况是,我事先不知道应用程序的部署背后的 URL 。 所以我想用反向的 URL 来猜这个值 。

我的主 urls. py 看起来像这个 :

urlpatterns = patterns(  ,
    (r admin/ , include(admin.site.urls)),
     url(r ^$ , view_server_root, {}, name= server_root ),
    ),
# here I also includes urls from the various sub-apps
for app in apps:
    urlpatterns.append(url(urlprefix,  include(app.urls))

我加了这个位子,因为所有的泌尿素都装满了:

from django.core import urlresolvers
from django.conf import settings
settings.SESSION_COOKIE_PATH = urlresolvers.reverse( server_root )

不幸的是, urlresolvers. reverse(服务器_root) return / < 返回 / , 而不是预期的(例如) /testapp/ 。 如果我登录到每个实例, 我的 cookie会重叠 。

虽然后来在应用程序的使用中,我对某些视图使用django.db.models.permalink ,这让我返回一个正确的前缀URL(例如 /testappp/page/465 )。

执行 urls.py 时可能缺少上下文。

有什么想法吗?

谢谢你看书

最佳回答

https://docs.djangoproject.com/en/dev/topics/ sections/#altering- sems- sems- at- runtime" rel=“nofollow” >该在运行时间 < / a> 改变Django设置。如果你再使用Django 1. 4 或以后,您也许可以在设置中使用 < a href=> https://docs.djangoproject.com/en/dev/topics/https/#reversion-lazy" rel=“no folfollow” _ {code> reverse_lazy 并设置了 < codection> SSSION_COOKIE_PATH 。py。

SESSION_COOKIE_PATH = reverse_lazy( server_root )
问题回答

暂无回答




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签