valentine
valentine

Reputation: 15

Django: How to convert HTML date from request to Python string

I having trouble converting Date from HTML to Python String to use it in query

I want to send query to MySQL with Date argument.

roomList=Room.objects.raw(
             "select room.id "
 +"from room join client_room on room.id=client_room.room_id "
 +"where client_room.date_in>'"+request.GET.get("date_out")+ ..."#argument-date_out

I recieve Date from HTML

            <p>
                <label>Enter first date</label><br>
                <input type="date" name="date_in" value="{{date1}}"/>
            </p>
            <p>
                <label>Enter second date</label><br>
                <input type="date" name="date_out" value="{{date2}}"/>
            </p>

While printing Date alone it prints correctly

print(request.GET.get("date_in"))
2021-04-01#Terminal

But if trying to assign and/or concatenate it returns NoneType

date=request.GET.get("date_in")
print("Date is " + date)
can only concatenate str (not "NoneType") to str #HTML Response

Upvotes: 0

Views: 381

Answers (2)

valentine
valentine

Reputation: 15

Problem was solved by using Django DateTimeField class.

date=models.DateTimeField()
date=request.GET.get("date_in")
print("Date is " + str(date))
Date is 2021-04-09#Terminal

Upvotes: 0

Sid
Sid

Reputation: 2189

Try using strftime:

from datetime import datetime
print("Date is " + datetime.strftime(date))

Upvotes: 1

Related Questions