logo
down
shadow

django template looping through a list


django template looping through a list

By : user2956832
Date : November 22 2020, 03:03 PM
should help you out I am trying to loop through a simple list like , You misnamed context variable in dict. It shoould be:
code :
def listloop(request):
    y = ['John', 'Julie', 'Pat']
    context = {'y': y} # 'y' instead of 'x'
    return render(requst, 'index.html', context)


Share : facebook icon twitter icon
Looping over a list of objects within a Django template

Looping over a list of objects within a Django template


By : kuldeep
Date : March 29 2020, 07:55 AM
this will help A couple of things. There is a method by which you can order your query and just get the first ten entries. It would be more efficient than the loop you have.
The reason your template doesn't work is that you're referring to the list rather than the individual entry. It should be:
code :
{% for entry in entries_list %}
        <i><b>Name:</i></b> {{ entry.name }}<br>
        <img src="/images{{ entry.picture }}" height="300" width="300"></img><br>
        <i><b>Ingredients:</i></b> {{ entry.ingredients }}<br>
        <p><i>{{ entry.description }}</i></p>
        <i><b>Created by:</i></b> {{ entry.user }}<br><br>
{% endfor %}
entries_list = Recipes.objects.order_by('-id')[0:10]
looping through list in django template

looping through list in django template


By : gmuniraj
Date : March 29 2020, 07:55 AM
I wish this help you Have you tried using with? If it's just a syntax issue, this might work:
code :
{% with clothes=babies|key_lookup:'clothes list' %}
    {% for cloth in clothes %}
        ...
    {% endfor %}
{% endwith %}
Django Template: looping through two lists

Django Template: looping through two lists


By : jasmin terrien
Date : March 29 2020, 07:55 AM
will help you I have two lists of Django model objects, which I want to display on a template. list1 is a one-dimensional array, and list1 is a two-dimensional array. In the template, I want to loop through each element in list1, display its value, and then display all the values of the corresponding element in list2. , In the python view you can zip them together.
view.py
code :
def someview(request):
    list1 = ['Andrew', 'Ben,' 'Charles']
    list2 = [[3, 4, 8], [12, 9], [10, 0, 5, 1]]
    zipped_list = zip(list1, list2)
    return render(request, 'base/home.html', {'zipped_list': zipped_list})
<ul>
    {% for item1 in zipped_list %} <- this is now a tuple with the first element being our first item and the second element being a list
    <li>{{ item1.0 }}
        <ul>
            {% for secondItem in item1.1 %}
                <li>{{ secondItem }}</li>
            {% endfor %}
        </ul>    
    </li>
    {% endfor %}
</ul>
Looping through CSV string in Django Template

Looping through CSV string in Django Template


By : user7102007
Date : March 29 2020, 07:55 AM
will help you Create a custom template tag and use it. Use following code to create a new template tag for your work done.
code :
@register.filter(name='split')
def split(value, arg):
    return value.split(arg)
{% with network.hosts|split:"," as hosts_list %}
    {% for host in hosts_list %}
     <h3>{{host}}</h3>
    {% endfor %}
{% endwith %}
Django: looping through filtered list in template

Django: looping through filtered list in template


By : Eustachio
Date : March 29 2020, 07:55 AM
like below fixes the issue Couple of things:
You don't want to put OneToOneField in both Species and SpeciesPage pointing to each other. (see https://docs.djangoproject.com/en/2.2/topics/db/examples/one_to_one/ for details)
code :
class SpeciesPageListView(generic.ListView):
    model = SpeciesPage
    template_name = 'speciespage_list.html'
    queryset = SpeciesPage.objects.filter(species_name__genus__tribe__subfamily__subfamily_name="Pierinae")
    context_object_name = 'pierinae_pages'
urlpatterns = [
    path('', views.SpeciesPageListView.as_view(), name='index'),
]
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org