Python: how to "fork out" a session in django

I have a server that transfers multiple applications.

Please imagine that I am a user registered in 2 or more of these applications, and when accessing each of them I use one other information for login.

Now, as this user, I use the same browser with different tabs to access these applications ... Registration for the first time (for the first application) goes as intended, but when I access the second application (as the second user), this the request will access the same object request.session. When I call the login (from the auth framework), the current user will be compared with the user in valid request.session( request.session[SESSION_KEY] != user.id) and request.session.flush()will be called.

This means that I will lose all the content request.sessionfor the user who accessed the 1st application, and the same user request.sessionwill be marked as the second user request.sessionfrom this point.

In this situation, I wanted to have a function / method that allows you to create a new one request.sessionfor the second user, leaving the source text as it is.

Edited after the first answer : First of all, thanks for the answer. I tried not to get too detailed in order to avoid too oriented answers, but now I think I should do this:

Well, I already called it "this" applications, but actually my project serves requests to offer the same final "product" (for example, a game). (I have several django applications inside my project. Each of them has a specific orientation and support depending on business considerations.)

It would be more detailed if I said that I have different entry point URLs, use the correct business server to process the request and receive the game.

My main url is the same (namespace) and my project has only one settings file.

enter image description here

+5
source share
2 answers

There may be several answers to your question, depending on whether you are ready to change the “use case” or not:

a) : , Django , .

b). : , ( ( /)) - .

c). "" -switch -, , gituub organization-switch facebook page/organization-switch, , github facebook.

c) SubProfile, "", SubProfile .

1) , - Subprofile django.contrib.auth.models.User, , . -, , , , , , -session. , "subprofile = 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})

.

2) . middleware ( howtos on SO middlewares Django, , ), Subprofile request.user. SubProfile, , , , :

class SubProfileMiddleware(object):

    def process_request(self, request):
        subprofile = request.GET.get('subprofile', None)
        if subprofile:
            # it 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

, Subprofile Subprofile request.user. subprofile=123, , .

, Organization, , , post_on_organization_wall(subprofile, message, organization), , :

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) . - allcall {% url %} url, URL-. Django url.

+2

, , , . , SESSION_COOKIE_DOMAIN, SESSION_COOKIE_PATH SESSION_COOKIE_NAME settings.py, , .

0

All Articles