Пример #1
0
def index_view(request):
    # POST
    if request.method == 'POST':
        # list of symbols
        char_list = string.ascii_lowercase + string.digits
        # generate unique id from list
        unique = ''.join(random.sample(char_list, 15))
        alt = ''
        if request.POST['alt']:
            alt = request.POST['alt']
        try:
            img = Image(image_file=request.FILES['image'], unique_id=unique, alt_text=alt)
        except MultiValueDictKeyError:
            msg = 'Пожалуйста, выберите изображение для загрузки!'
            return render(request, 'images/index.html', {'msg': msg})
        img.save()
        # create different link types
        site_url = 'http://picture-sharing.info'
        img.link_page = site_url + '/images/' + img.unique_id
        img.link_direct = site_url + img.image_file.url
        img.link_forums = '[url={0}][img]{0}[/img][/url]'.format(site_url + img.image_file.url)
        img.link_a = '<a href="{0}" target="_blank"><img src="{0}" \
border="0" alt="{1}"></a>'.format(site_url + img.image_file.url, alt)
        img.save()
        msg = 'Файл успешно загружен!'
        height, width = image_resize(img.image_file.height, img.image_file.width)
        content = {'image': img, 'height': height, 'width': width, 'msg': msg}
        return render(request, 'images/image.html', content)

    # GET
    return render(request, 'images/index.html')