小编Hyb*_*rid的帖子

Slider FileReader JS多图像上传(递增索引)

我正在尝试制作一个JavaScript多图像上传器,将图像预览上传到滑块,但我遇到了一些问题.到目前为止看起来我能够将图像上传到滑块,但问题似乎发生在我的i变量 - 当我尝试增加它时,它保持不变,不允许我next和previous滑块箭头工作.如果有人知道如何让这个滑块正常工作,我将不胜感激.

JS代码:

$('#_uploadImages').click(function() {
    $('#_imagesInput').click()
})

$('#_imagesInput').on('change', function() {
    handleFileSelect();
});

function handleFileSelect() {
    //Check File API support
    if (window.File && window.FileList && window.FileReader) {

        var files = event.target.files; //FileList object
        var output = document.getElementById("frames");

        for (var i = 0; i < files.length; i++) {
            var file = files[i];

            //Only pics
            if (!file.type.match('image')) continue;

            var picReader = new FileReader();
            picReader.addEventListener("load", function (event) {
                var picFile = event.target;

                console.log(event);

                current_i = i;
                prev_i …
Run Code Online (Sandbox Code Playgroud)

html javascript css django jquery

7
推荐指数
1
解决办法
1639
查看次数

Django测试错误"创建数据库的权限被拒绝" - 使用Heroku Postgres

我正在尝试使用Django App目录中的tests.py文件(mysite/polls/tests.py)进行简单的Django测试,但每次运行'python manage.py test polls'时,我都会收到错误消息:

C:\Python27\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 3.0\helpers\pycharm\django_test_manage.py" test polls "C:\Users\<myname>\PycharmProjects\mysite"
Testing started at 8:40 PM ...
Creating test database for alias 'default'...
Type 'yes' if you would like to try deleting the test database 'test_<database>', or 'no' to cancel: Got an error creating the test database: permission denied to create database
Run Code Online (Sandbox Code Playgroud)

根据我的阅读,显然Heroku PG使用共享数据库,因此我没有创建/销毁数据库的权限,这是测试所必需的.这有明显的解决方案吗?我仍然在我的本地驱动器上开发,所以任何变通办法都会受到赞赏.我知道测试是编程的一个重要部分,所以我希望能够尽快实现测试方法.

我正在尝试使用TestCasedjango类进行测试.

我正在使用的:
1)Heroku Postgres Hobby开发计划
2)Postgres 9.3.3
3)Python 2.7.6
4)Django 1.6.1


编辑:所以在做了一些研究之后,我发现我可以DATABASES在settings.py中覆盖我的dict变量来使用SQLite在本地测试(当'test'是shell中的参数时),但我仍然更喜欢PostgreSQL实现,从我读到的,PostgreSQL更严格(我是粉丝).

对于任何对我发现的半解决方案感兴趣的人(由Stackoverflow的另一个成员提供):

if 'test' in sys.argv:
    DATABASES['default'] = {'ENGINE': …
Run Code Online (Sandbox Code Playgroud)

python testing django postgresql heroku

6
推荐指数
1
解决办法
1791
查看次数

如何将 io.BytesIO pdfrw PDF 保存到 Django FileField

我想要做的基本上是:

  1. 从 URL 获取 PDF
  2. 通过pdfrw修改
  3. 将其作为 BytesIO obj 存储在内存中
  4. 通过将其上传到 Django FileField Model.objects.create(form=pdf_file, name="Some name")

我的问题是,当create()方法运行,这样可以节省所有字段除了为form。

helpers.py

import io
import tempfile
from contextlib import contextmanager

import requests
import pdfrw


@contextmanager
def as_file(url):
    with tempfile.NamedTemporaryFile(suffix='.pdf') as tfile:
        tfile.write(requests.get(url).content)
        tfile.flush()
        yield tfile.name


def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict):
    template_pdf = pdfrw.PdfReader(input_pdf_path)

    ## PDF is modified here

    buf = io.BytesIO()
    print(buf.getbuffer().nbytes). # Prints "0"!
    pdfrw.PdfWriter().write(buf, template_pdf)
    buf.seek(0)
    return buf
Run Code Online (Sandbox Code Playgroud)

视图.py

from django.core.files import File

class FormView(View):
    def get(self, …
Run Code Online (Sandbox Code Playgroud)

python pdf django bytesio pdfrw

6
推荐指数
2
解决办法
3026
查看次数

django-allauth 错误时重定向到自定义页面

我已将django-allauth登录/注册表单移动到我创建的自定义索引页面上的模式。如果登录/注册表单正确填写,一切正常,但现在的问题是,如果登录失败,我将被重定向到/accounts/login/,如果注册失败,我将被重定向到/accounts/signup/. 如何覆盖此行为,以便始终将我重定向回ra_app:index?

设置.py

LOGIN_URL = 'ra_app:index'
LOGIN_REDIRECT_URL = 'ra_app:index'
ACCOUNT_LOGIN_REDIRECT_URL = 'ra_app:index'
ACCOUNT_LOGOUT_REDIRECT_URL = 'ra_app:index'
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True  # Used in conjunction with ACCOUNT_AUTHENTICATION_METHOD to ensure ability to login
ACCOUNT_USERNAME_REQUIRED = False  # Used in conjuction with ACCOUNT_AUTHENTICATION_METHOD to ensure ability to login
ACCOUNT_LOGOUT_ON_GET = True  # Logout without confirmation
Run Code Online (Sandbox Code Playgroud)

python django redirect login django-allauth

5
推荐指数
1
解决办法
520
查看次数

无法将 Debounce ES5 转换为 ES6

我在debounce()网上找到了一个很棒的函数代码,但是我很难将它从 ES5 转换为 ES6。

问题如下:当我使用 ES5 实现时,一切正常。窗口被调整大小,console.log()被立即触发,并且在我指定的 500ms 之前忽略随后的调整大小。

但是,在 ES6 实现中,第一次调用立即生效……但之后的每次调用也会延迟 500 毫秒,即使在冷却之后!

如果有人知道我做错了什么,我真的很感激一些帮助。

例子:

function debounceES5(func, wait, immediate) {
    var timeout;
    return function () {
        var context = this, args = arguments;
        var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
};

const debounceES6 = (callback, wait, immediate=false) => { …
Run Code Online (Sandbox Code Playgroud)

javascript ecmascript-5 ecmascript-6 debounce

5
推荐指数
1
解决办法
2175
查看次数

jQuery setTimeout $ this.remove()重叠多个元素

我有2个单独的<div>元素,它们具有类似的invoke按钮,可以添加透明的覆盖消息.我的问题是,当我快速连续点击两个按钮,或者在超时到期之前两次按下相同的按钮时,叠加层仅被移除一次(而不是堆叠,但是添加了多个叠加层).我很感激任何帮助解决这个问题,以及如果有人可以帮我整理我的代码(它似乎非常低效).

JSFiddle:http://jsfiddle.net/Hybridx24/733ueao4/

$(document).on('click', '#invoke', function(e) {

    $this = $(this);

    $cardOverlay = $("<div style='transition: background 0.2s linear' class='card-overlay'><span style='color:white;position:relative;top:40%;font-weight:bold'>Success!</span></div>");

    $this.parent('.card-shadow').append($cardOverlay);

    $cardOverlay.height('100%').css({
        'opacity' : 1,
        'position': 'absolute',
        'top': 0,
        'left': 0,
        'background': 'rgba(0, 0, 0, 0.6)',
        'width': '100%',
        'z-index': 1,
        'border-radius': '6px',
        'text-align': 'center'
    });

    setTimeout(function() {
        $cardOverlay.fadeOut("normal", function(){
            $(this).remove();
        })
    }, 2000)
});
Run Code Online (Sandbox Code Playgroud)

html javascript css jquery fade

3
推荐指数
1
解决办法
161
查看次数

Django 结合 __unaccent 和 __search 查找

因此,我尝试在同一模型过滤器中使用__unaccent和__search,但这样做时收到错误。我正在尝试使用术语“Pokemon”来匹配术语“Pok\xc3\xa9mon”(注意“\xc3\xa9”)

\n\n

Game.objects.filter(title__unaccent__icontains="Pokemon")工作正常,但是当我使用时Game.objects.filter(title__unaccent__search="Pokemon),出现以下错误:

\n\n
\n

/autocomplete-games/ 函数 unaccent(tsquery) 处出现编程错误

\n\n

不存在 第 1 行: ...ALESCE(UNACCENT("main_game"."title"), \'\'))\n @@ (UNACCENT(p...\n ^ 提示:没有函数与给定名称匹配和参数类型。您可能需要\n 添加显式类型转换。

\n
\n\n

我在用:

\n\n

Python 3.5

\n\n

姜戈 1.10

\n\n

PostgreSQL(已安装非重音扩展)

\n

python django postgresql orm

3
推荐指数
1
解决办法
2380
查看次数

Python如何获取图像然后POST(通过请求库)

我正在使用 python 尝试get从一个 API 获取图像,然后将post其传输到另一个 API。

到目前为止我有这个代码:

def create_item(token, user_id):
    url = '<api_url_to_post_to>'
    headers = {"Authorization": "Token {0}".format(token)}

    image_url = '<some_random_image_url>'
    image_obj = requests.get(image_url)

    data = {
        "image": image_obj.content
    }

    r = requests.post(url, headers=headers, files=data)
    response = r.json()

    print(response)

    return response
Run Code Online (Sandbox Code Playgroud)

问题是,当我尝试使用post第二个 API 时,我收到“不允许使用文件扩展名”。错误。file这显然是一个自定义错误消息,但它表示我正在执行的操作有问题post。

关于我可能做错了什么有什么建议吗?

python django post get python-requests

1
推荐指数
1
解决办法
599
查看次数