简体   繁体   中英

Looping through two objects in a Django template

I have an app objects and image objects which are linked to one another (the apps have images).

def index(request):
    latest_apps_list = App.objects.all().exclude(approved=False).order_by('name')[:20]
    app_images = Image.objects.filter(app__in=latest_apps_list).order_by('app__name')[:20]
    t = loader.get_template('apps/index.html')
    c = Context({
        'latest_apps_list': latest_apps_list,
        'app_images': app_images
    })
    return HttpResponse(t.render(c))

Now I want to loop through those images in my template. How would I do that with both variables? I tried using zip(), but this returned mysql errors as it calls for unsupported db queries. Is there another way?

Currently I have:

{% for app in latest_apps_list %}

...{{ app.name }}

{% endfor %}

This works. Of course, it doesn't return the images urls. (I'm using sorl-thumbnail btw.)

UPDATE Perhaps I'm just going about doing this the wrong way. Here's how I have my model :

class App(models.Model):
    name = models.CharField(max_length=200)
    # ...

class Image(models.Model):
   app = models.ForeignKey(App)
   image = models.ImageField(upload_to = "apps")

And my view is in the original part of the post above. It seems like I should somehow be making the app's properties and the image properties all one thing, without the need to zip in the view. Is this possible?

UPDATE 2 I solved this by greatly simplifying how the model is created. Here's what I did in case anyone else is trying to do this.

apps/admin.py : the image object is included as an ordinary field.

class AppAdmin(admin.ModelAdmin):
   fieldsets = [
      ('Basic', {'fields':['name','desc','price','approved','image']}),
      ('Author',        {'fields':['docs_url', 'preview_url']}),
   ]
   list_display = ('name', 'desc', 'price', 'approved')


admin.site.register(App, AppAdmin)

apps/models.py : Just make image part of the app itself. No foreign keys needed.

class App(models.Model):
    name = models.CharField(max_length=200)
    # ...
    image = models.ImageField(upload_to = "apps")

apps/views.py : Now the view just has one object to loop through. No weird sql queries needed.

def index(request):
    latest_apps_list = App.objects.all().exclude(approved=False).order_by('name')[:20]
    t = loader.get_template('apps/index.html')
    c = Context({
        'latest_apps_list': latest_apps_list,
    })
    return HttpResponse(t.render(c))

you should zip them in the view, and pass that zipped object to the template, and then iterate through them.

view:

def index(request):
    latest_apps_list = list(App.objects.all().exclude(approved=False).order_by('name')[:20])
    app_images = Image.objects.filter(app__in=latest_apps_list).order_by('app__name')[:20]
    t = loader.get_template('apps/index.html')
    c = Context({
        'zipped_app_list': zip(latest_apps_list, list(app_images))
    })
    return HttpResponse(t.render(c))

template:

{% for app, image in zipped_app_list %}
    {{ app }}
    {{ image}}
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM