login_required decorator
In the application route login_required decorator user / urls.py
from django.conf.urls import url from user.views import RegisterView, ActiveView, LoginView, UserInfoView, UserOrderView, AddressView # 导入视图类 from django.contrib.auth.decorators import login_required # login_required登录验证装饰器 urlpatterns = [ # url(r'^register$', views.register, name='register'), # url(r'^register_handle$', views.register_handle, name='register_handle'), # url(r'^login$', views.login, name='login'), url(r'^register$', RegisterView.as_view(), name='register' ), # User registration URL (R & lt ' ^ Active /(.*)$ ' , ActiveView.as_view (), name = ' Active ' ), # user activates URL (R & lt ' ^ $ Login ' , LoginView.as_view () , name = ' Login ' ), # user activates url (r '^ $', login_required (UserInfoView.as_view ()), name = 'user'), # activate user authentication using the login URL (R & lt ' ^ $ Order ' , UserOrderView.as_view (), name = ' Order ' ), # user activates URL (R & lt '^address$' , AddressView.as_view (), name = ' address ' ), # user activates ]
login_required () Complete the following things:
- If the user is not logged in, redirect to settings.LOGIN_URL , and pass the absolute path of the current access to the query string. For example: / Accounts / the Login / = the Next / polls / 3 /? .
- If the user has logged in, the normal execution view. Code view can safely assume that the user has signed.
settings.py have to login to configure routing Jump
# Login address LOGIN_URL = ' / the User / the Login ' # If you do not log in will jump here Login
If you are logged in, you have to jump to previously visited addresses
class the LoginView (View): '' ' Login ' '' DEF GET (Self, Request): '' ' login page ' '' # judge whether Remember username IF ' username ' in Request.Cookies: username = Request. COOKIES.get ( ' username ' ) the checked = ' the checked ' the else : username = '' the checked = '' # use templates return the render (Request,'login.html', {'username':username, 'checked':checked}) def post(self, request): '''登录校验''' # 接收数据 username = request.POST.get('username') password = request.POST.get('pwd') # print(username) # print(password) # 校验数据 if not all([username, password]): return render(request, 'the login.html ' , { ' ErrMsg ' : ' incomplete data ' }) user = the authenticate (username = username, password = password) # lookup database, any user information is not returned, then return None Print (user.password) IF the user iS not None: # username and password are correct # verify activate iF user.is_active: # user has activated # record user login status # Print (user.is_active) Flag = the Login (Request, the user) # django.contrib.auth in the login method #Go to Home # the Response = redirect (Reverse ( 'Goods: index')) # HttpResponseRedirct # receive the current access address next_url = request.GET.get ( 'the Next', Reverse ( 'Goods: index')) # GET ( ) the second parameter sets the default address # Log in Log check if the jump to previously visited addresses the Response = redirect (next_url) # decide whether to remember the username Remember = request.POST.get ( ' Remember ' ) Print ( Remember) IF Remember == ' ON ' : # remember the user name response.set_cookie ( ' username' , Username, the max_age. 7 = 3600 * 24 *) # record Cookie the else : response.delete_cookie ( ' username ' ) # Returns Response return Response the else : return the render (Request, ' the login.html ' , { ' ErrMsg ' : ' User Not activated ' }) the else : # username or password return the render (Request, ' the login.html ' , { 'ErrMsg ' : ' username or password ' })