views:

30

answers:

1

I would like to have a "pagination" where I would have one image per page. My current code is pasted below. Unfortunatley now I get ALL images on EVERY pagination. Which is already a step in the right direction but not quite what I want.

How can I just have one image per page e.g Images 1 of my os.listdir and below that a link to the next Image in that os.listdir?

views.py

def p_main_page(request):
    stimuli_list=os.listdir('/Users/Me/Images')
    p = Paginator(stimuli_list, 1)
    urllist = ['/site_media/Images/%s' % url for url in stimuli_list]
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1
    try:
        stimuli = p.page(page)
    except (EmptyPage, InvalidPage):
        stimuli = p.page(p.num_pages)
    return render_to_response('stimulilist.html', {"stimuli": stimuli,
                                                "urllist": urllist})

template:

<html>

<head>
<title> Stimuli </title>
</head>

<body>

<p>
{% for url in urllist %}

<img src='{{ url }}' />

{% endfor %}

</p>

<div class="pagination">
    <span class="step-links">
        {% if stimuli.has_previous %}
            <a href="?page={{ stimuli.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Page {{ stimuli.number }} of {{ stimuli.paginator.num_pages }}.
        </span>

        {% if stimuli.has_next %}
            <a href="?page={{ stimuli.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>

<body>
</html>
A: 

You're defining urllist too early in your view code, before you've limited down the list of urls that you loop through in your template. Try:

def p_main_page(request):
    stimuli_list=os.listdir('/Users/Me/Images')
    p = Paginator(stimuli_list, 1)
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1
    try:
        stimuli = p.page(page)
    except (EmptyPage, InvalidPage):
        stimuli = p.page(p.num_pages)
    urllist = ['/site_media/Images/%s' % url for url in stimuli.object_list]
    return render_to_response('stimulilist.html', {"stimuli": stimuli,
                                                "urllist": urllist})

Incidentally, you might be better off using {{ MEDIA_URL }} in your template, rather than hard-coding the path to site-media in your views, and I'd probably not generate urllist in the view at all, and just loop over stimular.object_list in the template.

Dominic Rodger
Thanks a lot for the quick answer but unfortunatley this did not change the problem. I also tried your version: % url for url in stimuli. But this gave me an exception.
MacPython
Can you explain how and where exactly I should insert {{ MEDIA_URL }}
MacPython
@MacPython - What do you get if you print out `stimuli.object_list`] right before the line where `urllist` is assigned?
Dominic Rodger
Sorry. I dont understand the question. I thought object_list is when I have to query data from a database. By print out what exactly do you mean? Sorry, for my ignorance. Thanks again for your help.
MacPython
Sorry. Did not see the change you made. This did it for me:% url for url in stimuli.object_listFantastic. You are the master!
MacPython