1 from django.shortcuts import render, redirect
2 from django.http import HttpResponseRedirect
3 from django.contrib.auth.models import User
4 from django.contrib.auth import authenticate,login,logout
5 from django.contrib.auth.models import AnonymousUser
6
7 # Create your views here.
8 # 主页
9 def index(request,):
10 username = request.user
11 return render(request,'Myapp/index.html',locals())
12
13 # 登录
14 def tologin(request):
15 if request.method == 'POST' and request.POST:
16 data = request.POST
17 username = data.get('username')
18 password = data.get('password')
19 n = authenticate(username=username,password=password)
20 if n:
21 # landed successfully to get the current logged-on user, returned home
22 the Login (Request, the User = the n-)
23 return redirect ( ' / ' )
24- # fail redirected to the login page
25 return the render (Request, ' myApp / login.html ' )
26 is
27 # register
28 DEF Register (Request):
29 IF request.method == ' the POST ' and of request.POST:
30 Data = of request.POST
31 is username = data.get ( "username " )
32 password = data.get ( " password " )
33 is # checksum register, the name can not be repeated
34 is U = User.objects.filter (username = username) .first ()
35 IF U:
36 info = ' user name has been registered '
37 [ return the render (Request, ' Myapp / error.html ' , { ' info ' :} info)
38 is the else :
39 # successful registration, the user creates
40 User.objects.create_user (
41 is = username username,
42 is password = password
43 is )
44 is # redirected to a login page
45 return HttpResponseRedirect ( ' / tologin / ' )
46 is # registration fails, re-register
47 return the render (Request, ' Myapp / register.html ' )
48
49 DEF lagout (Request):
50 Zimbabwe Logout (Request)
51 is return the redirect ( ' / ' )