def upload_image(request): print('\nView: upload_image') if (request.user.is_superuser == False): print('Not authorized user, returning to front blog page') return redirect('/blog') Form = ImageForm(request.POST, request.FILES) if Form.is_valid(): Form.save() asset = Asset.objects.all().order_by('-id') paths = [] for x in asset: paths.append(x.asset.path.split('/main', 1)[-1]) paginator = Paginator(paths, 15) #5 blog posts per page page = request.GET.get('page') try: pagPaths = paginator.page(page) except PageNotAnInteger: #If page is not an integer, deliver first page. pagPaths = paginator.page(1) except EmptyPage: #If page is out of range, deliver last page of results pagPaths = paginator.page(paginator.num_pages) return render(request,'image_portal.html', {'ImageForm':Form, 'paths':pagPaths})
def image_uploader(r: HttpRequest): if r.method == "POST": form = ImageForm(r.POST, r.FILES) if form.is_valid(): form.save() return HttpResponseRedirect("/blog/imgs") else: form = ImageForm() return render(r, "blog/upload_img.html", {"form": form})
def image_new(request): if request.method == 'POST': post_image_form = PostImageForm(request.POST, request.FILES) image_form = ImageForm(request.POST, request.FILES) files = request.FILES.getlist('images') if image_form.is_valid() and post_image_form.is_valid(): post_image = post_image_form.save(commit=False) post_image.author = request.user post_image.save() for file in files: Image.objects.create(post_image=post_image, image=file) return redirect('blog:image-list') else: image_form = ImageForm() post_image_form = PostImageForm() files = PostFile.objects.all().order_by('-created_date')[:5] images = PostImage.objects.all().order_by('-created_date')[:8] return render( request, 'blog/image-new.html', { 'image_form': image_form, 'post_image_form': post_image_form, 'files': files, 'images': images })
def upload_image(request): """ A view for uploading a single image at a time, and displaying it's url. Template: blog/add_image_popup.html NOTE: this does not inherit from base.html! Context: form: an ImageForm instance url: a string representing the uploaded image's url :param request: :return: """ code = '' if request.method == 'POST': image_form = ImageForm(request.POST, request.FILES) if image_form.is_valid(): image = image_form.save() code = "<a href='{0}{1}' data-lightbox='ENTER GROUP' " \ "data-title='ENTER CAPTION'>" \ "<img class='thumbnail' src={0}{1} alt='test'>" \ "</a>".format(settings.MEDIA_URL, image.filename) else: print(image_form.errors) else: image_form = ImageForm() return render_to_response('blog/add_image_popup.html', { 'image_form': image_form, 'code': code }, context_instance=RequestContext(request))
def image_upload(request): view_url = reverse('blog.views.image_upload') if request.method == 'POST': # If the form has been submitted... form = ImageForm(request.POST, request.FILES) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data # ... image = form.save() return HttpResponseRedirect('/image/info/' + str(image.pk)) # Redirect after POST else: form = ImageForm() upload_url, upload_data = prepare_upload(request, view_url) t = loader.get_template('image_upload.html') c = RequestContext(request,{ 'form': form, 'title': 'Image Upload', 'upload_url': upload_url, 'upload_data': upload_data }) return HttpResponse(t.render(c))