2017-02-13 69 views
0

我目前正在使用python django web框架中的日曆。我想打印的東西,提高了以下錯誤:錯誤:強制爲Unicode:需要字符串或緩衝區,長時間發現

TypeError: coercing to Unicode: need string or buffer, long found

我已經看到了類似問題的人,但不是「長」和有效的解決方案.... 回溯:

> Internal Server Error: /tande/holiday/ Traceback (most recent call 
> last): File 
> "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 
> 149, in get_response 
>  response = self.process_exception_by_middleware(e, request) File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 
> 147, in get_response 
>  response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Library/Python/2.7/site-packages/django/contrib/auth/decorators.py", 
> line 23, in _wrapped_view 
>  return view_func(request, *args, **kwargs) File "/Users/ryoung/Documents/Database/prodman/tande/views.py", line 83, in 
> holiday 
>  cal = HolidayCalendar(my_holidays).formatmonth(year, month) File "/Users/ryoung/Documents/Database/prodman/tande/views.py", line 397, 
> in __init__ 
>  print holiday File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 
> 237, in __repr__ 
>  return repr(data) File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 
> 467, in __repr__ 
>  u = six.text_type(self) TypeError: coercing to Unicode: need string or buffer, long found 

這裏是我的代碼:

class HolidayCalendar(HTMLCalendar): 

    def __init__(self, holiday): 
     super(HolidayCalendar, self).__init__() 
     self.holiday = self.holiday_days(holiday) 
     print holiday 
     # for something in holiday: 
     #  print something 

    def holiday_days(self, holiday): 
      field = lambda holiday: holiday.start_date.day 
      return dict(
      [(day, list(items)) for day, items in groupby(holiday, field)] 
      ) 

我想看看self.holiday被設置爲...我想一個for循環藏漢,因爲我認爲這將是項目的列表,並將其RAI sed同樣的錯誤。 這是調用以上的視圖

@login_required 
def holiday(request): 
    # get persons username 
    # get month and year today 
    date_today = datetime.now() 
    year = date_today.year 
    month = date_today.month 
    # get holiday objects in the current month 
    my_holidays = Holiday.objects.order_by('start_date').filter(
     Q(start_date__year=year, start_date__month=month) | Q(end_date__year=year, end_date__month=month) 
    ) 
    cal = HolidayCalendar(my_holidays).formatmonth(year, month) 

    # forms... render template...etc 

謝謝!

+0

請發佈完整的回溯。 –

+0

編輯上面:) –

+1

這個問題似乎是在你的'Holyday'模型的'__str__'或'__unicode__'方法。這些方法應該返回resp。一個字符串或unicode對象,它看起來像返回一個很長的(可能是pk - 狂野的猜測......)。 –

回答

1

該問題似乎是在您的Holyday模型的strunicode方法。這些方法應該返回resp。一個字符串或unicode對象,它看起來像是返回一個很長的(可能是PK - 瘋狂的猜測......)

相關問題