def register(request): if request.user.is_authenticated(): return error(u'您已经是会员了') if request.method == 'POST': form = RegisterForm(request.POST) if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password1'] email = form.cleaned_data['email'] user = User.objects.create_user(username, email, password) user.save() user.profile.nickname = form.cleaned_data['nickname'] user.profile.save() return success() return error(dict(form.errors)) else: return render(request, 'users/register.html', {'title': u'注册'})
def login(request): if request.method == 'POST': if request.user.is_authenticated(): return error(u'您已经登录了') username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username, password=password) if user: django_login(request, user) return success() else: return error(u'用户名或密码错误') else: content = { 'title': u'登录', } return render(request, 'users/login.html', content)