def register(request): data = dict() data["tags"] = th.get_information(None) if request.method == 'POST': # 异步传输。。 imf = {} username = (request.POST.get('username')) password1 = (request.POST.get('password1')) password2 = (request.POST.get('password2')) user = User.objects.filter(username=username) if user: imf["flag"] = "name" return HttpResponse(json.dumps(imf), content_type='application/json') if password1 != password2: imf["flag"] = "pwd" return HttpResponse(json.dumps(imf), content_type='application/json') user = User.objects.create_user(username=username, password=password1) user.save() profile = UserProfile() profile.user_id = user.id profile.save() imf["flag"] = "yes" user = authenticate(username=username, password=password1) if user is not None: auth_login(request, user) return HttpResponse(json.dumps(imf), content_type='application/json') return render_to_response('blog_register(1).html', data, context_instance=RequestContext(request))
def regist(request): regist_info = '' if request.method == 'GET': form = RegistForm() # context, content, contents are the same(变量名不影响使用) contents = {'form': form} # thise are the same return render(request, 'blog/regist.html', contents) else: form = RegistForm(request.POST, request.FILES) if form.is_valid(): username = form.cleaned_data['username'] email = form.cleaned_data['email'] password1 = form.cleaned_data['password1'] password2 = form.cleaned_data['password2'] phone = form.cleaned_data['phone'] userimg = form.cleaned_data['userimg'] sex = form.cleaned_data['sex'] if userimg and userimg.size > USER_IMG_MAX_SIZE: logger.info(userimg.size) regist_info = 'user img too big' return render_to_response("blog/regist.html", RequestContext(request, {'form': form, 'regist_info': regist_info})) if password1 == password2: user_filter_result = User.objects.filter(email=email) if user_filter_result : regist_info = "邮箱已存在" return render_to_response("blog/regist.html", RequestContext(request, {'form': form, 'regist_info': regist_info})) else: user_profile = UserProfile() user_profile.userimg = DEFAULT_USER_IMG if userimg: imgpath = os.path.join(USER_IMG_PATH, email) with open(imgpath, 'wb') as img: img.write(userimg.read()) user_profile.userimg = email user = User.objects.create_user( username=username, password=password1, email=email) user_profile.user_id = user.id user_profile.phone = phone user_profile.sex = sex user_profile.save() regist_info = '注册成功' user = auth.authenticate( username=username, password=password1) auth.login(request, user) # 利用session传递信息给模板层 request.session['username'] = username request.session['userimg'] = os.path.join(USER_IMG_URL, user_profile.userimg) return HttpResponseRedirect('/') return render_to_response('blog/regist.html', RequestContext(request, {'form': form, 'regist_info': regist_info})) else: regist_info = "两次输入的密码不一致!" return render_to_response("blog/regist.html", RequestContext(request, {'form': form, 'regist_info': regist_info})) else: regist_info = '输入有误' return render_to_response('blog/regist.html', RequestContext(request, {'form': form, 'regist_info': regist_info}))
def regist(request): regist_info = '' if request.method == 'GET': form = RegistForm() # context, content, contents are the same(变量名不影响使用) contents = {'form': form} # thise are the same return render(request, 'blog/regist.html', contents) else: form = RegistForm(request.POST, request.FILES) if form.is_valid(): username = form.cleaned_data['username'] password1 = form.cleaned_data['password1'] password2 = form.cleaned_data['password2'] nickname = form.cleaned_data['nickname'] phone = form.cleaned_data['phone'] userimg = form.cleaned_data['userimg'] if password1 == password2: user_filter_result = User.objects.filter(username=username) nickname_filter_result = UserProfile.objects.filter( nickname=nickname) if user_filter_result or nickname_filter_result: regist_info = "邮箱或昵称已存在" return render_to_response("blog/regist.html", RequestContext(request, {'form': form, 'regist_info': regist_info})) else: user = User.objects.create_user( username=username, password=password1) # user.is_active=True # user.save user_profile = UserProfile() user_profile.user_id = user.id user_profile.phone = phone user_profile.nickname = nickname user_profile.userimg = '/media/uploads/userimg/defaultuser.png' if userimg: imgpath = os.path.join(UPLOADPATH, 'userimg', username) with open(imgpath, 'wb') as img: img.write(userimg.read()) user_profile.userimg = imgpath[(len(BASEPATH) + 5):] user_profile.save() regist_info = '注册成功' user = auth.authenticate( username=username, password=password1) auth.login(request, user) # 利用session传递信息给模板层 request.session['username'] = username request.session['userimg'] = user.userprofile.userimg return HttpResponseRedirect('/') return render_to_response('blog/regist.html', RequestContext(request, {'form': form, 'regist_info': regist_info})) else: regist_info = "两次输入的密码不一致!" return render_to_response("blog/regist.html", RequestContext(request, {'form': form, 'regist_info': regist_info})) else: regist_info = '输入有误' return render_to_response('blog/regist.html', RequestContext(request, {'form': form, 'regist_info': regist_info}))
def UpdateUserInfo(request): """ Update user info. """ try: if not request.user.is_authenticated(): return render_to_response("user/login.html", \ RequestContext(request, {"WebSiteInfo" : WebSiteInfo})) user = User.objects.filter(id=request.user.id)[0] email = request.GET.get('email', '') if len(email) > 0: if email.find("@") <= 0: return HttpResponse("邮箱地址不正确") else: user.email = email user.save() mobile_phone = request.GET.get('mobile_phone', '') position = request.GET.get('position', '') title = request.GET.get('jobTitle', '') location = request.GET.get('location', '') if len(mobile_phone) > 0 or len(position) > 0 or len(title) > 0 or len( location) > 0: profile = UserProfile.objects.filter(user=request.user.id) editProfile = None if profile is None or len(profile) <= 0: editProfile = UserProfile() else: editProfile = profile[0] editProfile.user_id = request.user.id editProfile.mobile_phone = mobile_phone editProfile.position = JobPosition.objects.filter( job_title=position)[0] editProfile.job_title = JobTitle.objects.filter(job_title=title)[0] editProfile.location = location editProfile.website_level = WebSiteLevel.objects.get(id=1) editProfile.save() return HttpResponse("1") except Exception, e: return HttpResponse(e)
def regist(request): regist_info = '' if request.method == 'GET': form = RegistForm() # context, content, contents are the same(变量名不影响使用) contents = {'form': form} # thise are the same return render(request, 'blog/regist.html', contents) else: form = RegistForm(request.POST, request.FILES) if form.is_valid(): username = form.cleaned_data['username'] email = form.cleaned_data['email'] password1 = form.cleaned_data['password1'] password2 = form.cleaned_data['password2'] phone = form.cleaned_data['phone'] userimg = form.cleaned_data['userimg'] sex = form.cleaned_data['sex'] if userimg and userimg.size > USER_IMG_MAX_SIZE: logger.info(userimg.size) regist_info = 'user img too big' return render_to_response( "blog/regist.html", RequestContext(request, { 'form': form, 'regist_info': regist_info })) if password1 == password2: user_filter_result = User.objects.filter(email=email) if user_filter_result: regist_info = "邮箱已存在" return render_to_response( "blog/regist.html", RequestContext(request, { 'form': form, 'regist_info': regist_info })) else: user_profile = UserProfile() user_profile.userimg = DEFAULT_USER_IMG if userimg: imgpath = os.path.join(USER_IMG_PATH, email) with open(imgpath, 'wb') as img: img.write(userimg.read()) user_profile.userimg = email user = User.objects.create_user(username=username, password=password1, email=email) user_profile.user_id = user.id user_profile.phone = phone user_profile.sex = sex user_profile.save() regist_info = '注册成功' user = auth.authenticate(username=username, password=password1) auth.login(request, user) # 利用session传递信息给模板层 request.session['username'] = username request.session['userimg'] = os.path.join( USER_IMG_URL, user_profile.userimg) return HttpResponseRedirect('/') return render_to_response( 'blog/regist.html', RequestContext(request, { 'form': form, 'regist_info': regist_info })) else: regist_info = "两次输入的密码不一致!" return render_to_response( "blog/regist.html", RequestContext(request, { 'form': form, 'regist_info': regist_info })) else: regist_info = '输入有误' return render_to_response( 'blog/regist.html', RequestContext(request, { 'form': form, 'regist_info': regist_info }))