Django直接重定向到同一页面

我有一个呈现测试的render_bfi2()函数。我不希望用户等待7天重新参加考试。我正在使用too_soon_test_request()函数进行检查。但是返回代码的redirect('pages:home_page')行不起作用。我最终看到该消息,但没有重定向。

def too_soon_test_request(request, inv):
    '''Makes sure that there is a specified amount of days between
    two test sessions.
    '''
    now = timezone.now()
    user_id = request.user.id
    response = ItemResponse.objects.filter(user=user_id,
                                           item__inventory__title=inv.title)[0]
    last_time = response.response_time
    time_diff = last_time - now
    if time_diff < datetime.timedelta(days=7):
        messages.warning(request, 'You have to wait to take this test again!')
        return redirect('pages:home_page')


@login_required
def render_bfi2(request):
    '''Renders Big Five Inventory-2 and save responses.'''
    inventory = Inventory.objects.get(code_name='bigfiveinventory2')
    items = Item.objects.filter(inventory=inventory).order_by('item_rank')
    user = request.user
    too_soon_test_request(request, inv=inventory)
    if request.method == 'POST':
        responses = []
        for i in range(1, items.count()):
            item = 'item' + str(i)
            item_response = request.POST[item]
            responses.append(item_response)
        responses = [i.split('_') for i in responses]
        responses = [[int(i) for i in sub] for sub in responses]
        for i in responses:
            current_item = inventory.items.get(item_rank=i[0])
            response = i[1]
            item_response = ItemResponse(item=current_item, user=user,
                                         response_time=timezone.now(),
                                         response=response)
            item_response.save()
        return redirect('pages:home_page')
    else:
        test_title = inventory
    return render(request, 'personality_tests/render_bfi-2.html',
                  {'bfi2_items': items,
                   'test_title': test_title})
评论