强制转换为Unicode:需要字符串或缓冲区,升级Django从1.2.3到1.6.5后发现实例方法
我有一个Django应用,版本是1.2.3,后来更新到了1.6.5,应用运行得很好,但管理员的链接打不开,访问 localhost:8000/admin/
时出现了下面的错误。
错误追踪信息
Traceback:
File "/Users/user/.envs/proj/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/user/.envs/proj/lib/python2.7/site-packages/django/contrib/admin/sites.py" in wrapper
215. return self.admin_view(view, cacheable)(*args, **kwargs)
File "/Users/user/.envs/proj/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapped_view
99. response = view_func(request, *args, **kwargs)
File "/Users/user/.envs/proj/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/Users/user/.envs/proj/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner
198. return view(request, *args, **kwargs)
File "/Users/user/.envs/proj/lib/python2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/Users/user/.envs/proj/lib/python2.7/site-packages/django/contrib/admin/sites.py" in index
358. model_dict['admin_url'] = reverse('admin:%s_%s_changelist' % info, current_app=self.name)
File "/Users/user/.envs/proj/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse
503. app_list = resolver.app_dict[ns]
File "/Users/user/.envs/proj/lib/python2.7/site-packages/django/core/urlresolvers.py" in app_dict
329. self._populate()
File "/Users/user/.envs/proj/lib/python2.7/site-packages/django/core/urlresolvers.py" in _populate
290. for name in pattern.reverse_dict:
File "/Users/user/.envs/proj/lib/python2.7/site-packages/django/core/urlresolvers.py" in reverse_dict
315. self._populate()
File "/Users/user/.envs/proj/lib/python2.7/site-packages/django/core/urlresolvers.py" in _populate
278. lookup_str = callback.__module__ + "." + callback.__name__
Exception Type: TypeError at /admin/
Exception Value: coercing to Unicode: need string or buffer, instancemethod found
当我把Django版本换回1.5.3时,一切又正常了,没有错误。那么最新的版本到底出了什么问题呢?我们需要对unicode设置做什么改动吗?
补充说明:
urls.py
from django.conf.urls import *
from django.conf import settings
from django.contrib import admin
from django.views.generic import TemplateView
from feeds.sitemap import SITEMAP
admin.autodiscover()
urlpatterns = patterns('',
# Core Website Pages
(r'^$', 'core.views.homepage'),
# Site Map
(r'^sitemap.xml$', 'django.contrib.sitemaps.views.sitemap', {'sitemaps': SITEMAP}),
# Admin pages
(r'^admin/', include(admin.site.urls)),
(r'^search/', include('search.urls')),
)
# Static Content Code, Used Only For Development
import os.path
static = os.path.join(
os.path.dirname(__file__), 'media'
)
new_static = os.path.join(
os.path.dirname(__file__), 'new_media'
)
if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': static}),
(r'^new_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': new_static}),
)
search/urls.py
from django.conf.urls import *
from search.views import SiteSearch
urlpatterns = patterns('search.views',
url(r'^$', SiteSearch(), name='site_search'),
)
search/views.py
from haystack.views import SearchView
class SiteSearch(SearchView):
def get_results(self):
model_search_form = self.form_class(self.request.GET)
model_search_form.is_valid()
models = model_search_form.get_models()
if self.query:
search_results = self.form.search()
if len(models) == 1 and models[0].__name__ == 'Press_Releases':
search_results = search_results.order_by('-date')
return search_results
return []
1 个回答
2
你的错误是因为你使用了一个旧版的Django-Haystack。你需要升级它,因为这个旧版本和Django 1.6不兼容。
在1.0版本及以上中,SearchView
类把__name__
定义成了一个方法,这和Python的正常用法不符,因为在Python中,这个属性应该是一个字符串。