Django'unicode'对象没有属性'year'

时间:2013-09-25 22:24:52

标签: python django python-2.7 django-models

我有一个让我发疯的问题。我使用内置服务器开发了一个django应用程序,现在我正在尝试将它部署在apache 2.2后面的生产服务器上。

我已将数据库从SQLite迁移到PostgreSQL。

在Django Admin中,当我在测试服务器上加载File对象的'change_list'视图时,它加载得很好。只要我在生产服务器上执行相同的操作,我就会得到以下结果:

AttributeError at /admin/wilder/file/

'unicode' object has no attribute 'year'

Request Method: GET
Request URL: https://<snip>.berkeley.edu/<snip>/admin/wilder/file/
Django Version: 1.4.1
Exception Type: AttributeError
Exception Value: 

'unicode' object has no attribute 'year'

Exception Location: <snip>/virtualenv/wilder/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py in date_hierarchy, line 302
Python Executable: <snip>/virtualenv/wilder/bin/uwsgi
Python Version: 2.7.5
Python Path: 

['<snip>/virtualenv/wilder/lib/python2.7/:<snip>/virtualenv/wilder/lib/python2.7/site-packages/:<snip>/ecog_patient_db',
 '.',
 '',
 '<snip>/virtualenv/wilder/lib/python2.7/site-packages/readline-6.2.4.1-py2.7-linux-x86_64.egg',
 '<snip>/virtualenv/wilder/lib/python27.zip',
 '<snip>/virtualenv/wilder/lib/python2.7',
 '<snip>/virtualenv/wilder/lib/python2.7/plat-linux2',
 '<snip>/virtualenv/wilder/lib/python2.7/lib-tk',
 '<snip>/virtualenv/wilder/lib/python2.7/lib-old',
 '<snip>/virtualenv/wilder/lib/python2.7/lib-dynload',
 '/home/knight/king/local/lib/python2.7',
 '/home/knight/king/local/lib/python2.7/plat-linux2',
 '/home/knight/king/local/lib/python2.7/lib-tk',
 '<snip>/virtualenv/wilder/lib/python2.7/site-packages',
 '<snip>/virtualenv/wilder/lib/python2.7/site-packages/PIL']

Server time: Wed, 25 Sep 2013 14:41:30 -0700



Error during template rendering

In template /<snip>/ecog_patient_db/templates/admin/change_list.html, error at line 90
'unicode' object has no attribute 'year'
80 {% endif %}
81 {% endblock %}
82 {% if cl.formset.errors %}
83 <p class="errornote">
84 {% blocktrans count cl.formset.errors|length as counter %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}
85 </p>
86 {{ cl.formset.non_form_errors }}
87 {% endif %}
88 <div class="module{% if cl.has_filters %} filtered{% endif %}" id="changelist">
89 {% block search %}{% search_form cl %}{% endblock %}
90 {% block date_hierarchy %}{% date_hierarchy cl %}{% endblock %}
91 
92 {% block filters %}
93 {% if cl.has_filters %}
94 <div id="changelist-filter">
95 <h2>{% trans 'Filter' %}</h2>
96 {% for spec in cl.filter_specs %}{% admin_list_filter cl spec %}{% endfor %}
97 </div>
98 {% endif %}
99 {% endblock %}
100 


Environment:


Request Method: GET
Request URL: https://wilder.berkeley.edu/wilder/admin/wilder/file/

Django Version: 1.4.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'south',
 'reversion',
 'massadmin',
 'django_extensions',
 'ecog_patient_db.wilder')
Installed Middleware:
('johnny.middleware.LocalStoreClearMiddleware',
 'johnny.middleware.QueryCacheMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.transaction.TransactionMiddleware')


Template error:
In template <snip>/ecog_patient_db/templates/admin/change_list.html, error at line 90
   'unicode' object has no attribute 'year'
   80 :       {% endif %}
   81 :     {% endblock %}
   82 :     {% if cl.formset.errors %}
   83 :         <p class="errornote">
   84 :         {% blocktrans count cl.formset.errors|length as counter %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}
   85 :         </p>
   86 :         {{ cl.formset.non_form_errors }}
   87 :     {% endif %}
   88 :     <div class="module{% if cl.has_filters %} filtered{% endif %}" id="changelist">
   89 :       {% block search %}{% search_form cl %}{% endblock %}
   90 :       {% block date_hierarchy %} {% date_hierarchy cl %} {% endblock %}
   91 : 
   92 :       {% block filters %}
   93 :         {% if cl.has_filters %}
   94 :           <div id="changelist-filter">
   95 :             <h2>{% trans 'Filter' %}</h2>
   96 :             {% for spec in cl.filter_specs %}{% admin_list_filter cl spec %}{% endfor %}
   97 :           </div>
   98 :         {% endif %}
   99 :       {% endblock %}
   100 : 


Traceback:
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  136.                     response = response.render()
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/response.py" in render
  104.             self._set_content(self.rendered_content)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  81.         content = template.render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/base.py" in render
  823.                 bit = self.render_node(node, context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/template/base.py" in render
  1178.                     _dict = func(*resolved_args, **resolved_kwargs)
File "<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in date_hierarchy
  302.                 if date_range['first'].year == date_range['last'].year:

Exception Type: AttributeError at /admin/wilder/file/
Exception Value: 'unicode' object has no attribute 'year'

错误和环境周围的上下文显示data_range变量以字符串形式返回,但代码期望它是一个日期时间对象:

<snip>/virtualenv/wilder/lib/python2.7/site-packages/django/contrib/admin/templatetags/admin_list.py in date_hierarchy
            link = lambda d: cl.get_query_string(d, [field_generic])
            if not (year_lookup or month_lookup or day_lookup):
                # select appropriate start level
                date_range = cl.query_set.aggregate(first=models.Min(field_name),
                                                    last=models.Max(field_name))

                if date_range['first'] and date_range['last']:
                    if date_range['first'].year == date_range['last'].year:
                        ...
                        year_lookup = date_range['first'].year
                        if date_range['first'].month == date_range['last'].month:
                            month_lookup = date_range['first'].month

            if year_lookup and month_lookup and day_lookup:
                day = datetime.date(int(year_lookup), int(month_lookup), int(day_lookup))

\u25bc Local vars
Variable Value
month_field 
'file_modification_date__month'

cl 
<django.contrib.admin.views.main.ChangeList object at 0x7f001c142f10>

year_field 
'file_modification_date__year'

field_generic 
'file_modification_date__'

date_range 
{'first': u'2005-01-27 11:05:14', 'last': u'2013-06-12 21:17:45.085712'}

day_field 
'file_modification_date__day'

month_lookup 
None

link 
<function <lambda> at 0x1250e60>

year_lookup 
None

day_lookup 
None

field_name 
'file_modification_date'

我终生无法弄清楚为什么突然这会在生产系统上失败。如果我添加?file_modification_date__year=2006,则https://<snip>.berkeley.edu/<snip>/admin/wilder/file/?file_modification_date__year=2006正确加载。但https://<snip>.berkeley.edu/<snip>/admin/wilder/file/是触发上述错误的原因,但仅限于生产服务器上。有线索吗?你还需要更多信息吗?

1 个答案:

答案 0 :(得分:0)

嗯,这很烦人。刷新memcache就可以了。我在用johnny_cache。