Django TypeError"期望的字符串或缓冲区"

时间:2016-05-03 13:44:41

标签: python django model-view-controller

我不知道哪里出错了。当我试图从表单中获取日期时,我收到错误"期望的字符串或缓冲区"

型号:

class Trip(models.Model):
    creator = models.ForeignKey(Person, on_delete=models.CASCADE, default=None)
    name = models.CharField(max_length=200)
    start_time = models.DateField()
    end_time = models.DateField()

形式:

class TripForm(forms.Form):
    name = forms.RegexField(regex=r'^\w+$', widget=forms.TextInput(attrs=dict(required=True, max_length=30)), label=_("Name"), error_messages={ 'invalid': _("This value must contain only letters, numbers and underscores.") })
    start_time = DateTimeField(widget=forms.widgets.DateInput(format="%d %b %Y" ) )
    end_time = DateTimeField(widget=forms.widgets.DateInput(format="%d %b %Y"))

并且在视图中即时提取日期:

   start_time=trip_form['start_time'],
   end_time=trip_form['end_time'],

在html中我创建了input_place:

<div>
      Planned end day of the Trip:
      {{ trip_form.end_time|attr:"type:date" }}
</div>

有什么想法吗?

我必须承认很难找到好的datetimepicker / datepicker。

回溯:

File "D:\Program Files\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "D:\Program Files\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "D:\Program Files\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)

File "D:\Program Files\Python27\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)

File "D:\Workspaces\TravelBuddyDj\TravelBuddy\views.py" in add_trip
  113.                 end_time=trip_form['end_time'],

File "D:\Program Files\Python27\lib\site-packages\django\db\models\manager.py" in manager_method
  122.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "D:\Program Files\Python27\lib\site-packages\django\db\models\query.py" in create
  401.         obj.save(force_insert=True, using=self.db)

File "D:\Program Files\Python27\lib\site-packages\django\db\models\base.py" in save
  700.                        force_update=force_update, update_fields=update_fields)

File "D:\Program Files\Python27\lib\site-packages\django\db\models\base.py" in save_base
  728.             updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)

File "D:\Program Files\Python27\lib\site-packages\django\db\models\base.py" in _save_table
  812.             result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)

File "D:\Program Files\Python27\lib\site-packages\django\db\models\base.py" in _do_insert
  851.                                using=using, raw=raw)

File "D:\Program Files\Python27\lib\site-packages\django\db\models\manager.py" in manager_method
  122.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "D:\Program Files\Python27\lib\site-packages\django\db\models\query.py" in _insert
  1039.         return query.get_compiler(using=using).execute_sql(return_id)

File "D:\Program Files\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  1059.             for sql, params in self.as_sql():

File "D:\Program Files\Python27\lib\site-packages\django\db\models\sql\compiler.py" in as_sql
  1019.                 for obj in self.query.objs

File "D:\Program Files\Python27\lib\site-packages\django\db\models\sql\compiler.py" in prepare_value
  958.             value = field.get_db_prep_save(value, connection=self.connection)

File "D:\Program Files\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_save
  728.                                       prepared=False)

File "D:\Program Files\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_db_prep_value
  1301.             value = self.get_prep_value(value)

File "D:\Program Files\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
  1296.         return self.to_python(value)

File "D:\Program Files\Python27\lib\site-packages\django\db\models\fields\__init__.py" in to_python
  1260.             parsed = parse_date(value)

File "D:\Program Files\Python27\lib\site-packages\django\utils\dateparse.py" in parse_date
  60.     match = date_re.match(value)

Exception Type: TypeError at /addtrip/
Exception Value: expected string or buffer

1 个答案:

答案 0 :(得分:0)

错误来自django.util.dateparse.parse_date,因此请确保您的日期格式正确。

以下是正则表达式parse_date用于计算日期,请确保您的输入采用此格式(字符串&#39; YYYY-MM-DD&#39;):

date_re = re.compile(
    r'(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})$'
)

您可能需要配置日期选择器以此格式提供日期,或者写一些javascript以Django期望的字符串格式输出日期值。

None也应该没问题(至少它不应该产生此错误),尽管根据您的模型,数据库可能会抱怨该值。