Django template loops unwantedly fetching items in random order - django

I am using Django 2.2 and psql 10.8 on Ubuntu 18.04.1.
I have a collection of items that I want to iterate over and render the results in a template.
They are expected to be rendered in exactly the order that they have been created in the database (by pk). However, they seem to be rendered in a random order instead.
The problem does not occur when using sqlite.
I have not found the solution for this problem; reverse iterating through the objects results also not in the desired behaviour. A simple portion of the code would be:
<div class="row">
<ul class="tabs">
{% for category in categories %}
<li class="tab col s3">{{category}}</li>
{% endfor %}
</ul>
</div>
Say I have created four categories A, B, C, D;
when using sqlite in dev, they would be rendered in that order on the frontend page.
With psql, I am seeing an unordered result.
Any help in the right direction is appreciated!

Related

Django - How do I change the sort order of search results while still on the page?

I have a list of results from a user's search and I want the user to be able to re-order them
as they wish. It is simple enough to pre-sort the queryset on the backend, in views.py which is what every Google
search brings up on the topic. But I need to have this done by the user. On the frontend. This is
usually done with a dropdown with options allowing alphabetical sort A-Z or sort by date added or so on.
I can't find an answer with Google search or a single tutorial that covers it, yet I see it used almost everywhere.
Does the solution involve ajax? How would I use ajax to do it? Is there a py module that does this in Django?
I am rendering the search results something like this
{% for stor in stories %}
<div>
<span class="story_block stock_bg">
<a href="{{stor.get_absolute_url}}">
<div class="story_con_block">
<p class="s_t">{{stor.title}}</p>
<p>by <strong>{{stor.author.username}}</strong></p>
<p>{{stor.summary}}</p>
</div>
</a>
</span>
</div>
{% endfor %}
It can be done with javascript if you are rendering the list with javascript. as javascript can manipulate the DOM , so you can sort it with javascript code and add click event to these sorting functions

Django - the effect on page load speed of using include tag inside for loop

The odds are that this question will be banned, because this forum seems to me a site for "why it doesn't work"-type of questions, rather than "is it a good idea to do what I do" ones. And yet, I am very much concerned about preserving DRY-ness in my code.
I have a Django template which looks like this:
<ol id = 'task_list'>
{% for item in qs %}
{% include 'list_item.html' with item=item %}
{% endfor %}
</ol>
list_item.html:
<li>
{{item}}
</li>
The advantage (at least, for me) of this code: it positively affects DRYness when I have a ajax code which posts a request for creating new items of the list and renders them on the client side subsequently:
JS:
$.post('my_view_url', function(response)
$('#container').append(response);
Django view:
def my_view(response)
#...
return render_to_response('list_item.html',....)
This way, list_item.html helps me use the same HTML code for both initial rendering of existing elements and client-side rendering of newly created items.
The disadvantage is that {% include %} is known to be rather slow.
The question: Is this code pattern not a performance killer in case of paginated rendering of large arrays of data ?
Additional note:
AFAIK, {% block %} is faster than {% include %}. But I've got no idea how to rewrite the code pattern using block.

Django put forloop.counter0 into custom tag argument list possible?

So I am using django-google-charts to generate bar chart. Here the chart is drawn in the "out" div.
However I want to generate more than one charts in one page, within different div, for example chart0 in "out0", chart1 in "out1", chart2 in "out2".
{% load googlecharts %}
<div id="out"></div>
{% googlecharts %}
{# some code here #}
{% graph "out" "out_data" "out_options" %}
{% endgooglecharts %}
So I tried to modify the {% graph "out" "out_data" "out_options" %}, my intention was
{% graph out|add:{{ forloop.counter0 }} "out_data" "out_options" %} so that graph output source will be replaced by out0, out1, out2, etc.
However the use of {{}} inside {{%%}} is not allowed. Plus the graph tag will take whatever at the first place as a string parameter as separated by comma.
Is it possible to solve the problem on the template side?
Thanks.
{% graph "out"|add:forloop.counter0 "out_data" "out_options" %}
no brackets inside {% %} and it should work
also: |add concatenates strings, you were concatenating a variable (out instead of "out")
the above code should now work as you expect :)
Finally I worked out fine using only the google chart tools Javasciprt API. It allows multiple charts on one page. Similar question here. The last answer is the solution.

Django: How do I get the number of elements returned in a database call?

This seems to me like a very simple question, but I can't seem to find the answer.
All I need to do is determine the number of objects returned by a database query.
The specific circumstance is this: I have a model named Student. This model has a ManyToManyField member named courses_current, which relates to a table of Course models. When I pass my Student instance to a template, I want to be able to do something like the following (the syntax may not be exact, but you'll get the basic idea):
<div id="classes">
{% if student.classes_current.all.size == 0 %}
<h1> HEY! YOU AREN'T TAKING ANY CLASSES! REGISTER NOW!
{% else %}
Here are your courses:
<!-- ... -->
{% endif %}
</div>
Now, I'm fairly certain that X_set.all.size is not a real thing. In the manage.py shell I can just use len(student.classes_current.all()), but I don't know of any way to use built-in functions, and "dictionary-like objects" don't have .size() functions, so I'm at a loss. I'm sure there's a very simple solution (or at least I hope there is), but I can't seem to find it.
{{ student.classes_current.all.count }} but be warned that it doesn't fetch the objects so you will need to do a separate query if you want to loop over them.
If you need loop over the classes for tag has way to get what you need.
{% for cl in student.current_classes.all %}
{{ cl }}
{% empty %}
<h1>Hey! ...</h1>
{% endfor %}
Documentation https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#for-empty

Django template: Ordering dictionary items for display

I am making a website that displays a user's chosen youtube videos. A user can enter a comment for each video.
I want to display (in this order):
User comment
video title
I have already made the view and have created the following list of dictionary items. Each one represents one video. I send this to my html page:
[
{"my_own_object": vid_obj1, "youtube_obj": obj1}
{"my_own_object": vid_obj2, "youtube_obj": obj2}
]
"youtube_obj" is the object supplied by youtube, which contains the url, title, rating, etc. "my_own_object" contains the user's comments as well as other information.
I iterate over the list and get one dictionary/video. That's fine. Then I need to display the video's information:
{% for key,value in list.items %}
{% if key = "my_own_object" %}
<div>
<p>{{value.user_comment}}</p>
</div>
{% endif %}
{% if key = "youtube_obj" %}
<div>
<p> {{value.media.title.text}}</p>
</div>
{% endif %}
{% endfor %}
This works, except that, because I cannot determine the dictionary order, I might end up with:
Video title
User comment
I thought I could get around this by assigning variables (and then printing the values in the proper order), and am still reeling from the fact that I cannot assign variables!
So, how can I get around this? Can I pluck the key/value that I need instead of iterating over the dictionary items - I tried looking for ways to do this, but no luck. Any other ideas? (I need to pass both video objects as I may need more information than comment and title, later.)
You can use dictionary keys directly:
{% for item in list %} {# PS: don't use list as a variable name #}
<p>{{item.my_own_object.user_comment}}</p>
<p>{{item.youtube_obj.media.title.text}}</p>
{% endfor %}
Just iterate twice. Once for the videos, and once again for the comments. Or, split them into their own dictionaries that are passed through to the template. That's probably a better option, as you avoid iterating twice over the dict. For very small dicts this will be no problem. For larger ones, it can be a problem.