I am trying to search records between two date range from database. Here is my code views:
def profile(request):
if request.method == 'POST':
fromdate = request.POST.get('from')
todate = request.POST.get('to')
cursordate = connection.cursor()
cursordate.execute('SELECT * FROM Lesson WHERE StartDateTime between "'+fromdate+'" and "'+todate+'"'
)
data = dictfetchall(cursordate)
return render(request,'personal.html',{'data':data})
else:
cursordate = connection.cursor()
cursordate.execute('SELECT * FROM Lesson')
data = dictfetchall(cursordate)
return render(request,'personal.html',{'data':data})
html:
{% for lesson in data1 %}
<div class="card mb-3 mt-1 shadow-sm">
<div class="card-body">
<p class="card-text">
<div class="row">
<div class="col-2">
<div>{{ lesson.StartDateTime}} - {{ lesson.EndDateTime}}</div>
</div>
<div class="col-4">
<div>{{ lesson.Name }}</div>
</div>
</div>
<div class="mt-5"></div>
</p>
</div>
</div>
{% endfor %}
But I get the following error:
Invalid column name "2021-03-02". (207) (SQLExecDirectW); [42S22] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name "2021-03-03". (207)')
Is it connected with the fact that type of the 'StartDateTime' is DateTime and not Date? However,I tried to hardcord dates with time in sql query it still failed. (I know that it may be done with the help of ORM Django, but I need to use raw SQL)