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 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 }))