views:

43

answers:

2

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!

+1  A: 

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.

godswearhats
You should use {% url %} template tag to avoid hard coding urls in your templates.
Ofri Raviv
Thanks a million. This is fantastic, I will try immediately. One question right now. The regex part (?P<image_id>\d*)/?$ has to be changed to fit or if I use your code below it will work?
MacPython
Another question where do I mention where my pictures are located on the harddrive?
MacPython
So I tried and 2 things I dont understand/wont work:1. Where do i store the images so Django can read them in? Do I name them 0.jpg, 1.gif, 2.png etc to get the image_id right?2. The regex part: you have "url" at the start. I never saw any regex like this. Usually they start with a "(". And even though I get an error, it seems the regexs are correct like that. What does the url at the start exactly mean?
MacPython
MacPython,Take a look at the Django documentation: http://docs.djangoproject.com/en/1.2/Particularly, look at http://docs.djangoproject.com/en/1.2/topics/http/urls/#topics-http-urls for URLs.
godswearhats
Also, you can call the image whatever you like. Create a django model that includes an ImageField. See the docs on uploaded files for more info (http://docs.djangoproject.com/en/1.2/topics/http/file-uploads/#topics-http-file-uploads)
godswearhats
A: 

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.

Lexo