Python:如何;叉子;在django举行的会议

Python:如何;叉子;在django举行的会议,python,django,session,authentication,login,Python,Django,Session,Authentication,Login,我有一个服务器,可为多个应用程序提供数据 请想象一下,我是其中两个或更多应用程序的注册用户,在访问每个应用程序时,我使用一个不同的登录信息 现在,作为那个用户,我使用相同的浏览器和不同的选项卡来访问这些应用程序。。。 第一次(对于第一个应用程序)记录时,一切都按预期进行,但当我访问第二个应用程序(作为第二个用户)时,该请求将访问相同的request.session对象。当我调用登录(从auth框架)时,当前用户将与实际的请求.session(请求.session[session_KEY]!=us

我有一个服务器,可为多个应用程序提供数据

请想象一下,我是其中两个或更多应用程序的注册用户,在访问每个应用程序时,我使用一个不同的登录信息

现在,作为那个用户,我使用相同的浏览器和不同的选项卡来访问这些应用程序。。。 第一次(对于第一个应用程序)记录时,一切都按预期进行,但当我访问第二个应用程序(作为第二个用户)时,该请求将访问相同的
request.session
对象。当我调用登录(从auth框架)时,当前用户将与实际的
请求.session
请求.session[session_KEY]!=user.id
)中的用户进行比较,并调用
请求.session.flush()

这意味着我将丢失访问第一个应用程序的用户的所有
请求.session
内容,并且该用户
请求.session
将被“标记”为从该点开始的第二个用户
请求.session

在这种情况下,我想要的是有一个函数/方法,允许为第二个用户创建一个新的
request.session
,保持原来的状态

在第一个答案后编辑: 首先,谢谢你的回答。 我试图不太详细,以避免过于导向的答案,但现在我认为我应该这样做:

好的,我以前调用过“it”应用程序,但实际上,我的项目服务于请求,以便提供相同的最终“产品”(例如,一个游戏)。 (我的项目中有几个django应用程序。每个应用程序都有特定的方向和后端,具体取决于应用的业务考虑。)

如果我告诉你我有不同的入口点URL,使用正确的业务后端来处理请求和检索游戏,那会更详细

我的主URL是相同的(命名空间),我的项目只有一个设置文件


如果我正确理解您的问题,那么问题在于您在应用程序之间共享会话,即使用户不同。您应该能够通过在settings.py中设置SESSION\u COOKIE\u DOMAIN、SESSION\u COOKIE\u PATH或SESSION\u COOKIE\u NAME来解决此问题,以确保您的应用程序不会相互碰撞会话。

根据您是否准备好更改“用例”,您的问题可能有几个答案:

a)您无法更改用例:这是不可能的,因为一个Django会话绑定到一个浏览器会话,无论是多个浏览器窗口实例还是选项卡

b)您可以更改用例:用户仍然可以使用多个浏览器(或配置文件(或chrome/chrome中的私有浏览模式))实现这一点,而无需对代码进行任何修改

c)您可以在网站中实现“用户”切换功能,允许用户在不同窗口的同一会话中拥有多个活动配置文件,它的用途类似于github组织切换功能或facebook页面/组织切换,但您可以在多个选项卡中拥有多个用户配置文件,而github或facebook上则不是这样

要实现c)您需要将一个“子文件”模型实例附加到您的“用户”模型,并根据查询字符串参数在每个传入请求上激活正确的子文件,并跨请求保留子文件信息

1)我想您已经有了类似的
子文件
模型,该模型的外键为
django.contrib.auth.models.User
,您可能还有一个允许用户更改其子文件的视图。为了使子文件切换工作,它需要在当前选项卡会话中持久化它正在使用的子文件中的信息,为此它需要在查询字符串中添加一个参数,因为它是选项卡的唯一绑定位置,而不是用户会话。例如“subfile=123”。您需要使用表单等正确验证子文件,视图如下所示:

def select_subprofile(request):
   if request.method == 'POST':
      form = SubProfileSelectForm(request)
      if form.is_valid():   
          subprofile = form.cleaned_data['subprofile']
          url = '%s?subprofile' % (reverse('homepage'), subprofile) 
          return redirect(url)  # the redirect is something like '/homepage?subprofile=123'
   else:
      form = SubProfileSelectForm()
   return render(request, 'myapp/subprofile_select.html', {'form':form})
class SubProfileMiddleware(object):

    def process_request(self, request):
        subprofile = request.GET.get('subprofile', None)
        if subprofile:
            # it's important to check for user here or malicious users will be
            # able to use Subprofiles of other users
            subprofile = Subprofile.objects.filter(user=request.user, id=subprofile)
            # This can also be written 
            # subprofile = request.user.subprofile_set.filter(id=subprofile)
            if not subprofile:
                # this is a malicious user
                raise Http403
            else:
                request.user.subprofile = subprofile
        else:
             # set default subprofile
             request.user.subprofile = self.user.default_subprofile
