Take a simple view like this:
def my_gallery(request):
images= ?
t = Template("<html><body>Here my images from XY {{ images }}.</body></html>")
html = t.render(Context({'images': ? }))
return HttpResponse(html)
How do I have to define the variable images/ What do I hav开发者_Python百科e to fill in the Context so that Django displays me:
1 image more than 1 image 1 soundfile more than 1 soundfiles
on the site my_gallery
Thanks!
I'm guessing what you want to know..
#/appname/model.py
from django.db import models
class MyImages(models.Model):
images = models.ImageField(upload_to='/upload_path/')
#/appname/views.py
from appname.models import MyImages
from django.template.context import RequestContext
from django.shortcuts import render_to_response
def my_gallery(request, template='gallery.html'):
obj_images= MyImages.objects.all()
kwvars = {
'images': obj_images,
}
return render_to_response(template, kwvars, RequestContext(request))
#gallery.html
...
{% for image in images %}
{{ images.url }}
{% endfor %}
精彩评论