Home > database >  django datetime fromisoformat: argument must be str error
django datetime fromisoformat: argument must be str error

Time:12-13

I'm trying to save the date information to the database but I'm getting a typeerror

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 codes in this views.py file I try timezone.now but it doesn't work

CodePudding user response:

sorry my codes here

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' %}")

CodePudding user response:

you are not giving datetime value, so it gives error. First import timezone from django utils

from django.utils import timezone

and replace

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

to

date = timezone.now()

where you import timezone from django utils

  • Related