pagination - Redirect to paginated page after form submit - Django -
i use paginator create series of pages meet criteria.
each page contains form user can submit.
when form submitted, them stay on/return page were. how do that?
i've searched everywhere , read docs, can't find answer.
i'm sure problem in front of computer, please :)
following view should this:
def assignment(request, pk): """view each assignment""" if request.user.is_authenticated(): #### 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 append ?next={{request.path}}
form action.
doesn't work, however, since paginator page not included in path. doing suggested happens: http://127.0.0.1:8000/coding/assignment/1/?next=/coding/assignment/1/
did give paginator second though, , think ?page={{ articles.number }}
should work, because in example in docs link this:
<a href="?page={{ articles.next_page_number }}">next</a>
however, doesn't work.
other ideas?
i think page parameter should in request.path, try appending submit action in template:
?next={{request.path}}
and make sure have request template context processor in settings.py file. (see https://stackoverflow.com/a/1711592/837845 details)
Comments
Post a Comment