簡體   English   中英

Django2:如何與 localhost:8000 和子域 analysis.localhost:8000 共享“request.user”?

[英]Django2: How to share 'request.user' with localhost:8000 and subdomain analysis.localhost:8000?

我正在使用 Django 構建一個博客,並且我已經實現了一個分析工具,它允許我 plot 關於我的訪問者的詳細信息(國家來源、設備、瀏覽器......)。

analysis.localhost:8000localhost的子域,定義為基於 class 的視圖,帶有自定義 Mixin SuperuserAccessRequired ,如果用戶不是員工,則返回401 Unauthorized (我正在使用django-hosts處理子域,這是我第一次使用子域。)。

我的問題:

如果我登錄localhost:8000並導航到analysis.localhost:8000我得到401 響應

You are seeing this as:  AnonymousUser # generated by print(You are seeing this as: ', request.user) from SuperuserAccessRequired
Unauthorized: /
[25/Sep/2019 13:14:03] "GET / HTTP/1.1" 401 89

我謙虛的假設說localhost:8000x.localhost:8000沒有共享某些變量。

我該如何解決這個問題,我已經閱讀了 5 次 django-hosts 文檔,但我似乎找不到我所缺少的

我的代碼:

項目/根/settings.py

...
ALLOWED_HOSTS = ['localhost']
ALLOWED_HOSTS += ['analysis.localhost', 'blog.localhost']

SITE_ID = 1

INSTALLED_APPS += [
    # Django Apps

    'blog',
    'analysis',
]

INSTALLED_APPS += [
    # Django Dependencies

    'sass_processor',
    'django_hosts',
]


MIDDLEWARE = [
    'django_hosts.middleware.HostsRequestMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'analysis.middleware.TrackVisitorMiddleware',
    'django_hosts.middleware.HostsResponseMiddleware',
]

ROOT_URLCONF = 'root.urls'
ROOT_HOSTCONF = 'root.hosts'
BLOG_URLCONF = 'blog.urls'
ANALYSIS_URLCONF = 'analysis.urls'
DEFAULT_HOST = 'www'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
    'analysis': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'analysis_db.sqlite3'),
    }
}

...

項目/根/hosts.py

host_patterns = patterns('',
    host(r'localhost:8000', settings.ROOT_URLCONF, name='www'),
    host(r'analysis.localhost:8000', settings.ANALYSIS_URLCONF, name='analysis'),
    host(r'blog.localhost:8000', settings.BLOG_URLCONF, name='blog'),
)

自定義混音:

class SuperuserAccessRequired(AccessMixin):
    """Verify that the current user is staff."""
    def dispatch(self, request, *args, **kwargs):
        print('You are seeing this as: ', request.user)
        if not request.user.is_staff:
            return HttpResponse(status=401, content='You are not authorized to view this page.')
        return super().dispatch(request, *args, **kwargs)

更新

如果我從analysis.localhost中刪除端口號,我會被重定向到 Apache 默認頁面......很奇怪。 (只是忽略這個更新,我忘記了 /etc/hosts,伙計,我正在失去它)

更新 2

深入研究這件事,看來我必須重寫我的 SessionMiddleware。

任何幫助/指導將不勝感激。

您需要為子域之間的共享 cookie 設置 SESSION_COOKIE_DOMAIN。

SESSION_COOKIE_DOMAIN = '.localhost'
SESSION_COOKIE_NAME = 'sharesession'

查看更多信息SESSION_COOKIE_DOMAIN

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM