Search code examples
pythondjangoweb

django datetime fromisoformat: argument must be str error


I'm trying to save the date information to the database but I'm getting a type error:

def message_send(request):
    if request.user.is_authenticated:
        if request.method == 'POST':
            name = request.user.username
            email = request.user.get_email
            title = request.POST.get('title')
            message = request.POST.get('message')
            date = models.DateTimeField(default=timezone.now)
            support = supportmessages(name=name,email=email,title=title,message=message,date=date)
            support.save()
            messages.success(request, 'Your mesaage has been sent')
            return render(request,"messagesend.html")
        return render(request,"messagesend.html")
    else:
        return redirect(request,"{% url 'home' %}")

The code's in this views.py file I try timezone.now but it doesn't work.


Solution

  • With this line:

    date = models.DateTimeField(default=timezone.now)
    

    You are actually feeding the date field the django.db.models.DateTimeField class that is used to define the database field.

    You should replace it with the value you want to insert:

    date = timezone.now()