2017-01-05 6 views
1

私のDjangoプロジェクトは、次のディレクトリ構造を持つ単一のウェブサイトである:ジャンゴ1.10認証

project 
-- main_folder 
    -- settings.py 
    -- views.py 
    -- urls.py [1] 
    -- ... 
-- app_folder 
    -- views.py 
    -- urls.py [2] 
    -- ... 
-- not_app_folder 
    -- views.py 
    -- urls.py [3] 
    -- ... 
-- manage.py 

は、私は、ユーザーを認証するためにurls.py [1]にこのコードを使用します。

from django.contrib.auth import views as auth_views 
... 
urlpatterns = [ 
    url(r'^login/$', auth_views.login), 
    ... 
] 

I Django 1.9(テンプレートメソッド{% if user.is_authenticated %}はすべてのページにtrueを返します)というWebサイトのすべてのページで正常に機能したコードがあります。アップグレードの認証後

urls.py [1]ファイルからすべてのURLでうまく動作しますが、私はurls.py [2]に言及したページに移動するときfalseを返す{% if user.is_authenticated %}urls.py [3](それは、単純なディレクトリである)、テンプレートメソッド(私が持っている(これは、アプリケーションディレクトリです)すべてのページで同じテンプレート)。

Django 1.10の変更点と、Webサイトのどのページでも認証を維持する方法を教えてください。

答えて

0

Djangoをアップグレードするときは非常に注意が必要です。多くの関数は非難されますが、期待どおりではなく動作しています。

このコードは、Djangoの1.9にうまく働いた:

vars = RequestContext(request, {'key': 'value'}) 
return render_to_response('template.html', vars) 

しかしrender_to_responseはすぐに廃止される予定とDjango 1.10のためにあなたが書く必要があります:

return render(request, 'template.html', {'key': 'value'}) 

どのように多くのアプリケーションやviews.pyに差はなかったですあなたが持っているファイル。ユーザー認証が正しく機能するようになりました。

-1

私はhttps://djangosnippets.org/snippets/2845/で見つけたこのミドルウェアを使用します。 それも、ただの設定でごMIDDLEWARE_CLASSESdjango.contrib.auth.middleware.*AuthenticationMiddleware後にそれを置くURLの正規表現のホワイトリスト(LOGIN_EXEMPT_URLS

# -*- coding: UTF-8 -*- 

# django dependencies 
from django.contrib.auth.views import redirect_to_login 
from django.contrib.auth import REDIRECT_FIELD_NAME 
from django.conf import settings 

# python dependencies 
from re import compile 

#---# 

EXEMPT_URLS = [compile(settings.LOGIN_URL.lstrip('/'))] 
if hasattr(settings, 'LOGIN_EXEMPT_URLS'): 
    EXEMPT_URLS += [compile(expr) for expr in settings.LOGIN_EXEMPT_URLS] 

#---# 

class LoginRequiredMiddleware: 
    """ 
    Middleware that requires a user to be authenticated to view any page other 
    than LOGIN_URL. Exemptions to this requirement can optionally be specified 
    in settings via a list of regular expressions in LOGIN_EXEMPT_URLS (which 
    you can copy from your urls.py). 

    Requires authentication middleware and template context processors to be 
    loaded. You'll get an error if they aren't. 
    """ 
    def process_request(self, request): 
     assert hasattr(request, 'user'), ("The Login Required middleware " 
      "requires authentication middleware to be installed. Edit "  
      "your MIDDLEWARE_CLASSES setting to insert " 
      "'django.contrib.auth.middlware.AuthenticationMiddleware'. " 
      "If that doesn't work, ensure your " 
      "TEMPLATE_CONTEXT_PROCESSORS setting includes " 
      "'django.core.context_processors.auth'.") 
     if not request.user.is_authenticated(): 
      path = request.path_info.lstrip('/') 
      if not any(m.match(path) for m in EXEMPT_URLS):    
       path = request.get_full_path() 
       return redirect_to_login(path, settings.LOGIN_URL, 
             REDIRECT_FIELD_NAME) 

を持っています。あなたがそこにそれを持っていないなら、あなたはそれを加えなければなりません。

MIDDLEWARE_CLASSES = (
    'django.middleware.security.SecurityMiddleware', 
    ... 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
    'sis_tools.middleware.LoginRequiredMiddleware', # <-- HERE 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
    'django.middleware.cache.FetchFromCacheMiddleware', 
) 

ホワイトリストは、あなたがこのようにそれを使用することができますurls.pyのように動作します:

LOGIN_EXEMPT_URLS = (r'^about.html$', r'^legal/',) 

これはあなたがまたsample.com/legal/*


セクションのページsample.com/about.htmlを訪問するユーザーと、すべてを可能にログインページにLOGIN_URLの設定を設定する必要があります。

LOGIN_PAGE = "/accounts/login" 

LOGIN_REDIRECT_URL URLを設定すると便利です。ユーザーがログインページにサイトを入力するとジャンプする場所です。

LOGIN_REDIRECT_URL = "/"