문제

I have two Django web apps that use the same database and modules, my issue is, I want one of the websites to act as primary site "A" (e.g. for secure authentication, as only site A will be under the https scheme, mind you "B" is under a subdomain of "A" so there's no real cross domain issues involved as I share the secret key between the settings files).

So far I have setup the Django sites framework, so my main "A" site is equipped with its own settings and urls module, as is my secondary (slave) site "B".

Now how would I go on implementing a way of proxy-ing the urls from "B" that require https, without repeating my self over and over again, mind you I use i18n patterns.

So basically I need a way for my apps to tell that the "login" url in urls_site_b should proxy to "login" in urls_site_a, then return to site B after success (using the next param for example).

I am considering a proxy-ing piece of middleware but that doesn't solve the part identifying which urls should be linked. I thought about a constant setting that holds a list of urls that should be proxied. However programming such will take some time I rather have some sort of advise before I proceed.

Also in my templates (which are shared aswell) I would use the {% url %} tag, this might be a problem as the tag wouldn't resolve the correct url as it only looks in the urls module that belongs to the current site.

도움이 되었습니까?

해결책

I think you're thinking overly complex, a simple decorator should do the trick (untested):

def proxy(view):
    @wraps(view)
    def wrapper(request, *args, **kwargs):
        current_site = get_current_site(request)
        try:
            main_site = Site.objects.get(id=1).domain  # or set this hard
        except Site.DoesNotExist:
            raise Http404
        if current_site != main_site:
            return HttpResponseRedirect('http://{0}{1}?next=http://{2}/'.format(
                main_site, request.path, current_site)
            )
        return view(request, *args, **kwargs)
    return wrapper

urls_site_b.py

urlpatterns = patterns('',
    url(r'^foo/$', proxy(bar), name='bar'),
)

Now make sure urls_site_a.py also has this pattern (obviously you wouldn't need to decorate the view there).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top