From 3d9b708b8e8e087292360e1526165fef2b82ed4c Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Thu, 27 Apr 2017 14:25:39 +0100 Subject: [PATCH] Preserve multi-value dict items on page preview Fixes #3564. Convert QueryDict to a plain dict when writing to session and vice versa when retrieving, so that multi-value fields are preserved. --- wagtail/wagtailadmin/views/pages.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/wagtail/wagtailadmin/views/pages.py b/wagtail/wagtailadmin/views/pages.py index 94a62cf672..9f93fa0495 100644 --- a/wagtail/wagtailadmin/views/pages.py +++ b/wagtail/wagtailadmin/views/pages.py @@ -6,6 +6,7 @@ from django.core.exceptions import PermissionDenied from django.core.urlresolvers import reverse from django.db.models import Count from django.http import Http404, HttpResponse, JsonResponse +from django.http.request import QueryDict from django.shortcuts import get_object_or_404, redirect, render from django.template.loader import render_to_string from django.utils import timezone @@ -581,12 +582,23 @@ class PreviewOnEdit(View): page = self.get_page() form_class = page.get_edit_handler().get_form_class(page._meta.model) parent_page = page.get_parent().specific - post_data, timestamp = self.request.session[self.session_key] + post_data_dict, timestamp = self.request.session[self.session_key] + + # convert post_data_dict back into a QueryDict + post_data = QueryDict('', mutable=True) + for k, v in post_data_dict.items(): + post_data.setlist(k, v) + return form_class(post_data, instance=page, parent_page=parent_page) def post(self, request, *args, **kwargs): # TODO: Handle request.FILES. - request.session[self.session_key] = request.POST, time() + + # Convert request.POST to a plain dict (rather than a QueryDict) so that it can be + # stored without data loss in session data + post_data_dict = dict(request.POST.lists()) + + request.session[self.session_key] = post_data_dict, time() self.remove_old_preview_data() form = self.get_form() return JsonResponse({'is_valid': form.is_valid()})