Django Template and Bootstrap Tables









up vote
0
down vote

favorite












I have a django queryset that looks something like this



Group Label Name
A 1 Jack
A 2 Ryan
B 2 Alice
C 1 Sam
B 1 Mark
...


Basically, what I am attempting to do in my html, is create 6 small tables (2 rows of 3 tables). However, I want the first table to be Group A sorted by Label #, the second to be Group B sorted by Label #, etc.



I could bite the bullet and do 6 different django querysets, but this seems inefficient.



My initial thought was to have some counter in the django template to keep track of whether or not to start a new row. However, I am not sure if there is a way to filter & sort in the django template instead of the view.



Thanks for any help.










share|improve this question





















  • Where is the code you have tried for that?
    – mlegg
    Nov 9 at 16:54














up vote
0
down vote

favorite












I have a django queryset that looks something like this



Group Label Name
A 1 Jack
A 2 Ryan
B 2 Alice
C 1 Sam
B 1 Mark
...


Basically, what I am attempting to do in my html, is create 6 small tables (2 rows of 3 tables). However, I want the first table to be Group A sorted by Label #, the second to be Group B sorted by Label #, etc.



I could bite the bullet and do 6 different django querysets, but this seems inefficient.



My initial thought was to have some counter in the django template to keep track of whether or not to start a new row. However, I am not sure if there is a way to filter & sort in the django template instead of the view.



Thanks for any help.










share|improve this question





















  • Where is the code you have tried for that?
    – mlegg
    Nov 9 at 16:54












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a django queryset that looks something like this



Group Label Name
A 1 Jack
A 2 Ryan
B 2 Alice
C 1 Sam
B 1 Mark
...


Basically, what I am attempting to do in my html, is create 6 small tables (2 rows of 3 tables). However, I want the first table to be Group A sorted by Label #, the second to be Group B sorted by Label #, etc.



I could bite the bullet and do 6 different django querysets, but this seems inefficient.



My initial thought was to have some counter in the django template to keep track of whether or not to start a new row. However, I am not sure if there is a way to filter & sort in the django template instead of the view.



Thanks for any help.










share|improve this question













I have a django queryset that looks something like this



Group Label Name
A 1 Jack
A 2 Ryan
B 2 Alice
C 1 Sam
B 1 Mark
...


Basically, what I am attempting to do in my html, is create 6 small tables (2 rows of 3 tables). However, I want the first table to be Group A sorted by Label #, the second to be Group B sorted by Label #, etc.



I could bite the bullet and do 6 different django querysets, but this seems inefficient.



My initial thought was to have some counter in the django template to keep track of whether or not to start a new row. However, I am not sure if there is a way to filter & sort in the django template instead of the view.



Thanks for any help.







python html django bootstrap-4






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 16:48









a.powell

4181617




4181617











  • Where is the code you have tried for that?
    – mlegg
    Nov 9 at 16:54
















  • Where is the code you have tried for that?
    – mlegg
    Nov 9 at 16:54















Where is the code you have tried for that?
– mlegg
Nov 9 at 16:54




Where is the code you have tried for that?
– mlegg
Nov 9 at 16:54












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Here's what I would do:



  1. Order the queryset the way you want (sorted by group, then by label) by using the order_by operation.

  2. In your template, use the ifchanged conditional to test to see when you have a different group. In that case, you output the table tags as necessary.

Example of item 2 above:



% for object in queryset %
% ifchanged obj.group %
<table>
% endifchanged %
<tr>
<td> obj.label </td>
<td> obj.name </td>
</tr>
% ifchanged obj.group %
</table>
% endifchanged %
% endfor %





share|improve this answer




















  • Thank you! This works, except the last ifchanged doesn't work. I removed that section and added a </table> BEFORE <table> to close the previous table if group has changed. I still have to figure out how to make the columns and rows with bootstrap
    – a.powell
    Nov 9 at 17:25










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230016%2fdjango-template-and-bootstrap-tables%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Here's what I would do:



  1. Order the queryset the way you want (sorted by group, then by label) by using the order_by operation.

  2. In your template, use the ifchanged conditional to test to see when you have a different group. In that case, you output the table tags as necessary.

Example of item 2 above:



% for object in queryset %
% ifchanged obj.group %
<table>
% endifchanged %
<tr>
<td> obj.label </td>
<td> obj.name </td>
</tr>
% ifchanged obj.group %
</table>
% endifchanged %
% endfor %





share|improve this answer




















  • Thank you! This works, except the last ifchanged doesn't work. I removed that section and added a </table> BEFORE <table> to close the previous table if group has changed. I still have to figure out how to make the columns and rows with bootstrap
    – a.powell
    Nov 9 at 17:25














up vote
0
down vote













Here's what I would do:



  1. Order the queryset the way you want (sorted by group, then by label) by using the order_by operation.

  2. In your template, use the ifchanged conditional to test to see when you have a different group. In that case, you output the table tags as necessary.

Example of item 2 above:



% for object in queryset %
% ifchanged obj.group %
<table>
% endifchanged %
<tr>
<td> obj.label </td>
<td> obj.name </td>
</tr>
% ifchanged obj.group %
</table>
% endifchanged %
% endfor %





share|improve this answer




















  • Thank you! This works, except the last ifchanged doesn't work. I removed that section and added a </table> BEFORE <table> to close the previous table if group has changed. I still have to figure out how to make the columns and rows with bootstrap
    – a.powell
    Nov 9 at 17:25












up vote
0
down vote










up vote
0
down vote









Here's what I would do:



  1. Order the queryset the way you want (sorted by group, then by label) by using the order_by operation.

  2. In your template, use the ifchanged conditional to test to see when you have a different group. In that case, you output the table tags as necessary.

Example of item 2 above:



% for object in queryset %
% ifchanged obj.group %
<table>
% endifchanged %
<tr>
<td> obj.label </td>
<td> obj.name </td>
</tr>
% ifchanged obj.group %
</table>
% endifchanged %
% endfor %





share|improve this answer












Here's what I would do:



  1. Order the queryset the way you want (sorted by group, then by label) by using the order_by operation.

  2. In your template, use the ifchanged conditional to test to see when you have a different group. In that case, you output the table tags as necessary.

Example of item 2 above:



% for object in queryset %
% ifchanged obj.group %
<table>
% endifchanged %
<tr>
<td> obj.label </td>
<td> obj.name </td>
</tr>
% ifchanged obj.group %
</table>
% endifchanged %
% endfor %






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 16:57









Jonah Bishop

7,72832957




7,72832957











  • Thank you! This works, except the last ifchanged doesn't work. I removed that section and added a </table> BEFORE <table> to close the previous table if group has changed. I still have to figure out how to make the columns and rows with bootstrap
    – a.powell
    Nov 9 at 17:25
















  • Thank you! This works, except the last ifchanged doesn't work. I removed that section and added a </table> BEFORE <table> to close the previous table if group has changed. I still have to figure out how to make the columns and rows with bootstrap
    – a.powell
    Nov 9 at 17:25















Thank you! This works, except the last ifchanged doesn't work. I removed that section and added a </table> BEFORE <table> to close the previous table if group has changed. I still have to figure out how to make the columns and rows with bootstrap
– a.powell
Nov 9 at 17:25




Thank you! This works, except the last ifchanged doesn't work. I removed that section and added a </table> BEFORE <table> to close the previous table if group has changed. I still have to figure out how to make the columns and rows with bootstrap
– a.powell
Nov 9 at 17:25

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53230016%2fdjango-template-and-bootstrap-tables%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo