Views & Template issue

37 views
Skip to first unread message

Mikko Meronen

unread,
Sep 9, 2018, 7:48:40 AM9/9/18
to django...@googlegroups.com

Hi again,

I'm trying to include two views in one html template, however the latter(bolded) view doesn't work.

The first one works well and I can see the items created within last 15 minutes in my webpage. However when I want to see the older items (bolded html part and index1 in views.py), I don't get anything, not even an error. Do I need to add something to other files in Django or is this even possible?

- Mikko

                                <div class="col-lg-8">
<ul>
{% for news in newstest %}
<h4> <a href="{{ news.url }}" >{{ news.title }}</a> </h4>
<p>{{ news.publisher }} {{ news.section }} {{ news.country }}</p>
<hr class="m-y-md" />
{% endfor %}

<p> +15 min </p>

{% for news in newstest1 %}
<h4> <a href="{{ news.url }}" >{{ news.title }}</a> </h4>
<p>{{ news.publisher }} {{ news.section }} {{ news.country }}</p>
<hr class="m-y-md" />
{% endfor %}
</ul>
</div>



def index(request):
    newstest = NewsData.objects.filter(created__gt= time.time() - 900).order_by('-created')
    args = {'newstest': newstest}
    return render(request, "news/index.html", args)

def index1(request):
    newstest1 = NewsData.objects.filter(created__lt= time.time() - 900).order_by('-created')
    args1 = {'newstest1': newstest1}
    return render(request, "news/index.html", args1)


Jason

unread,
Sep 9, 2018, 9:48:49 AM9/9/18
to Django users
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})

Mikko Meronen

unread,
Sep 9, 2018, 10:22:02 AM9/9/18
to django...@googlegroups.com
Thank you.

-Mikko

--
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...@googlegroups.com.
To post to this group, send email to django...@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.
For more options, visit https://groups.google.com/d/optout.

Mikko Meronen

unread,
Sep 11, 2018, 7:46:17 PM9/11/18
to django...@googlegroups.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>

Andréas Kühne

unread,
Sep 12, 2018, 4:37:54 AM9/12/18
to django...@googlegroups.com
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


Mikko Meronen

unread,
Sep 12, 2018, 6:49:12 AM9/12/18
to django...@googlegroups.com
Thank you, works well now :)

Reply all
Reply to author
Forward
0 new messages