简体   繁体   English

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

[英]Django TypeError “expected string or buffer”

I have no idea where is mistake. 我不知道哪里出错了。 I get error "expected string or buffer", when i'm trying to get date from form. 当我试图从表单中获取日期时,我收到错误“期望的字符串或缓冲区”。

Model: 模型:

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()

Form: 形成:

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"))

And in view im extracting date like that: 并在视图中提取日期:

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

in html im creating input_place like that: 在html我创建input_place像这样:

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

Any ideas? 有任何想法吗?

I have to admit is very hard to find good datetimepicker/datepicker. 我不得不承认很难找到好的datetimepicker / datepicker。

Traceback: 追溯:

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

The error is coming from django.util.dateparse.parse_date so make sure your date is properly formatted. 该错误来自django.util.dateparse.parse_date因此请确保您的日期格式正确。

Here is the regex parse_date is using to figure out the date, make sure your input is in this format (string 'YYYY-MM-DD'): 这是正则表达式parse_date用来计算日期,确保你的输入是这种格式(字符串'YYYY-MM-DD'):

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

You will likely have to configure your date picker to give you the date in this format, or write a bit of javascript to output your date value in the string format Django is expecting. 您可能需要配置日期选择器以此格式提供日期,或者写一些javascript以Django期望的字符串格式输出日期值。

None should also be OK (at least it shouldn't produce this error), although based on your model the database will likely complain about that value. None人也应该没问题(至少它不应该产生这个错误),虽然根据你的模型,数据库可能会抱怨这个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM