简体   繁体   中英

Django Images Display

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 eg /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 eg 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!

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.

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.

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