0

我使用分页器来创建一系列符合特定标准的页面。
每个页面都包含一个用户可以提交的表单。
提交表单后,我希望他们留在/返回他们所在的页面。我怎么做?
我到处搜索并阅读了文档,但找不到答案。
我确定问题出在电脑前,但请帮忙:)

以下是应该这样做的视图:

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})

编辑:
相关建议我附加?next={{request.path}}到我的表单操作。
但是,这不起作用,因为分页器页面不包含在路径中。做他建议的事情: http://127.0.0.1:8000/coding/assignment/1/?next=/coding/assignment/1/
虽然我确实给了分页器第二次看,并认为这?page={{ articles.number }}应该可行,因为在文档中的示例中,他们做了这样的链接:
<a href="?page={{ articles.next_page_number }}">next</a> 但是,这不起作用。
还有其他想法吗?

4

2 回答 2

1

我认为页面参数应该在 request.path 中,所以尝试将其附加到模板中的提交操作:

?next={{request.path}}

并确保您在 settings.py 文件中有请求作为模板上下文处理器。(详见https://stackoverflow.com/a/1711592/837845

于 2013-05-04T13:04:51.180 回答
0

因此,对于每个努力重定向到分页页面的人来说,我所做的首先是为了查看主页,我将此页面的分页存储在会话中:

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

现在鉴于从表单接收 POST 请求返回 HttpResponseRedirect:

return HttpResponseRedirect(request.session['previous_page'])
于 2020-09-22T13:11:31.463 回答