简体   繁体   中英

Redirect to paginated page after form submit - Django

I use paginator to create a series of pages that meet certain criteria.
Each page contains a form that the user can submit.
When the form is submitted, I would like them to stay on/return to the page where they were. How do I do that?
I've searched everywhere and read the docs, but can't find the answer.
I'm sure the problem is in front of the computer, but please help :)

Following is the view that should do this:

def assignment(request, pk):
"""View for each assignment"""
if request.user.is_authenticated():
    #### Get correct articles
    assignment = get_object_or_404(Assignment, pk=pk)
    country = assignment.country.ccode
    start_date = assignment.start_date
    end_date = assignment.end_date
    articles = Article.objects.filter(country=country).filter(pub_date__range=(start_date,end_date))
    paginator = Paginator(articles, 1)

    #### Pagination ####
    page = request.GET.get('page')
    try:
        articles = paginator.page(page)
    except PageNotAnInteger:
        articles = paginator.page(1)
    except EmptryPage:
        articles = paginator(page(paginator.num_pages))

    #### Forms ####
    if request.method == 'POST':
        if 'event' in request.POST:
            event_form = EventRecordForm(request.POST, prefix='event')
            if event_form.is_valid():
                obj = event_form.save(commit=False)
                obj.article = paginator.page(page).object_list[0]
                obj.classified = True
                obj.coder = request.user.coder
                obj.save()
                return HttpResponseRedirect(reverse ('coding:assignment', args=(pk,)))
            relevance_form = RelevanceCodingRecordForm(prefix='relevance')
        elif 'relevance' in request.POST:
            relevance_form = RelevanceCodingRecordForm(request.POST, prefix='relevance')
            if relevance_form.is_valid():
                obj = relevance_form.save(commit=False)
                obj.article = paginator.page(page).object_list[0]
                obj.screened = True
                obj.coder = request.user.coder
                obj.save()
                return HttpResponseRedirect(reverse ('coding:assignment', args=(pk,)))
            event_form = EventRecordForm(prefix='event')
    else:
        event_form = EventRecordForm(prefix='event')
        relevance_form = RelevanceCodingRecordForm(prefix='relevance')


else:
    print ERROR
return render(request, 'coding/assignment.html', 
{'articles':articles,'assignment':assignment,'event_form':event_form,'relevance_form':relevance_form})

Edit:
Related suggested that I append ?next={{request.path}} to my form action.
This doesn't work, however, since the paginator page is not included in the path. Doing what he suggested this happens: http://127.0.0.1:8000/coding/assignment/1/?next=/coding/assignment/1/
I did give paginator a second look though, and think that ?page={{ articles.number }} should work, because in the example in the docs they do a link like this:
<a href="?page={{ articles.next_page_number }}">next</a> However, this doesn't work.
Any other ideas?

I think the page parameter should be in the request.path, so try appending this to the submit action in the template:

?next={{request.path}}

and make sure you have request as a template context processor in your settings.py file. (See https://stackoverflow.com/a/1711592/837845 for details)

So to everyone struggling with redirecting to paginated page what i did was first for view of main page i store this page's pagination in session:

    request.session['previous_page'] = request.path_info + "?page=" + request.GET.get("page", '1')

Now in view that receives POST request from form return HttpResponseRedirect:

return HttpResponseRedirect(request.session['previous_page'])

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