python 无法在django modelform中上传图像

krcsximq  于 2023-04-19  发布在  Python
关注(0)|答案(1)|浏览(136)

嗨,我在django中创建了一个表单,它有一个imagefield,但我无法上传图像
我的观点。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 })

我的模特。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)

我的设置.py-

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

MEDIA_URL = '/media/'

在confirm.html模板之后,它在/student/confirm/给出错误-OSError
[Errno 13] Permission denied:'/home/details'
请帮助我,因为我不知道如何解决这个问题…
先谢了

mbjcgjjk

mbjcgjjk1#

<form action="your_url" method="post" enctype="multipart/form-data">
检查您是否在表单中包含了enctype。

相关问题