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

2
  • 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. Commented Jan 27, 2015 at 7:41
  • this is my settings.py ..now can u tell me what changes should i do to make it work..?? Commented Jan 27, 2015 at 8:16

1 Answer 1

1

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

Check if you have included enctype in your form.

Sign up to request clarification or add additional context in comments.

2 Comments

i did include enctype...but still there is this error and i dont know how to solve it..:(...any other suggestions..??
The permission denied error is because of write permission to your upload path. check this [link] (stackoverflow.com/questions/11392309/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.