def creat_group(request): if request.method == "POST": form = CreatGroupForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] introduction = form.cleaned_data['introduction'] grouptype = form.cleaned_data['grouptype'] group = Group(name=name, introduction=introduction, grouptype=grouptype, logo=STATIC_URL + 'img/face.png') url_number = len(Group.objects) + 1 group.url_number = url_number group.birthday = datetime.datetime.now() if request.FILES: path = 'img/group/' + str(url_number) if not os.path.exists(MEDIA_ROOT + path): os.makedirs(MEDIA_ROOT + path) img = Image.open(request.FILES['logo']) if img.mode == 'RGB': filename = 'logo.jpg' filename_thumbnail = 'thumbnail.jpg' elif img.mode == 'P': filename = 'logo.png' filename_thumbnail = 'thumbnail.png' filepath = '%s/%s' % (path, filename) filepath_thumbnail = '%s/%s' % (path, filename_thumbnail) # 获得图像的宽度和高度 width, height = img.size # 计算宽高 ratio = 1.0 * height / width # 计算新的高度 new_height = int(288 * ratio) new_size = (288, new_height) # 缩放图像 if new_height >= 288: thumbnail_size = (0,0,288,288) else: thumbnail_size = (0,0,new_height,new_height) out = img.resize(new_size, Image.ANTIALIAS) thumbnail = out.crop(thumbnail_size) thumbnail.save(MEDIA_ROOT + filepath_thumbnail) group.thumbnail = MEDIA_URL + filepath_thumbnail out.save(MEDIA_ROOT + filepath) group.logo = MEDIA_URL + filepath group.save() sgcard = S_G_Card(user=request.user, group=group, is_active=True, is_admin=True,creat_time=datetime.datetime.now()) sgcard.save() return HttpResponseRedirect('/group/' + str(url_number) + '/') else: form = CreatGroupForm() return render_to_response('group/creat_group.html', {'form':form, 'STATIC_URL':STATIC_URL, 'current_user':request.user}, context_instance=RequestContext(request))
def creat_group(request): if request.method == "POST": form = CreatGroupForm(request.POST) if form.is_valid(): name = form.cleaned_data["name"] introduction = form.cleaned_data["introduction"] grouptype = form.cleaned_data["grouptype"] group = Group(name=name, introduction=introduction, grouptype=grouptype, logo=STATIC_URL + "img/face.png") url_number = len(Group.objects) + 1 group.url_number = url_number group.birthday = datetime.datetime.now() if request.FILES: path = "img/group/" + str(url_number) if not os.path.exists(MEDIA_ROOT + path): os.makedirs(MEDIA_ROOT + path) img = Image.open(request.FILES["logo"]) if img.mode == "RGB": filename = "logo.jpg" elif img.mode == "P": filename = "logo.png" filepath = "%s/%s" % (path, filename) # 获得图像的宽度和高度 width, height = img.size # 计算宽高 ratio = 1.0 * height / width # 计算新的高度 new_height = int(260 * ratio) new_size = (260, new_height) # 缩放图像 out = img.resize(new_size, Image.ANTIALIAS) out.save(MEDIA_ROOT + filepath) group.logo = MEDIA_URL + filepath group.save() sgcard = S_G_Card( user=request.user, group=group, is_active=True, is_admin=True, creat_time=datetime.datetime.now() ) sgcard.save() return HttpResponseRedirect("/group/" + str(url_number) + "/") else: form = CreatGroupForm() return render_to_response( "group/creat_group.html", {"form": form, "STATIC_URL": STATIC_URL, "current_user": request.user}, context_instance=RequestContext(request), )
def creat_group(request): if request.method == "POST": form = CreatGroupForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] introduction = form.cleaned_data['introduction'] grouptype = form.cleaned_data['grouptype'] group = Group(name=name, introduction=introduction, grouptype=grouptype, logo=STATIC_URL + 'img/face.png', thumbnail=STATIC_URL + 'img/face.png') url_number = len(Group.objects) + 1 group.url_number = url_number group.birthday = datetime.datetime.now() if request.FILES: path = 'img/group/' + str(url_number) if not os.path.exists(MEDIA_ROOT + path): os.makedirs(MEDIA_ROOT + path) img = Image.open(request.FILES['logo']) if img.mode == 'RGB': filename = 'logo.jpg' filename_thumbnail = 'thumbnail.jpg' elif img.mode == 'P': filename = 'logo.png' filename_thumbnail = 'thumbnail.png' filepath = '%s/%s' % (path, filename) filepath_thumbnail = '%s/%s' % (path, filename_thumbnail) # 获得图像的宽度和高度 width, height = img.size # 计算宽高 ratio = 1.0 * height / width # 计算新的高度 new_height = int(288 * ratio) new_size = (288, new_height) # 缩放图像 if new_height >= 288: thumbnail_size = (0, 0, 288, 288) else: thumbnail_size = (0, 0, new_height, new_height) out = img.resize(new_size, Image.ANTIALIAS) thumbnail = out.crop(thumbnail_size) thumbnail.save(MEDIA_ROOT + filepath_thumbnail) group.thumbnail = MEDIA_URL + filepath_thumbnail out.save(MEDIA_ROOT + filepath) group.logo = MEDIA_URL + filepath group.save() sgcard = S_G_Card(user=request.user, group=group, is_active=True, is_admin=True, creat_time=datetime.datetime.now()) sgcard.save() return HttpResponseRedirect('/group/' + str(url_number) + '/') else: form = CreatGroupForm() return render_to_response('group/creat_group.html', { 'form': form, 'STATIC_URL': STATIC_URL, 'current_user': request.user }, context_instance=RequestContext(request))