完成了以下任务:
一、由于django不建议自定义user,对自定义user的支持很差,所以修改表结构并migrate
二、完成了登录功能
代码如下:
view.py:
def login(req):
if req.method == 'GET':
if req.user.is_anonymous():
if req.GET.get('next'):
req.session['next'] = req.GET.get('next')
return ren2res("login.html", req, {})
else:
return HttpResponseRedirect("/")
elif req.method == 'POST':
user = auth.authenticate(username=req.POST.get('username'), password=req.POST.get('password'))
if user is not None:
auth.login(req, user)
next = req.session.get('next')
if next:
return HttpResponseRedirect(next)
else:
return HttpResponseRedirect('/')
else:
return ren2res("login.html", req, {'err': "Wrong Username or Password!"+
"US:"+req.POST.get('username')+' PS:'+req.POST.get('password')})
注册功能:
代码:view.py
def register(req):
if req.method == 'GET':
if req.user.is_anonymous():
if req.GET.get('next'):
req.session['next'] = req.GET.get('next')
return ren2res('register.html', req, {})
else:
return HttpResponseRedirect('/')
elif req.method == 'POST':
username = req.POST.get("username")
email = req.POST.get('email')
result = User.objects.filter(username=username);
p = re.compile('^[0-9a-zA-Z_]+$')
if len(username) == 0 or p.match(username)==None:
return ren2res('register.html', req, {'err': "Invalid username"})
elif len(email) == 0:
return ren2res('register.html', req, {'err': "Email can't be null"})
elif len(result) != 0:
return ren2res('register.html', req, {'err': "This username has been registered! Try another"})
else:
pw1 = req.POST.get('pw1')
if pw1 == "":
return ren2res('register.html', req, {'err': "Password can't be null", 'account': account})
pw2 = req.POST.get('pw2')
if pw1 != pw2:
return ren2res('register.html', req, {'err': "Password not consistent", 'account': account})
else:
newuser = User()
newuser.username = username
newuser.email = email
newuser.set_password(pw1)
newuser.is_staff = 0
newuser.is_active = 1
newuser.is_superuser = 0
newuser.save()
newuser = auth.authenticate(username=username, password=pw1)
auth.login(req, newuser)
next = req.session.get('next')
if next:
return HttpResponseRedirect(next)
else:
return HttpResponseRedirect('/')
3、初步开发交题
四、下次之前完成交题和评测显示