Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django国际化日历并使用单字符日名称_Python_Django - Fatal编程技术网

Python django国际化日历并使用单字符日名称

Python django国际化日历并使用单字符日名称,python,django,Python,Django,我有两个问题,我最近构建了一个django定制templatetag,它在调用时显示日历。我目前面临两个问题,不知道如何解决 如何将日期名称显示为单个字符(S、M、T等),我找到了calendar.day_缩写,返回(SAT、MON..等) 我的网站正在使用多种语言,我想知道如何让它们按照查看语言显示。我尝试使用LocaleTextCalendar(),但没有成功 从django导入模板 导入日历 从django.conf导入设置 register=template.Library() def

我有两个问题,我最近构建了一个django定制templatetag,它在调用时显示日历。我目前面临两个问题,不知道如何解决

  • 如何将日期名称显示为单个字符(S、M、T等),我找到了calendar.day_缩写,返回(SAT、MON..等)
  • 我的网站正在使用多种语言,我想知道如何让它们按照查看语言显示。我尝试使用LocaleTextCalendar(),但没有成功

    从django导入模板

    导入日历

    从django.conf导入设置

    register=template.Library()

    def calendar_解析器(解析器、令牌): """ 日历解析器将处理验证参数并将其传递到上下文 """ 尝试: 标记\名称、年份、月份、条目,如\解析\标记=标记。拆分\内容() 除值错误外: raise template.TemplateSyntaxError,“%r标记需要六个参数”%token.contents.split()[0] 返回日历节点(年、月、条目、解析标记)

    类日历节点(template.node): """ 处理模板中的特定节点。静默失败。 “”“

    注册模板标记,使其可用于模板 tag(“日历视图”,日历解析器)

返回包含缩写的工作日名称的标题。n指定一个工作日的宽度(以字符为单位)


因此,对于n=1,将返回单字符缩写。

请编辑问题以修正格式。这很难阅读。我试过了,但不起作用…帮助在编辑页面的右侧。非常详细。
def __init__(self, year, month, entries, resolve_tag):
    try:
        self.year = template.Variable(year)
        self.month = template.Variable(month)
        self.entries = template.Variable(entries)
        #resolved strings
        self.resolve_tag = resolve_tag
    except ValueError:
        raise template.TemplateSyntaxError

def render(self, context):
    try:
        # FIRST_DAY_OF_WEEK beginning of the week, django setting
        cal = calendar.LocaleTextCalendar(settings.FIRST_DAY_OF_WEEK, 'ar')



        # render calendar header
        context['week_header'] = [day for day in calendar.day_name]



        # Get the variables from the context so the method is thread-safe.
        my_entries = self.entries.resolve(context)
        my_year = self.year.resolve(context)
        my_month = self.month.resolve(context)


        month_days = cal.itermonthdays(my_year, my_month)

        lst = [[]]
        week = 0

        # make month lists containing list of days for each week
        # each day tuple will contain list of entries and 'current' indicator
        for day in month_days:
            entries = current = False   # are there entries for this day; current day?
            lst[week].append((day, my_entries, current))
            if len(lst[week]) == 7:
                lst.append([])
                week += 1

        # assign variable to context as resolve_tag
        context[self.resolve_tag] = lst

        return ''


    except ValueError:
        return
    except template.VariableDoesNotExist:
        raise template.TemplateSyntaxError