Ich arbeite derzeit an einem Kalender im Python-Django-Web-Framework. Ich versuche, etwas und das Erhöhen der folgende Fehler zu drucken:Fehler: erzwingt Unicode: brauche Zeichenfolge oder Puffer, lange gefunden
TypeError: coercing to Unicode: need string or buffer, long found
ich Menschen mit ähnlichen Problemen gesehen, aber nicht mit ‚langen‘ und eine Lösung, die funktioniert .... Traceback:
> 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
Hier ist mein Code:
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)]
)
ich versuche, zu sehen, was self.holiday wie setzt ... ich eine for-Schleife als gut versucht, weil ich dachte, es wäre eine Liste der Elemente, und es rai sed den gleichen Fehler. Hier ist der Ansicht, dass die oben
@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
Dank ruft!
Bitte buchen Sie die vollständige Traceback. –
Bearbeiten oben gemacht :) –
Das Problem scheint in Ihrem 'Holyday'-Modell '__str__' oder' __unicode__' Methode zu sein. Diese Methoden sollten zurückkehren bzw. Ein String oder Unicode-Objekt, es sieht so aus, als ob es eine lange zurückgibt (möglicherweise der pk - wilder Tipp ...). –