Loop over Django objects and Bootstrap cards

989 views
Skip to first unread message

valentin jungbluth

unread,
Jan 3, 2019, 8:58:18 AM1/3/19
to Django users
Hello guys,

I would like to use Bootstrap cards in order to create one card by object and add some sub_objects in each one.

For example :

I have an object Publication which could contain one or many sub_objects Document.

Publication object has some attributes : category, title, picture, description ...
Document object has some attributes like title, format, language, ...

I would like to get something like this :

Sans titre.png



For a same category, I create a card by publication and I list all documents for each publication.

This is what I get with my code :

Capture du 2019-01-03 14-07-50.png



As you can see, I should have document n°1 and document°2 in the same card and not two different cards.

This is my code :

   
{% for category in research_categories|dictsort:'name' %}
     
<div class="row">
       
<fieldset>
         
<legend id="category_{{ category.id }}"><span class="name">{{ category }}</span></legend>
       
</fieldset>
      </
div>
   
       
<div class="row">
         
<div class="col-sm-4">
           
{% for element in test_research %}
             
{% if element.publication.category|stringformat:"s"  == category|stringformat:"s" %}
               
{% ifchanged %}
                 
<div class="card" style="width:250px">
                   
<img class="card-img-top" src="{{ element.publication.cover.url }}" alt="Card image">
                   
<div class="card-body">
                     
<h4 class="card-title">{{ element.publication }}</h4>
   
                     
<table class="table table-condensed">
                       
<tbody>
                       
<tr>
                         
<td> {{ element.title }}</td>
                       
</tr>
                        </
tbody>
                     
</table>
                    </
div>
                 
</div>
                {% endifchanged %}
              {% endif %}
            {% endfor %}
          </
div>
       
</div>
   
{% endfor %}


And my view according to this part is :

   
 # By default, display documents
    test_research
= Document.objects.all().order_by('publication__title', 'title', 'language', 'format')


    research_categories
= defaultdict(list)
   
for element in test_research:
        research_categories
[element.publication.category].append(element)


    research_publications
= defaultdict(list)
   
for element in test_research:
        research_publications
[element.publication].append(element)


    kwargs
['test_research'] = test_research
    kwargs
['research_categories'] = research_categories
    kwargs
['research_publications'] = research_publications


How I can do that ?
Thank you !

Pradeep Singh

unread,
Jan 3, 2019, 9:07:38 AM1/3/19
to django...@googlegroups.com
how to fetch image from database to template 

--
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/1658ec0f-9769-4c73-a1dd-d68d773bd88e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

valentin jungbluth

unread,
Jan 3, 2019, 9:11:11 AM1/3/19
to Django users
I used UploadField so I just have to use
{{ object.fieldname.url }}

and it works.

But my question is : How I can gather all documents according to one publication in one card ?

shiva kumar

unread,
Jan 3, 2019, 10:41:26 PM1/3/19
to django...@googlegroups.com
I think use inline div elements in place of block. Block elements makes u to start elements in next line.

--
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.
Reply all
Reply to author
Forward
0 new messages