0

I am trying to search and list in between two dates from database ,,but do not know how to do it?? the

models.py

...
class ProductsTbl(models.Model):
    created = models.DateTimeField(editable=False)
...

views.py

def search(request):
    error = False
    if 'q1' and 'q2'in request.GET:
        q = request.GET['q1','q2']
        if not q:
            error = True
        else:
            books = ProductsTbl.objects.filter(created__range=q)
            return render(request, 'search_results.html',
                {'books': books, 'query': q})
    return render(request, 'search_form.html', {'error': error})

I have look up stackoverflow here but do not understand how to do it

shoud I add one more DateTimeField in models.py to help search??

actually I can do it in python manage.py shell, but how can I make it works on the web?? enter image description here

my

search_form.html

<form action="" method="get">
<table style="width:30%">
<tr>
<td><input type="text"  class="datepicker" name="q1"> </td>
<td style="width:5%"> between </td>
<td><input type="text"  class="datepicker" name="q2"></td>
<br>
<td><input type="submit" value="Search"></td>
</tr>
</table>
</form>

the

search_results.html

{% if books %}
<p>Found {{ books|length }} product{{ books|pluralize }}.</p>
<ul>
{% for book in books %}
<li>{{ book.created }}</li>
<h2><a href="{% url 'thing_detail' slug=book.slug %}">{{ book.name }}</a></h2>
{% endfor %}
</ul>
{% else %}
<p>No products matched your search criteria.</p>
{% endif %}

if only filter for one date it is ok for me to use this

views.py

def publish(request):
    error = False
    if 'q' in request.GET:
        q = request.GET['q']
        if not q:
            error = True
        else:
            books = ProductsTbl.objects.filter(release__icontains=q)
            return render(request, 'publish_results.html',
                {'books': books, 'query': q})
    return render(request, 'publish_form.html', {'error': error})

enter image description here

however ,,,when comes between two dates range,,,I do not know how to do it

Community
  • 1
  • 1
Joe Lin
  • 613
  • 2
  • 11
  • 33

3 Answers3

5

You are almost there, you just need to use the __range test properly.

First, convert your strings to datetimes, something like:

import datetime

date_from = datetime.datetime.strptime(request.GET['q1'], '%Y-%m-%d')
date_to = datetime.datetime.strptime(request.GET['q2'], '%Y-%m-%d')

The challenge here will be to ensure the string is in the correct format (e.g. I've used '%Y-%m-%d' in my example above, like '2016-04-22').

Second, the error shown in your screenshot is because the datetimes you have created do not have a timezone specified, but the values in the database do have a timezone. Python is showing an error because it cannot compare the two.

You can read about this in the documentation and in this answer here.

Finally, apply the range as follows:

books = ProductsTbl.objects.filter(created__range=(date_from, date_to))
Community
  • 1
  • 1
Jon Combe
  • 176
  • 1
  • 5
2

Use this query to get the results for two date ranges...

First get the two dates from search query string and apply here.

ProductsTbl.objects.filter(created__range=["2016-04-06", "2016-08-30"])
Community
  • 1
  • 1
  • I add: date_from = datetime.datetime.strptime(request.GET['q1'], 'yy-mm-dd'),,,,and,,,, date_to = datetime.datetime.strptime(request.GET['q2'], 'yy-mm-dd'),,and,, books = ProductsTbl.objects.filter(created__range=['date_from','date_to']),,but does not work – Joe Lin Apr 22 '16 at 06:49
0

thanks for every one who reply to me ,,,I solve the proble,,here is the new

views.py

def search(request):
    error = False
    if 'q1' and 'q2'in request.GET:
        q1 = request.GET['q1']
        q2 = request.GET['q2']
        if not q1:
            error = True
        elif not q2:
            error = True
        else:
            books = ProductsTbl.objects.filter(created__range=(q1,q2))
            return render(request, 'search_results.html',
                {'books': books })
    return render(request, 'search_form.html', {'error': error})

now I can filter and list the range dates

enter image description here

Joe Lin
  • 613
  • 2
  • 11
  • 33