- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 django 创建具有开始日期时间和结束日期时间的事件。在表单中,我尝试使用 AdminSPlitDateTime 小部件分隔日期和时间输入(我希望有一些 JS 用于输入。)当我尝试创建事件时,django 给我“'list'对象没有属性'剥离'”错误。 SplitDateTimeWidget 也会发生这种情况。
当我删除小部件或使用 DateTimeInput 小部件(无拆分)时,一切都会按预期工作。但用户可能会混淆输入正确的日期时间格式。所以我想使用 AdminSplitDateTime。
这一定是用小部件分割日期和时间的问题,但我找不到解决这个问题的方法。
使用Python 3.7Django 2.2.7Pipenv 2018.11.26
我的型号是
class Event(models.Model):
event_start = models.DateTimeField()
event_end = models.DateTimeField()
notes = models.CharField(max_length=140, null=True, blank=True)
hasta = models.ForeignKey(Hasta, on_delete=models.CASCADE)
doktor = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return f'{self.event_start} -- {self.event_end}'
class Meta:
ordering = ['event_start']
我的表单类是:
class EventCreateForm(ModelForm):
event_start = forms.DateTimeField(widget= admin_widgets.AdminSplitDateTime())
event_end = forms.DateTimeField(widget=admin_widgets.AdminSplitDateTime())
def __init__(self, *args, **kwargs):
"""this whole thing is for filtering the hasta objects. User can only create an event with his patients."""
self.user = kwargs.pop('user')
super().__init__(*args, **kwargs)
self.fields['hasta'].queryset = Hasta.objects.filter(doktor_id=self.user.id)
class Meta:
model = Event
fields = [
'event_start',
'event_end',
'notes',
'hasta',
]
labels = {
'event_start': _('Randevu Başlangıç Tarihi'),
'event_end': _('Randevu Bitiş Tarihi'),
'notes': _('Notlar')
}
我的 View 类是:
class EventCreateView(LoginRequiredMixin, CreateView):
model = Event
template_name = 'create_event.html'
form_class = EventCreateForm
success_url = '/randevu'
def get_form_kwargs(self):
"""This adds the user object to the kwargs and it will be used in create event form to filter
the patients of the current user"""
kwargs = super(EventCreateView, self).get_form_kwargs()
kwargs.update({'user': self.request.user})
return kwargs
def form_valid(self, form):
"""This adds the current user id to the created event"""
form.instance.doktor = self.request.user
form.save()
messages.success(self.request, f"randevu Kaydedildi!")
return super().form_valid(form)
我收到以下错误;
AttributeError: 'list' object has no attribute 'strip'
[19/Nov/2019 13:34:11] "POST /randevu/yeni_randevu/ HTTP/1.1" 500 117105
Internal Server Error: /randevu/yeni_randevu/
Traceback (most recent call last):
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\contrib\auth\mixins.py", line 52, in dispatch
return super().dispatch(request, *args, **kwargs)
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\views\generic\edit.py", line 172, in post
return super().post(request, *args, **kwargs)
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\views\generic\edit.py", line 141, in post
if form.is_valid():
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\forms.py", line 185, in is_valid
return self.is_bound and not self.errors
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\forms.py", line 180, in errors
self.full_clean()
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\forms.py", line 381, in full_clean
self._clean_fields()
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\forms.py", line 399, in _clean_fields
value = field.clean(value)
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\fields.py", line 148, in clean
value = self.to_python(value)
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\fields.py", line 462, in to_python
result = super().to_python(value)
File "C:\Users\alper\.virtualenvs\sekreter-1XQSW2uk\lib\site-packages\django\forms\fields.py", line 379, in to_python
value = value.strip()
AttributeError: 'list' object has no attribute 'strip'
最佳答案
您必须将 SplitDateTimeField
与 AdminSplitDateTime
结合使用。
关于python - Django SplitDateTime 小部件抛出 'list' 对象没有属性 'strip',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58932440/
我在 Django 中有一个带有日期时间字段的模型。 class Entry(models.Model): start = models.DateTimeField() end = m
我正在尝试使用 django 创建具有开始日期时间和结束日期时间的事件。在表单中,我尝试使用 AdminSPlitDateTime 小部件分隔日期和时间输入(我希望有一些 JS 用于输入。)当我尝试创
我是一名优秀的程序员,十分优秀!