Python论坛  - 讨论区

标题:[python-chinese] [django]分页遇到的怪异问题。

2007年02月22日 星期四 20:56

风向标 vaneoooo在gmail.com
星期四 二月 22 20:56:59 HKT 2007

ÎÒʹÓÃÁËbeykingÐֵķÖÒ³´úÂ룬½á¹ûÔÚ·ÖÒ³ÏîÄ¿³¬¹ý10ҳʱ£¬¾Í³öÏÖÕâÑùµÄ±¨´í£º

 TypeError at /forum/ sequence item 10: expected string, NoneType
found  Request
Method: GET Request URL: http://127.0.0.1/forum/ Exception Type:
TypeError Exception
Value: sequence item 10: expected string, NoneType found Exception Location:
C:\Python24\lib\site-packages\django-
0.95.1-py2.4.egg\django\template\__init__.py in render_node, line 706

Ææ¹ÖµÄÊÇ£¬Í¬ÑùµÄ´úÂëÔÚbeykingÐÖµÄÏîÄ¿ beylog ÖÐÈ´Äܹ»Õý³£·ÖÒ³£¬´úÂëÈçÏ£º

from django import template



register = template.Library()



def calcPageRanges(cur_index, max_index):
    result = []

    if max_index <= 10:
        result.append(range(0, max_index + 1))
        return result

    c1 = cur_index - 3
    c2 = cur_index + 4
    if c1 <= 0:
        result.append(range(0, 7))
        result.append([max_index])
        return result
    else:
        result.append([0])

    if c2 > max_index:
        result.append(range(max_index -6, max_index + 1))
    else:
        result.append(range(c1, c2))
        result.append([max_index])

    return result


_state= 'Page'
_year = None
_month = None
_category_id = None

def getGlobalVars(context):
    global _state, _year, _month, _category_id

    try:
        _state= template.resolve_variable('state', context)
    except template.VariableDoesNotExist:
        _state= 'Page'

    if _state== 'Category':
        try:
            _category_id = template.resolve_variable('category_id', context)
        except template.VariableDoesNotExist:
            _category_id = None
    elif _state== 'Archive':
        try:
            _year = template.resolve_variable('year', context)
            _month = template.resolve_variable('month', context)
        except template.VariableDoesNotExist:
            _year = None
            _month = None


def pageRangeLink(cur_index, page_range):
    global _state, _year, _month, _category_id
    for n in page_range:
        if n != cur_index:
            if _state== 'Page':      #ÎÒÔÚʹÓÃʱ¾ÍÔÚ´Ë´¦£¬ÒÔ¼°ºóÐøµÄ¸÷¸öÏà¹ØλÖüÓÈë×Ô¼º¶¨ÒåÏ if
_state == 'forum':
                yield '%s'%(n, n)
            elif _state== 'Category':
                yield '%s'%(_category_id, n,
n)
            elif _state== 'Archive':
                yield '%s'%(_year, _month,
n, n)
        else:
            yield '%s'%(n)

def pagePrevLink(page_index):
    global _state, _year, _month, _category_id
    if _state== 'Page':
        return ''%(page_index)
    elif _state== 'Category':
        return ''%(_category_id, page_index)
    elif _state== 'Archive':
        return ''%(_year, _month, page_index)

def pageNextLink(page_index):
    global _state, _year, _month, _category_id
    if _state== 'Page':
        return ''%(page_index)
    elif _state== 'Category':
        return ''%(_category_id, page_index)
    elif _state== 'Archive':
        return ''%(_year, _month, page_index)


class PagebarNode(template.Node):
    def __init__(self, cur_index, max_index):
        self.curindex = cur_index
        self.maxindex = max_index

    def render(self, context):
        self.cur_index = template.resolve_variable(self.curindex, context)
        self.max_index = template.resolve_variable(self.maxindex, context)

        if self.max_index < 1: return ''

        getGlobalVars(context)

        pageRanges = calcPageRanges(self.cur_index, self.max_index)
        htmls = []
        htmls.append('
') if len(pageRanges) == 1: htmls.extend(pageRangeLink(self.cur_index, pageRanges[0])) else: i = 0 e = len(pageRanges) - 1 for pr in pageRanges: if (len(pr) == 1) and (i == 0): htmls.append( pagePrevLink(self.cur_index-1) ) htmls.extend(pageRangeLink(self.cur_index, pr)) if i != e: htmls.append( '...' ) if (len(pr) == 1) and (i != 0): htmls.append( pageNextLink(self.cur_index+1) ) i = i + 1 htmls.append('
') return ''.join(htmls) #class DoPagebar: # def __call__(self, parser, token): # tokens = token.contents.split() # if len(tokens) != 3: # raise template.TemplateSyntaxError, "%r tag requires 2 arguments" % tokens[0] # cur_index = tokens[1] # max_index = tokens[2] # return PagebarNode(cur_index, max_index) def doPagebar(parser, token): tokens = token.contents.split() if len(tokens) != 3: raise template.TemplateSyntaxError, "%r tag requires 2 arguments" % tokens[0] cur_index = tokens[1] max_index = tokens[2] return PagebarNode(cur_index, max_index) # registration register.tag('pagebar', doPagebar) #DoPagebar() ÔÚ·ÖÒ³²»×ã10ҳʱһÇÐÕý³££¬³¬¹ý10Ò³Ëæ¼´³öÏָôíÎó¡£ Ï൱ͷÌÛ£¬ÐÂÄêÀïµÄÒ»Ìì¾Í»¨·ÑÔÚÕâÉÏÃæÁË£¬Çó½ÌÖ¸µã¡£Ð»Ð» -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070222/0d6fd395/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号