views:

47

answers:

2

As mentioned in the title, my paginator doesn't show anything when I click to go to a page beyond the first. First, let me describe my page in general:
Its function is to get a request input from the user specifying the period interval from which he wants to see a bunch of "call records" along with other filters (this is important). So essentially there's a start and end date from the request and I use it to filter my objects. The link to "page2" is something like: "localhost:8000/?page=2" and redirects to my existing page but without any data. It's obvious now that the link to the next page should include the other parameters such as start_date=xxxx-xx-xx, or else it wouldn't work.

Here's part of my view.py and I took out a lot of lines to make it brief, the code runs fine:

if request.GET:   
    filter_form = ReportFilterForm(request.GET)
    if filter_form.is_valid():
        start = filter_form.cleaned_data["start_date"]
        end = filter_form.cleaned_data["end_date"]  

        #a bunch of omitted lines that use the form to filter
        paginator = Paginator(queryset, 100)

        try:
            page = int(request.GET.get('page', '1'))
        except ValueError:
            page = 1
        try:
            call_logs = paginator.page(page)
        except (EmptyPage, InvalidPage):
            call_logs = paginator.page(paginator.num_pages)
else:
    filter_form = ReportFilterForm()
return render_to_response('xxxx.html', 
                          {'queryset': queryset,
                           'filter_form': filter_form,
                           'call_logs': call_logs, 
                           })

My template xxxx.html, just the paginator section, which is pretty standard, taken from the documentation:

                {% if call_logs.paginator.num_pages %}
                <div class="pagination">
                    <span class="step-links">
                        {% if call_logs.has_previous %}
                            <a href="**{{ SOME_MAGIC_TEMPLATE_VARIABLE_THAT_GETS_CURRENT_ABSOLUTE_URL}}**&?page={{ call_logs.previous_page_number }}"><<</a>
                        {% endif %}
                        <span class="current">
                            Page {{ call_logs.number }} of {{ call_logs.paginator.num_pages }}
                        </span>
                                {% if call_logs.has_next %}
                                      <a href=" **{{ SOME_MAGIC_TEMPLATE_VARIABLE_THAT_GETS_CURRENT_ABSOLUTE_URL}}**&page={{ call_logs.next_page_number }}">>></a>
                                {% endif %}
                    </span>
                </div>
                {% endif %}

My question is how do I get the current window URL using django templates and not javascript? Thank you.

+1  A: 

My question is how do I get the current window URL using django templates and not javascript? Thank you.

it's not necessary the right way to do it, but you can check this post

but i will suggest that you shouldn't mix the filter with the pagination.

rather that you can use AJAX when doing filtering you can create a new function that deal with filtering alone or you can just use the same function and test if request.is_ajax(): , like that when a users filter the contain you will have your filter data (start_date,end_date ) in the URL.

and now when a user want to pass to the next page you already have the filtered argument in the url that you can use to create a queryset that will be pass to the Paginator.

And to deal with the javascript not active you can replace AJAX with a simple POST form and just remember don't mix the filtering with the pagination :)

Hope this will Help :)

singularity
+1  A: 

You could add the full path to the context from the request object if I understand you correctly:

return render_to_response('xxxx.html', 
                          {'queryset': queryset,
                           'filter_form': filter_form,
                           'call_logs': call_logs,,
                           'magic_url': request.get_full_path(),
                           })
sunn0
Before I saw your answer, I did "magic_url" by hand, getting each request and concatenating everything together... I'll go with your solution since it'll get the work done. I should have payed attention to Eclipse's Pydev code completion.
chiurox