0

I want to make a Notes Platform where teachers can upload Notes and others can access it . The problem is whenever I am uploading the image it is going to the same folder and I am not able to categorise it according to year,branch,subject, and unit-wise. Since all the media files are uploaded to the same folder I am unable to get the logic how will i able to fetch the media/image/doc file when a user query for the notes. my model form is :-

class Note(models.Model):
  year_choices = (
        ( 1  , 'First' ),
        ( 2  , 'Second'),
        ( 3  , 'Third' ),
        ( 4  , 'Fourth')
  )
  branch_choices = (
        ( 'IT','IT'  ),
        ( 'EE','EE'  ),
        ( 'CSE','CSE'),
        ( 'EC','EC'  ),
        ( 'ME','ME'  ),
        ( 'CE','CE'  ),
  )
  unit_choices = ((1,'1'),(2,'2'),(3,'3'),(4,'4'),(5,'5'),
                 (6,'6'),(7,'7'),(8,'8'),(9,'9'),(10,'10'))
  branch = models.CharField(max_length=55,choices=branch_choices)
  year = models.IntegerField(choices = year_choices)
  subject_name = models.CharField(max_length = 15)
  unit = models.IntegerField(choices=unit_choices)
  location = 'images'
  picture = models.ImageField(upload_to = location)

My notes uploading form and searchform(for searching) is field is :-

class notesform(forms.ModelForm):
    class Meta:
        model = Note
        fields = [ 'year','branch','subject_name','unit','picture' ]

class searchform(forms.ModelForm):
    class Meta:
        model = Note
        fields = [ 'year','branch','subject_name','unit' ]

My notes adding function logic is :-

def addNotes(request):
    if request.user.is_authenticated():
        if request.method == "POST":
            form = notesform(request.POST,request.FILES)
            if form.is_valid():
                profile = Note()               
                profile.year = form.cleaned_data["year"]
                profile.branch = form.cleaned_data["branch"]
                profile.subject_name = form.cleaned_data["subject_name"]
                profile.picture = form.cleaned_data["picture"]
                post = form.save(commit=False)
                post.save()
                return redirect('Notes', pk=post.pk)
        else:
            form = notesform()
        return render(request, 'newNotes.html', {'form': form})
    else:
        return redirect('/accounts/login/')

I want to make upload in such a way that when the teacher fill the form and send upload the media the files upload according to the fields data he will be filling in. For ex:- Teacher fill the form as "First year" then "CSE branch" then "Data Structure" ad "Unit 1" , then it goes to the dynamic media folder "media/images/1/CSE/DATA_STRUCTURE/UNIT_1/".So that i can easily implement the search query .

If it can not applied in this way please suggest some other ways.

Abhimanyu
  • 725
  • 1
  • 6
  • 20
  • Check http://stackoverflow.com/questions/1190697/django-filefield-with-upload-to-determined-at-runtime. You can take the model instance when saving then return the file path according to your needs. – alfredo138923 Feb 24 '17 at 17:44

1 Answers1

0

The problem is whenever I am uploading the image it is going to the same folder and I am not able to categorise it according to year,branch,subject, and unit-wise.

Instead of creating a new directory for very unit (which is ultimately going to result in a huge directory tree), you can provide a well structured name to uploaded picture and save it in the images directory itself.

Define a function to generate new name for picture:

def generate_picture_name(instance, filename):
    url = "images/{0}_{1}_{2}_{3}.jpg".format(
        instance.branch, instance.year, instance.subject_name, instance.unit)
    return url

And update picture field to use generate_picture_name for saving image file.

picture = models.ImageField(upload_to = generate_picture_name)

Now the image file will be saved as:
media/images/CSE_1_DATA_STRUCTURE_UNIT_1.jpg

narendra-choudhary
  • 4,582
  • 4
  • 38
  • 58