def photo_edit(request, *args, **kwargs): owner_id = kwargs.get('owner_id', None) gallery_id = kwargs.get('gallery_id', None) gallery = get_object_or_404(Gallery, pk=gallery_id) photo_id = kwargs.get('photo_id', None) if photo_id: edit = True photo = get_object_or_404(Photo, pk=photo_id) else: edit = False photo = Photo(gallery=gallery) if request.POST: form = PhotoForm(request.POST, request.FILES, instance=photo, edit=edit) if form.is_valid(): form.save() redirect_url = reverse('gallery', args=(owner_id, gallery_id)) return HttpResponseRedirect(redirect_url) else: form = PhotoForm(instance=photo, edit=edit) context = {'form': form, 'edit': edit, 'photo': photo} return render(request, 'gallery/photo_edit.html', context)
def upload_photo(request): if request.user.is_authenticated(): if request.method == "POST": logger.debug('FFFFFFFILES==%s', request.FILES) pf = PhotoForm(request.POST, request.FILES) logger.debug('pf====%s', pf) if pf.is_valid(): name = pf.cleaned_data['name'] photos = Photo.objects.filter(name=name) if photos: error = 'this photo name has already exist, please use another one' return render_to_response('gallery/upload_photo.html', {'pf': pf, 'error': error}, context_instance=RequestContext(request)) album = pf.cleaned_data['album'] fn = request.FILES['img'].name url = handle_upload_photo(request.FILES['img'], str(album), fn) thumbnail = make_thumbnail(url) # thumbnail = make_thumbnail(request.FILES['img']) #img字段不再用来存储上传的图像,而是存储生成的缩略图thumbnail photo = Photo(name=name, url=url, img=thumbnail, album=album) photo.save() return HttpResponseRedirect('/gallery/upload_photo_success/') else: pf = PhotoForm() albums = Album.objects.all() return render_to_response('gallery/upload_photo.html', {'pf': pf, 'request': request}, context_instance=RequestContext(request)) return HttpResponseRedirect('/login/')
def create(self, request): form = PhotoForm(request.POST, request.FILES) print form if form.is_valid(): print request.FILES['image'] new_photo = form.save() return new_photo else: return form.errors
def edit_photo(request, photo_id): photo = get_object_or_404(Photo, id=photo_id) form = PhotoForm(request.POST or None, instance=photo) context = { 'form': form, 'photo': photo, } if request.method == 'POST' and form.is_valid(): form.save() messages.success(request, 'Отредактировано!') return redirect('choice_photo') return render(request, 'gallery/edit_photo.tpl', context)
def add_photo(request): if request.method == "POST": form = PhotoForm(request.POST, request.FILES) if form.is_valid(): instance = form.save(commit=False) instance.save() messages.success(request, 'Фотография добавлена') return redirect('choice') else: messages.success(request, 'Фотография не добавлена') else: form = PhotoForm() context = { "form": form } return render(request, 'gallery/add_photo.tpl', context)
def photo_load(request, slug): photos = get_object_or_404(Album, slug=slug) username = auth.get_user(request).username if request.user.is_authenticated(): user = User.objects.get(username=username) if user.has_module_perms('gallery'): if request.method == 'POST': form = PhotoForm(request.POST, request.FILES or None) if form.is_valid(): obj = Album(img=request.FILES['img']) obj = form.save(commit=False) obj.save() return render(request, 'photo_album.html', {'photos': photos, 'username': auth.get_user(request).username}) else: form = PhotoForm return render(request, 'photo_load.html', {'form': form, 'username': auth.get_user(request).username}) else: return render(request, 'photo_add.html') else: return render(request, 'photo_add.html') return render(request, 'photo_album.html', {'photos': photos, 'username': auth.get_user(request).username})
def add_photo(request): if request.method == "POST": form = PhotoForm(request.POST, request.FILES) if form.is_valid(): instance = form.save(commit=False) instance.save() messages.success(request, 'Фотография добавлена') return redirect('choice') else: messages.success(request, 'Фотография не добавлена') else: form = PhotoForm() context = {"form": form} return render(request, 'gallery/add_photo.tpl', context)
def add_photo(request, album_name_slug): try: album = Album.objects.get(slug=album_name_slug) except Album.DoesNotExist: album = None if request.method == 'POST': form = PhotoForm(request.POST, request.FILES) if form.is_valid(): if album: photo = form.save(commit=False) photo.album = album photo.image = form.cleaned_data['image'] photo.user = request.user photo.save() return photos(request, album_name_slug) else: print form.errors form = PhotoForm() context_dict = {'form': form, 'albums': album} return render(request, 'gallery/add_photo.html', context_dict)
def get_context_data(self, *args, **kwargs): data = super(ImageList, self).get_context_data(*args, **kwargs) data['form'] = PhotoForm() return data