2017-01-05 2 views
1

내 장고 프로젝트는 다음 디렉토리 구조를 가지고 하나의 웹 사이트입니다 Django 1.9 (모든 페이지에서 {% if user.is_authenticated %}true을 반환 함)으로 모든 웹 사이트 페이지에서 성공적으로 작업 한 코드를 가지고 있어야합니다. 업그레이드 인증 후장고 1.10 인증

urls.py [1] 파일에서 모든 URL에서 잘 작동하지만, 내가 urls.py [2]에 언급 된 페이지로 이동할 때 false을 반환 {% if user.is_authenticated %} 또는 urls.py [3] (이것은 단순한 디렉토리), 템플릿 방법 (나는이 (가 응용 프로그램 디렉토리입니다) 모든 페이지에 대해 동일한 템플리트).

Django 1.10의 변경된 사항과 웹 사이트의 모든 페이지에서 인증을 유지하는 방법은 무엇입니까?

답변

0

장고를 업그레이드 할 때 매우주의해야합니다. 많은 함수가 더 이상 사용되지 않지만 예상대로 작동하지 않습니다.

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

그러나 render_to_response 곧 더 이상 사용되지 않습니다 및 장고 1.10 당신은 작성해야 :

이 코드는 장고 1.9에서 잘 작동 얼마나 많은 응용 프로그램 또는 views.py에 차이가

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

없었다 당신이 가지고있는 파일들. 이제 사용자 인증이 올바르게 작동합니다.

-1

https://djangosnippets.org/snippets/2845/에서 찾은이 미들웨어를 사용합니다. 심지어

# -*- 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_CLASSESdjango.contrib.auth.middleware.*AuthenticationMiddleware 후 넣어 URL을 (LOGIN_EXEMPT_URLS)에 정규 표현식 화이트리스트가 있습니다. 거기에 없으면 추가해야합니다. 화이트리스트는 urls.py처럼 작동

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', 
) 

그래서 당신은 다음과 같이 사용할 수 있습니다 :

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 = "/"