django 2, list passed to template with different nested objects, how to access each object?











up vote
0
down vote

favorite












I'm passing a list to the template that looks like this with real data



list = [[<Conversation: Conversation object(1)>, datetime.datetime,
<QuerySet [{'username: 'admin'}]>]]


I want to loop through this in the template and each loop has access to all three items. I'm not sure the ideal way to do this or if I should format my list differently or pull the data in the views first, serialize it and loop through the raw data in the template.



{% for l in list %}
// Here I should have access to the three objects
{{ l.0.id }} //Should print the id of the conversation object
{{ l.1 }} // Should print off the datetime stamp
{{ l.2.username }} // Should print off admin
{% endfor %}


Any help with be great.










share|improve this question






















  • If you feel like my answer is not adequate, I'm certainly open to improving it with your suggestions if you would like it to function differently
    – robotHamster
    Nov 12 at 6:46










  • Your answer was correct and I was able to do what I needed to to. I did hit another problem related to the M2M field in the Conversation object so I'm working on manipulating your solution to resolve that problem. Apparently Django doesn't pull all of the values in a M2M, just the first one it comes across.
    – David
    Nov 13 at 4:28










  • If you post another question I would be glad to help! :)
    – robotHamster
    Nov 13 at 5:26















up vote
0
down vote

favorite












I'm passing a list to the template that looks like this with real data



list = [[<Conversation: Conversation object(1)>, datetime.datetime,
<QuerySet [{'username: 'admin'}]>]]


I want to loop through this in the template and each loop has access to all three items. I'm not sure the ideal way to do this or if I should format my list differently or pull the data in the views first, serialize it and loop through the raw data in the template.



{% for l in list %}
// Here I should have access to the three objects
{{ l.0.id }} //Should print the id of the conversation object
{{ l.1 }} // Should print off the datetime stamp
{{ l.2.username }} // Should print off admin
{% endfor %}


Any help with be great.










share|improve this question






















  • If you feel like my answer is not adequate, I'm certainly open to improving it with your suggestions if you would like it to function differently
    – robotHamster
    Nov 12 at 6:46










  • Your answer was correct and I was able to do what I needed to to. I did hit another problem related to the M2M field in the Conversation object so I'm working on manipulating your solution to resolve that problem. Apparently Django doesn't pull all of the values in a M2M, just the first one it comes across.
    – David
    Nov 13 at 4:28










  • If you post another question I would be glad to help! :)
    – robotHamster
    Nov 13 at 5:26













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm passing a list to the template that looks like this with real data



list = [[<Conversation: Conversation object(1)>, datetime.datetime,
<QuerySet [{'username: 'admin'}]>]]


I want to loop through this in the template and each loop has access to all three items. I'm not sure the ideal way to do this or if I should format my list differently or pull the data in the views first, serialize it and loop through the raw data in the template.



{% for l in list %}
// Here I should have access to the three objects
{{ l.0.id }} //Should print the id of the conversation object
{{ l.1 }} // Should print off the datetime stamp
{{ l.2.username }} // Should print off admin
{% endfor %}


Any help with be great.










share|improve this question













I'm passing a list to the template that looks like this with real data



list = [[<Conversation: Conversation object(1)>, datetime.datetime,
<QuerySet [{'username: 'admin'}]>]]


I want to loop through this in the template and each loop has access to all three items. I'm not sure the ideal way to do this or if I should format my list differently or pull the data in the views first, serialize it and loop through the raw data in the template.



{% for l in list %}
// Here I should have access to the three objects
{{ l.0.id }} //Should print the id of the conversation object
{{ l.1 }} // Should print off the datetime stamp
{{ l.2.username }} // Should print off admin
{% endfor %}


Any help with be great.







django django-templates






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 4:32









David

155




155












  • If you feel like my answer is not adequate, I'm certainly open to improving it with your suggestions if you would like it to function differently
    – robotHamster
    Nov 12 at 6:46










  • Your answer was correct and I was able to do what I needed to to. I did hit another problem related to the M2M field in the Conversation object so I'm working on manipulating your solution to resolve that problem. Apparently Django doesn't pull all of the values in a M2M, just the first one it comes across.
    – David
    Nov 13 at 4:28










  • If you post another question I would be glad to help! :)
    – robotHamster
    Nov 13 at 5:26


















  • If you feel like my answer is not adequate, I'm certainly open to improving it with your suggestions if you would like it to function differently
    – robotHamster
    Nov 12 at 6:46










  • Your answer was correct and I was able to do what I needed to to. I did hit another problem related to the M2M field in the Conversation object so I'm working on manipulating your solution to resolve that problem. Apparently Django doesn't pull all of the values in a M2M, just the first one it comes across.
    – David
    Nov 13 at 4:28










  • If you post another question I would be glad to help! :)
    – robotHamster
    Nov 13 at 5:26
















If you feel like my answer is not adequate, I'm certainly open to improving it with your suggestions if you would like it to function differently
– robotHamster
Nov 12 at 6:46




If you feel like my answer is not adequate, I'm certainly open to improving it with your suggestions if you would like it to function differently
– robotHamster
Nov 12 at 6:46












Your answer was correct and I was able to do what I needed to to. I did hit another problem related to the M2M field in the Conversation object so I'm working on manipulating your solution to resolve that problem. Apparently Django doesn't pull all of the values in a M2M, just the first one it comes across.
– David
Nov 13 at 4:28




Your answer was correct and I was able to do what I needed to to. I did hit another problem related to the M2M field in the Conversation object so I'm working on manipulating your solution to resolve that problem. Apparently Django doesn't pull all of the values in a M2M, just the first one it comes across.
– David
Nov 13 at 4:28












If you post another question I would be glad to help! :)
– robotHamster
Nov 13 at 5:26




If you post another question I would be glad to help! :)
– robotHamster
Nov 13 at 5:26












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










I would pass it a dict or list of dict



For example:



list = [{"conversation":ConversationObject, "date":datetime.datetime, "user":UserObject}]


Then in your template, can do something like:



{% for item in list%}
item.conversation.id
item.date
item.user
{%endfor%}





share|improve this answer























    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%2f53256007%2fdjango-2-list-passed-to-template-with-different-nested-objects-how-to-access-e%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
    1
    down vote



    accepted










    I would pass it a dict or list of dict



    For example:



    list = [{"conversation":ConversationObject, "date":datetime.datetime, "user":UserObject}]


    Then in your template, can do something like:



    {% for item in list%}
    item.conversation.id
    item.date
    item.user
    {%endfor%}





    share|improve this answer



























      up vote
      1
      down vote



      accepted










      I would pass it a dict or list of dict



      For example:



      list = [{"conversation":ConversationObject, "date":datetime.datetime, "user":UserObject}]


      Then in your template, can do something like:



      {% for item in list%}
      item.conversation.id
      item.date
      item.user
      {%endfor%}





      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        I would pass it a dict or list of dict



        For example:



        list = [{"conversation":ConversationObject, "date":datetime.datetime, "user":UserObject}]


        Then in your template, can do something like:



        {% for item in list%}
        item.conversation.id
        item.date
        item.user
        {%endfor%}





        share|improve this answer














        I would pass it a dict or list of dict



        For example:



        list = [{"conversation":ConversationObject, "date":datetime.datetime, "user":UserObject}]


        Then in your template, can do something like:



        {% for item in list%}
        item.conversation.id
        item.date
        item.user
        {%endfor%}






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 4:52

























        answered Nov 12 at 4:45









        robotHamster

        343115




        343115






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256007%2fdjango-2-list-passed-to-template-with-different-nested-objects-how-to-access-e%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

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python