Сделай это в одном представлении; Есть три варианта, которые необходимо учитывать:
- Пользователь только что прибыл на страницу - отправьте ему пустую форму (InitialImageForm)
- Пользователь отправил изображение - не сохраняйте его, вместо этого создайте 4 новых изображения и передайте их обратно с новой формой (ProcessedImageForm), позволяющей выбрать одно из сгенерированных изображений
- Пользователь отправил окончательную форму вместе с исходным изображением и выбранным обработанным изображением - сохраните все это
Этот код немного грязный, но он дает вам идею. Вам нужно было бы написать две формы самостоятельно, а также, вероятно, модель для представления изображения и обработанного / выбранного изображения
from PIL import Image
def view(self, request):
if request.method=='POST':
# We check whether the user has just submitted the initial image, or is
# on the next step i.e. chosen a processed image.
# 'is_processing_form' is a hidden field we place in the form to differentiate it
if request.POST.get('is_processing_form', False):
form = ProcessedImageForm(request.POST)
if form.is_valid():
# If you use a model form, simply save the model. Alternatively you would have to get the data from the form and save the images yourself.
form.save()
return HttpResponseRedirect('/confirmation/')
elif form.POST.get('is_initial_form', False) and form.is_valid():
form = InitialImageForm(request.POST)
if form.is_valid():
# Get the image (it's not saved yet)
image = Image.open(form.cleaned_data.get('image').url)
processed_list = []
# Create your 4 new images and save them in list
...
# You can show a template here that displays the 4 choices
return direct_to_template(request,template = 'pick_processing.html',extra_context = { 'image' : image, 'processed_list':processed_list })
else:
# When the user arrives at the page, we give them a form to fill out
form = ImageSubmitForm()
return direct_to_template(request,template = 'upload_image.html',extra_context = { 'form' : form })