Django Images Display

2019-09-16 02:25发布

Ok, now I know how to display images on ONE /myurl/ in Django as a list (YEAH!)

But how can I display ONE Image per url with a button to click to the next image etc.

So basically I would want to come to the first /urlstart/:

text and a next button.

brings user to e.g. /urlstart/1

There the first image of a List is displayed and below that a button.

That brings user to /urlstart/2

Here the next image of a list is displayed.

etc.

How does the url.py regex part have to look like so this is possible? How does the view has to be tweaked to get 1 List of Images but multiple urls?

And finally if anybody has implemented this with a down loadable and installable module/library, like e.g. photologue (which unfortunately did not work for me) or any better idea than that I would be really happy to get some insights on how to make this fast and efficient.

Thanks so much to everybody who takes the time to answer!

2条回答
在下西门庆
2楼-- · 2019-09-16 02:55

URL regex could look something like this:

url(r'^urlstart/(?P<image_id>\d*)/?$', 'urlstart', name='urlstart')

View code could look something like this:

def urlstart(request, image_id=0):
  if image_id == 0:
    image = None
  else:
    image = get_object_or_404(Image, image_id)
  next_image = image_id + 1

  return render_to_response('template.html', locals(), context_instance=RequestContext(request)

Template code:

{% if image %}
  <img src={{ image.url }}>
  <a href="/urlstart/{{ next_image }}">Next</a>
{% else %}
  Put your text here.  See first image by clicking <a href="/urlstart/1">here</a>
{% endif %}

Obviously this is simplified. For example, you may want to check to see if there is an image to come next.

Hope this helps.

查看更多
Ridiculous、
3楼-- · 2019-09-16 03:05

Take a look at Pagination which will provide you with Next/Previous links, etc.

Also, it would probably be a good idea to use permalinks and pass the next/previous image in from the view instead of hard-coding the URL into the template.

查看更多
登录 后发表回答