0

I am using Django 3.1.2

  • On order page I am providing option to user that either he can upload a new image or can select image available.

  • According to his selection to Upload image or select image value is to be stored in Django model field.

If the user uploads the Image it is to be stored in model otherwise, If user selects an existing image its path or image to be saved in model.

Can you please give an example?

1 Answers1

0

Here's an example of the Django docs itself, which you can modify like this and handle the situation with a single form:

from django.http import HttpResponseRedirect
from django.shortcuts import render
from .forms import UploadFileForm
from .models import ModelWithFileField

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            if form.file:
                instance = ModelWithFileField(file_field=request.FILES['file'])#or form.file
                instance.save()
                return HttpResponseRedirect('/success/url/')
            elif form.selected:
                instance = ModelWithFileField(file_field=selected_file(form.selected)) #define a function to fetch the selected file by id or name or whatever you use in your template
                instance.save()
                return HttpResponseRedirect('/success/url/')
            else:
                form = UploadFileForm()
                return render(request, 'upload.html', {'form': form})  #nothing chosen or uploaded


    else:
        form = UploadFileForm()
    return render(request, 'upload.html', {'form': form})

and your form will look something like this:

class UploadFileForm(forms.Form):
    title = forms.CharField(max_length=50)
    file = forms.FileField(required=False)
    selected = forms.IntegerField(required=False)

and If needed you could also customize your form validators. Also if you're willing to, you could switch to DRF and use Serializers instead of Forms which I personally find a bit easier.

Pedram Z
  • 1
  • 1
  • And what if I had only image field in model whaich has datatype Imagefield .Not two different fields "selected" and "file" only one field "temp_img" either for uploaded image or same field for selected image. – Chauhan Mukesh Feb 20 '21 at 15:22
  • Your model here has one ImageField too( named file_field), but your Form has 2 different fields. Then you can fill your model field with either the file uploaded by the user or get the selected file from your own statics by defining a function named selected_file in the code above or whatever you like. depending on how you represent the picture in your template or form ( a url, an id, etc.) – Pedram Z Feb 20 '21 at 15:28
  • I have stored files also in models (https://stackoverflow.com/questions/66226144/display-images-in-select-option-for-selection-django-python) you can see in this question but here I have not handled it in view that how to get that image file or selected file because I don't know how to do it. – Chauhan Mukesh Feb 20 '21 at 15:34