开发者

Django - Passing image objects to a second view for processing and db saving

开发者 https://www.devze.com 2023-04-10 08:57 出处:网络
Here\'s the use case: My main page has an image upload form.When the form is submitted, a page loads with a 4x4 grid of the uploaded image with different photo filters applied.Clicking on an option s

Here's the use case:

My main page has an image upload form. When the form is submitted, a page loads with a 4x4 grid of the uploaded image with different photo filters applied. Clicking on an option saves the image with the chosen processing to the database.

My question is, how would I go about doing this without saving to the database until the processing has been chosen?

I'm not oppose开发者_运维问答d to using this problem to learn ajax.


Do it in a single view; There are three possibilities it has to take into consideration:

  • The user has just arrived on the page - server them an empty form (InitialImageForm)
  • The user has submitted an image - don't save it, instead create 4 new images and pass them back with a new form (ProcessedImageForm) allowing them to choose one of the generated images
  • The user has now submitted the final form along with the original image, and the chosen processed image - save it all

This code is a bit messy but it gives you the idea. You would need to write two Forms yourself, along with probably a Model to represent the image and the processed/chosen image

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

精彩评论

暂无评论...
验证码 换一张
取 消