Hi Mikko,

It works correctly the way you have written it. Look at the code you have
written:
t = time.time()

def index(request):
    newstest = NewsData.objects.filter(created__gt= t -
300).order_by('-created')
    newstest1 = NewsData.objects.filter(created__lt= t -
300).order_by('-created')
    args = {'newstest': newstest, 'newstest1':newstest1,* 'newstest2':['Older
than 5 min']*}


You get the time when the server is started (the time.time() function is
only run once) and then you never call that function again.
If you want to check the time every time you load the page you should do it
like this:

def index(request):
    t = time.time()
    newstest = NewsData.objects.filter(created__gt= t -
300).order_by('-created')
    newstest1 = NewsData.objects.filter(created__lt= t -
300).order_by('-created')
    args = {'newstest': newstest, 'newstest1':newstest1,* 'newstest2':['Older
than 5 min']*}

Also you should not use the basic time fileds in python when querying the
database, but use fields that include the current timezone. In that case
you should add this instead:

from django.utils.timezone import now

def index(request):
    t = now()
    newstest = NewsData.objects.filter(created__gt= t -
300).order_by('-created')
    newstest1 = NewsData.objects.filter(created__lt= t -
300).order_by('-created')
    args = {'newstest': newstest, 'newstest1':newstest1,* 'newstest2':['Older
than 5 min']*}

Regards,

Andréas


Den ons 12 sep. 2018 kl 01:46 skrev Mikko Meronen <
mikkovillemero...@gmail.com>:

> Hi, I still have a small issue here and couldn't figure it out.
>
> I want to divide my items based on the time created. The division point is
> 5 minutes and  I want that point to be seen in my webpage. However when I
> made the division point, Django doesn't update it (the items that become
> older than 5 mins do not move to the older group) until I restart the
> server. Otherwise the new items will aggregate to the webpage as supposed.
> Is there anyway in Django to make this work in views.py and templates?
>
> Otherwise I guess I should try to figure out if it is possible in SQL side
> by creating more tables based on the creation time and updating the tables
> there. Now all my items are in a single table.
>
> - Mikko
>
> t = time.time()
>
> def index(request):
>     newstest = NewsData.objects.filter(created__gt= t -
> 300).order_by('-created')
>     newstest1 = NewsData.objects.filter(created__lt= t -
> 300).order_by('-created')
>     args = {'newstest': newstest, 'newstest1':newstest1,* 'newstest2':['Older
> than 5 min']*}
>     return render(request, "news/index.html", args)
>
>
> <div class="container padding">
> <div class="row">
> <div class="col-xs-12 col-sm-12 col-md-9 col-lg-9 col-xl-9">
> <ul>
> {% for news in newstest %}
> <h4> <a href="{{ news.url }}" >{{ news.title }}</a> </h4>
> <p>{{ news.published }} {{ news.publisher }} {{ news.section }} {{
> news.country }}</p>
> <hr class="m-y-md" />
> {% endfor %}
> </ul>
> </div>
> </div>
> </div>
> *<div class="container padding">*
> * <div class="row">*
> * <div class="col-xs-12 col-sm-12 col-md-9 col-lg-9 col-xl-9">*
> * <ul>*
> * {% for c in newstest2 %}*
> * <p> {{c}} </p>*
> * <hr class="m-y-md" />*
> * {% endfor %}*
> * </ul>*
> * </div>*
> * </div>*
> * </div>*
>
> <div class="container padding">
> <div class="row">
> <div class="col-xs-12 col-sm-12 col-md-9 col-lg-9 col-xl-9">
> <ul>
> {% for news1 in newstest1 %}
> <h4> <a href="{{ news1.url }}" >{{ news1.title }}</a> </h4>
> <p>{{ news1.published }} {{ news1.publisher }} {{ news1.section }} {{
> news1.country }}</p>
> <hr class="m-y-md" />
> {% endfor %}
> </ul>
> </div>
> </div>
> </div>
>
> su 9. syysk. 2018 klo 17.21 Mikko Meronen (mikkovillemero...@gmail.com)
> kirjoitti:
>
>> Thank you.
>>
>> -Mikko
>>
>> su 9. syysk. 2018 klo 16.49 Jason (jjohns98...@gmail.com) kirjoitti:
>>
>>> You've defined two views which pass in different querysets to be
>>> rendered.
>>>
>>> so if you hit index, you get the first queryset rendered but not the
>>> second because newstest1 is not rendered at all.  and vice versa.
>>>
>>> solution is to add newstest1 to index and pass both querysets as context
>>> to the render.  eg
>>>
>>> return render(request, 'news/index.html', {'newstest': newstest,
>>> 'newstest1': newstest1})
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-users+unsubscr...@googlegroups.com.
>>> To post to this group, send email to django-users@googlegroups.com.
>>> Visit this group at https://groups.google.com/group/django-users.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-users/6ebc2322-5bb5-4809-a6f3-1adf94e92168%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/6ebc2322-5bb5-4809-a6f3-1adf94e92168%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAGD0jjLOtkTd2PRpM38Z_ypUDJ%2BeEmXcSz640EU_qn1kjMeuFw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAGD0jjLOtkTd2PRpM38Z_ypUDJ%2BeEmXcSz640EU_qn1kjMeuFw%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAK4qSCe659nRerJoDMrhZ4oW_sMFxhSmDGftRxpFXdgaNc%2BjuA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to