def organization_wall_post(request, organization):
    organization = Organization.objects.get_object_or_404(organization)
    if request.method == 'POST':
        form = MessageForm(request.POST)
        if form.is_valid():
             post_on_organization_wall(request.user.subprofile, message, organisation)
    else:
        form = MessageForm()
    return render(request, 'organisation/wall_post.html', {'form': form})
此视图可以是每个游戏的第一页

2)之后,您需要为当前选项卡检索用户的子文件。 对于这个问题,我们将使用a(查找,如果您不知道它是什么)中的查询字符串来将当前
子文件
实例附加到
请求。用户
。中间件将针对每个传入请求,将查询字符串中找到的当前子文件信息对应的子文件实例附加到当前用户对象,中间件如下所示:

def select_subprofile(request):
   if request.method == 'POST':
      form = SubProfileSelectForm(request)
      if form.is_valid():   
          subprofile = form.cleaned_data['subprofile']
          url = '%s?subprofile' % (reverse('homepage'), subprofile) 
          return redirect(url)  # the redirect is something like '/homepage?subprofile=123'
   else:
      form = SubProfileSelectForm()
   return render(request, 'myapp/subprofile_select.html', {'form':form})
class SubProfileMiddleware(object):

    def process_request(self, request):
        subprofile = request.GET.get('subprofile', None)
        if subprofile:
            # it's important to check for user here or malicious users will be
            # able to use Subprofiles of other users
            subprofile = Subprofile.objects.filter(user=request.user, id=subprofile)
            # This can also be written 
            # subprofile = request.user.subprofile_set.filter(id=subprofile)
            if not subprofile:
                # this is a malicious user
                raise Http403
            else:
                request.user.subprofile = subprofile
        else:
             # set default subprofile
             request.user.subprofile = self.user.default_subprofile
def organization_wall_post(request, organization):
    organization = Organization.objects.get_object_or_404(organization)
    if request.method == 'POST':
        form = MessageForm(request.POST)
        if form.is_valid():
             post_on_organization_wall(request.user.subprofile, message, organisation)
    else:
        form = MessageForm()
    return render(request, 'organisation/wall_post.html', {'form': form})
这样,您就可以在应用程序的每个视图中访问
request.user的
subfile
属性上的
subfile
实例。如果存在有效的查询字符串
subfile=123
,则用户将激活这些子文件,否则为默认子文件

假设您的应用程序是一个具有
组织的应用程序
模型,每个实例都有墙,用户可以使用子文件在其上发布消息,在墙上发布消息的函数具有以下签名
在组织上发布(子文件,消息,组织)
,使用此功能的视图如下所示:

def select_subprofile(request):
   if request.method == 'POST':
      form = SubProfileSelectForm(request)
      if form.is_valid():   
          subprofile = form.cleaned_data['subprofile']
          url = '%s?subprofile' % (reverse('homepage'), subprofile) 
          return redirect(url)  # the redirect is something like '/homepage?subprofile=123'
   else:
      form = SubProfileSelectForm()
   return render(request, 'myapp/subprofile_select.html', {'form':form})
class SubProfileMiddleware(object):

    def process_request(self, request):
        subprofile = request.GET.get('subprofile', None)
        if subprofile:
            # it's important to check for user here or malicious users will be
            # able to use Subprofiles of other users
            subprofile = Subprofile.objects.filter(user=request.user, id=subprofile)
            # This can also be written 
            # subprofile = request.user.subprofile_set.filter(id=subprofile)
            if not subprofile:
                # this is a malicious user
                raise Http403
            else:
                request.user.subprofile = subprofile
        else:
             # set default subprofile
             request.user.subprofile = self.user.default_subprofile
def organization_wall_post(request, organization):
    organization = Organization.objects.get_object_or_404(organization)
    if request.method == 'POST':
        form = MessageForm(request.POST)
        if form.is_valid():
             post_on_organization_wall(request.user.subprofile, message, organisation)
    else:
        form = MessageForm()
    return render(request, 'organisation/wall_post.html', {'form': form})

3)现在您需要跨请求持久化子文件信息。最简单的方法是将everycall to
{%url%}
替换为您自己的
url
模板标记,该标记检查子文件键的请求查询字符串是否存在,并将其添加到请求的url中。您可以重复使用。

我不确定Django如何为未完成请求的域推送cookie,或者www.2.com/playCDS--重定向到-->www.me.com/playCDS,然后您根据引用人更改用户配置文件,是吗?我仍然发布了我的答案,我会相应地更新,但是基本的