Opened 4 years ago
Closed 4 years ago
#31974 closed Uncategorized (invalid)
SplitDateTimeWidget and is_valid problem
Reported by: | carson | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 3.0 |
Severity: | Normal | Keywords: | SplitDateTimeWidget |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
this the models.py
class Rent_Car(models.Model): rent_time = models.DateTimeField( blank=False) car = models.ForeignKey(Cop_Car, on_delete=models.SET_NULL, null=True, blank=False) user = models.ForeignKey(Users, on_delete=models.SET_NULL, null=True, blank=False) car_rage = models.IntegerField(null=False, blank=False) rent_mark = models.CharField(max_length=300", null=True, blank=True)
this the forms.py
class RentcarForm(forms.ModelForm): class Meta: model = models.Rent_Car fields = '__all__' # model.rent_time = forms.DateTimeField(widget=forms.SplitDateTimeWidget) # initial = datetime.datetime.now() # model.rent_time = datetime.datetime.now() widgets = { 'rent_time': forms.SplitDateTimeWidget(date_attrs={'class':'form-control', 'type':'date'},time_attrs={'class':'form-control', 'type':'time'},date_format='%Y-%m-%d'), } def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) for field_name,field in self.fields.items(): field.widget.attrs.update({'class':'form-control'})
this is the views.py
def rentcar_add_edit(request, rid=None): rentcar_obj = models.Rent_Car.objects.filter(pk=rid).first() if request.method == 'GET': if rid: rentcar_form = RentcarForm(instance=rentcar_obj) else: rentcar_form = RentcarForm(initial={'rent_time': datetime.datetime.now(), 'user': request.user}, instance=rentcar_obj) return render(request, 'edit.html', {'rentcar_form': rentcar_form}) else: rentcar_form = RentcarForm(request.POST, instance=rentcar_obj) if rentcar_form.is_valid(): print(rentcar_form.errors) print(rentcar_form.cleaned_data) rentcar_form.save() messages.success(request,'ok!') return redirect('list') else: return render(request, 'edit.html', {'rentcar_form': rentcar_form})
when post the rent_time. show the error' 'list' object has no attribute 'strip
the traceback below:
D:\Python\lib\site-packages\django\core\handlers\exception.py in inner response = get_response(request) … ▼ Local vars Variable Value exc AttributeError("'list' object has no attribute 'strip'") get_response <bound method BaseHandler._get_response of <django.core.handlers.wsgi.WSGIHandler object at 0x000001BBE3C72EC8>> request <WSGIRequest: POST '/rentcar/add/'> D:\Python\lib\site-packages\django\core\handlers\base.py in _get_response response = self.process_exception_by_middleware(e, request) … ▼ Local vars Variable Value callback <function rentcar_add_edit at 0x000001BBE3B0A1F8> callback_args () callback_kwargs {} middleware_method <bound method CsrfViewMiddleware.process_view of <django.middleware.csrf.CsrfViewMiddleware object at 0x000001BBE3CAC788>> request <WSGIRequest: POST '/rentcar/add/'> resolver <URLResolver 'carsoft.urls' (None:None) '^/'> resolver_match ResolverMatch(func=rentcar.views.rentcar_add_edit, args=(), kwargs={}, url_name=add, app_names=[], namespaces=[], route=rentcar/add/) response None self <django.core.handlers.wsgi.WSGIHandler object at 0x000001BBE3C72EC8> wrapped_callback <function rentcar_add_edit at 0x000001BBE3B0A1F8> D:\Python\lib\site-packages\django\core\handlers\base.py in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) … ▼ Local vars Variable Value callback <function rentcar_add_edit at 0x000001BBE3B0A1F8> callback_args () callback_kwargs {} middleware_method <bound method CsrfViewMiddleware.process_view of <django.middleware.csrf.CsrfViewMiddleware object at 0x000001BBE3CAC788>> request <WSGIRequest: POST '/rentcar/add/'> resolver <URLResolver 'carsoft.urls' (None:None) '^/'> resolver_match ResolverMatch(func=rentcar.views.rentcar_add_edit, args=(), kwargs={}, url_name=add, app_names=[], namespaces=[], route=rentcar/add/) response None self <django.core.handlers.wsgi.WSGIHandler object at 0x000001BBE3C72EC8> wrapped_callback <function rentcar_add_edit at 0x000001BBE3B0A1F8> D:\Python\lib\site-packages\django\contrib\auth\decorators.py in _wrapped_view return view_func(request, *args, **kwargs) … ▼ Local vars Variable Value args () kwargs {} login_url None redirect_field_name 'next' request <WSGIRequest: POST '/rentcar/add/'> test_func <function login_required.<locals>.<lambda> at 0x000001BBE3B0A0D8> view_func <function rentcar_add_edit at 0x000001BBE3AFACA8> D:\202008\carsoft\rentcar\views.py in rentcar_add_edit if rentcar_form.is_valid(): … ▼ Local vars Variable Value rentcar_form <RentcarForm bound=True, valid=True, fields=(rent_time;car;user;car_rage;rent_mark)> rentcar_obj None request <WSGIRequest: POST '/rentcar/add/'> rid None D:\Python\lib\site-packages\django\forms\forms.py in is_valid return self.is_bound and not self.errors … ▼ Local vars Variable Value self <RentcarForm bound=True, valid=True, fields=(rent_time;car;user;car_rage;rent_mark)> D:\Python\lib\site-packages\django\forms\forms.py in errors self.full_clean() … ▼ Local vars Variable Value self <RentcarForm bound=True, valid=True, fields=(rent_time;car;user;car_rage;rent_mark)> D:\Python\lib\site-packages\django\forms\forms.py in full_clean self._clean_fields() … ▼ Local vars Variable Value self <RentcarForm bound=True, valid=True, fields=(rent_time;car;user;car_rage;rent_mark)> D:\Python\lib\site-packages\django\forms\forms.py in _clean_fields value = field.clean(value) … ▼ Local vars Variable Value field <django.forms.fields.DateTimeField object at 0x000001BBE41AAC88> name 'rent_time' self <RentcarForm bound=True, valid=True, fields=(rent_time;car;user;car_rage;rent_mark)> value ['2020-09-02', '11:17'] D:\Python\lib\site-packages\django\forms\fields.py in clean value = self.to_python(value) … ▼ Local vars Variable Value self <django.forms.fields.DateTimeField object at 0x000001BBE41AAC88> value ['2020-09-02', '11:17'] D:\Python\lib\site-packages\django\forms\fields.py in to_python result = super().to_python(value) … ▼ Local vars Variable Value __class__ <class 'django.forms.fields.DateTimeField'> self <django.forms.fields.DateTimeField object at 0x000001BBE41AAC88> value ['2020-09-02', '11:17'] D:\Python\lib\site-packages\django\forms\fields.py in to_python value = value.strip() … ▼ Local vars Variable Value self <django.forms.fields.DateTimeField object at 0x000001BBE41AAC88> value ['2020-09-02', '11:17']
looks the widgets SplitDateTimeWidget return the datetime field with a list not a str.
Note:
See TracTickets
for help on using tickets.
Please review the `MultiWidget` docs. There's an example implementing a subclass to work with
DateField
, rather than the expectedMultiValueField
. You can adapt that for your use-case.Please also review TicketClosingReasons/UseSupportChannels, as the issue tracker is not the appropriate place for this kind of question about usage errors.
Thanks.