无法在django modelform中上传图片

时间:2015-01-27 07:23:30

标签: python django django-file-upload

嗨我在django中制作了一个有图像场但我无法上传图像的表格

我的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)

我的settings.py -

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

MEDIA_URL = '/media/'

在confirm.html模板之后,它在/ student / confirm /

给出错误-OSError

[Errno 13]许可被拒绝:'/ home / details'

请帮助我,因为我不知道如何解决这个问题...

提前致谢

1 个答案:

答案 0 :(得分:0)

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

检查您的表单中是否包含了enctype。