i am trying to add a date to an application that I am writing. the apllication is actualy a list of activities. when i tried to add a datefield to the ui and save it into my database. i got this error
Internal Server Error: /admin/main/vergadering/ Traceback (most recent call last): File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangocorehandlersexception.py", line 47, in inner response = get_response(request) File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangocorehandlersbase.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangocontribadminoptions.py", line 614, in wrapper return self.admin_site.admin_view(view)(*args, **kwargs) File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangoutilsdecorators.py", line 130, in _wrapped_view response = view_func(request, *args, **kwargs) File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangoviewsdecoratorscache.py", line 44, in _wrapped_view_func response = view_func(request, *args, **kwargs) File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangocontribadminsites.py", line 233, in inner return view(request, *args, **kwargs) File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangoutilsdecorators.py", line 43, in _wrapper return bound_method(*args, **kwargs) File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangoutilsdecorators.py", line 130, in _wrapped_view response = view_func(request, *args, **kwargs) File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangocontribadminoptions.py", line 1811, in changelist_view 'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)}, File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangodbmodelsquery.py", line 269, in __len__ self._fetch_all() File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangodbmodelsquery.py", line 1308, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangodbmodelsquery.py", line 53, in __iter__ results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size) File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangodbmodelssqlcompiler.py", line 1189, in execute_sql return list(result) File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangodbmodelssqlcompiler.py", line 1606, in cursor_iter for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel): File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangodbmodelssqlcompiler.py", line 1606, in <lambda> for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel): File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsite-packagesdjangodbutils.py", line 97, in inner return func(*args, **kwargs) File "C:Usersfam de baereAppDataLocalProgramsPythonPython38libsqlite3dbapi2.py", line 64, in convert_date return datetime.date(*map(int, val.split(b"-"))) ValueError: day is out of range for month [02/Feb/2021 13:53:33] "GET /admin/main/vergadering/ HTTP/1.1" 500 133973
this is my model
class Vergadering(models.Model): kriskras = models.ForeignKey(Kriskras, on_delete=models.CASCADE) date = models.DateField(("Date"), default=datetime.date.today) extra = models.CharField(max_length=5) activiteit = models.CharField(max_length=500) def __str__(self): return self.activiteit
this is my html page
{% for vergadering in ls.vergadering_set.all %} <div class="input-group mb-3"> <input type="date", value="{{vergadering.date}}" class="form-control col-2" name="d{{vergadering.id}}"> <input type="text", value="{{vergadering}}" class="form-control" name="c{{vergadering.id}}"> </div> {% endfor %}
and this is my view
def index(response, id): ls = Kriskras.objects.get(id=id) if response.method == "POST": print(response.POST) if response.POST.get("wijzig"): print("wijzig") for vergadering in ls.vergadering_set.all(): pass elif response.POST.get("newVergadering"): txt = response.POST.get("new") date = response.POST.get("newdate") if len(txt) > 2: ls.vergadering_set.create(activiteit=txt, date=date) else: print("invalid") return render(response, "main/kriskras.html", {"ls":ls})
i cant figure out why this is raising, i think it has something to do with the date format?
Advertisement
Answer
try this
class Vergadering(models.Model): kriskras = models.ForeignKey(Kriskras, on_delete=models.CASCADE) date = models.DateField(("Date"), default=datetime.date.today()) # >>> import datetime >>> datetime.date.today <built-in method today of type object at 0x9e14e0> >>> datetime.date.today() datetime.date(2021, 2, 2)