为什么会出现索引错误(元组索引超出范围)?

时间:2020-05-12 16:17:43

标签: python django

实际上,我正在使用sql的Django项目中进行工作。每当我尝试转到网站上的特定链接时,引用的页面不会打开,并且显示元组索引超出范围。错误在行上显示 代码的23个。我不明白其背后的原因。索引似乎还可以。

def form_valid(self, form):
    key_word = form.cleaned_data['search']
    key_word = '%' + key_word + '%'

    context = {}
    if self.request.user.is_authenticated:
        with connection.cursor() as cursor:
            cursor.execute('SELECT * ' +
                           'FROM `author` ' +
                           'WHERE `id` = %s;', [self.request.user.author_id, ])
            row = cursor.fetchone()

            context['username'] = row[1]

    with connection.cursor() as cursor:
        sql = """SELECT `publication`.`id`, `title`, `description`, `date`, `location`, `publisher`.`id`,
                 `publisher`.`name`, `publisher`.`type`
                 FROM `publication`, `publisher`
                 WHERE `approved`=TRUE AND (`title` LIKE %s OR `description` LIKE %s);"""

        cursor.execute(sql, [key_word, key_word])
        tbl = cursor.fetchall()
        if tbl is not None and tbl[0] is not None:
            context['publications'] = []
            for row in tbl:
                get_publication(row, context)

    return render(self.request, 'portal/search.html', context)

0 个答案:

没有答案