0

hi i made a form in django which has an imagefield but i am unable to upload images

my views.py-

def fill(request):
    global firstname, lastname, email, roomnumber, hostel, hometown, homestate, gender, year, branch, age, dateofbirth, formdata, photo
    if request.method == 'POST':
        formdata = Studentbiodata(request.POST, request.FILES)
        if formdata.is_valid():
            post = request.POST
            firstname = post['first_name']
            lastname = post['last_name']
            email = post['email']
            roomnumber = post['room_number']
            hostel = post['hostel']
            hometown = post['home_town']
            homestate = post['home_state']
            gender = post['gender']
            year = post['year']
            branch = post['branch']
            age = post['age']
            dateofbirth = post['date_of_birth']
            photo = request.FILES['photo']
            return redirect('/student/confirm')
    else:
        formdata = Studentbiodata()
    return render(request, 'student/fillform.html', {'form': formdata})

def confirm(request):
    if request.method == 'POST':
        if 'confirm' in request.POST:
            data = formdata.save()
            return redirect('/student/lastpage')
        elif 'edit' in request.POST:
            return redirect('/student/fillform')
    return render(request, 'student/confirm.html', {'first_name': firstname, 'last_name': lastname, 'gender': gender, 'date_of_birth': dateofbirth, 'branch': branch, 'year': year, 'hostel': hostel, 'room_number': roomnumber, 'home_town': hometown, 'home_state': homestate, 'email': email, 'photo': photo })

my models.py-

class Biodata(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    gender = models.CharField(max_length=10, choices=gender_choices)
    age = models.PositiveSmallIntegerField(default=0)
    date_of_birth = models.DateField()
    year = models.CharField(max_length=2, choices=year_choices)
    branch = models.CharField(max_length=25, choices=branch_choices)
    room_number = models.PositiveSmallIntegerField(default=0)
    hostel = models.CharField(max_length=15, choices=hostel_choices)
    home_town = models.CharField(max_length=30)
    home_state = models.CharField(max_length=20, choices=state_choices)
    email = models.EmailField(max_length=50)
    photo = models.ImageField(upload_to='',blank=True)

my settings.py-

MEDIA_ROOT = '/home/details/student/media/'

MEDIA_URL = '/media/'

after confirm.html template it is giving error-OSError at /student/confirm/

[Errno 13] Permission denied: '/home/details'

plz help me as i dont know how to solve this problem...

thanks in advance

aquaman
  • 1,523
  • 5
  • 20
  • 39
  • You need to check permissions on your `MEDIA_ROOT` located in `settings.py`. If you haven't set up media configuration you need to do that before you can start uploading images. – haki Jan 27 '15 at 07:41
  • this is my settings.py ..now can u tell me what changes should i do to make it work..?? – aquaman Jan 27 '15 at 08:16

1 Answers1

1

<form action="your_url" method="post" enctype="multipart/form-data">

Check if you have included enctype in your form.

Dan
  • 135
  • 4
  • 13
  • i did include enctype...but still there is this error and i dont know how to solve it..:(...any other suggestions..?? – aquaman Jan 27 '15 at 13:59
  • The permission denied error is because of write permission to your upload path. check this [link] (http://stackoverflow.com/questions/11392309/django-mod-wsgi-oserror-errno-13-permission-denied-static-when-debug-off) – Dan Jan 28 '15 at 04:09