使用月过滤器时Django错误

时间:2014-09-17 17:49:26

标签: python django django-models

我正在关注django教程并且能够运行__year过滤器,但是当我尝试使用月份过滤器时,我得到了错误。以下是我在启动python manage.py shell

后尝试过的命令
>>> from django.utils import timezone
>>> current_year = timezone.now().year
>>> current_month = timezone.now().month
>>> print (current_year)
2014
>>> print (current_month)
9
>>> Choice.objects.filter(question__pub_date__year=current_year)
[<Choice: Not much>, <Choice: the sky>, <Choice: Just hacking again>]
>>> Choice.objects.filter(question__pub_date__month=current_month)

我收到以下错误:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 116, in _
_repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 141, in _
_iter__
    self._fetch_all()
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 966, in _
fetch_all
    self._result_cache = list(self.iterator())
  File "C:\Python34\lib\site-packages\django\db\models\query.py", line 265, in i
terator
    for row in compiler.results_iter():
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 70
0, in results_iter
    for rows in self.execute_sql(MULTI):
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 77
5, in execute_sql
    sql, params = self.as_sql()
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 10
9, in as_sql
    where, w_params = self.compile(self.query.where)
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 80
, in compile
    return node.as_sql(self, self.connection)
  File "C:\Python34\lib\site-packages\django\db\models\sql\where.py", line 106,
in as_sql
    sql, params = qn.compile(child)
  File "C:\Python34\lib\site-packages\django\db\models\sql\compiler.py", line 80
, in compile
    return node.as_sql(self, self.connection)
  File "C:\Python34\lib\site-packages\django\db\models\lookups.py", line 149, in
 as_sql
    lhs_sql, params = self.process_lhs(qn, connection)
  File "C:\Python34\lib\site-packages\django\db\models\lookups.py", line 304, in
 process_lhs
    sql, tz_params = connection.ops.datetime_extract_sql(self.extract_type, lhs,
 tzname)
  File "C:\Python34\lib\site-packages\django\db\backends\sqlite3\base.py", line
197, in datetime_extract_sql
    raise ImproperlyConfigured("This query requires pytz, "
django.core.exceptions.ImproperlyConfigured: This query requires pytz, but it is
n't installed.

我使用的是Windows 7 64位操作系统 Python 3.4.1 Django 1.7

1 个答案:

答案 0 :(得分:3)

根据django documentation,如果你有时区支持启用

settings.py USE_TZ = True

然后您需要安装pytz模块

pip install pytz

,这也显示在错误的最后一行。