强制转换为Unicode:需要字符串或缓冲区,在将dj​​ango从1.2.3升级到1.6.5后找到instancemethod

时间:2014-06-23 11:31:57

标签: python unicode django-1.6

我有一个版本为1.2.3的django应用程序并更新到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}),
  )

搜索/ urls.py

from django.conf.urls import *
from search.views import SiteSearch

urlpatterns = patterns('search.views',
    url(r'^$', SiteSearch(), name='site_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 个答案:

答案 0 :(得分:2)

您的异常是由较旧的Django-Haystack版本引起的。升级它,它与Django 1.6不兼容。

version 1.0 at least中,SearchView类将__name__定义为方法,这违反了Python中的正常用法,其中该属性是字符串。< / p>