문제

Django 양식으로보기에서 이미지 업로드 작업을 도와 주시겠습니까?

Models.py

class User_Profile(models.Model):
    user = models.OneToOneField(User, unique=True, related_name='profile')
    photo  = models.ImageField(upload_to = 'profiles/', null=True, blank=True)

forms.py

class ProfileForm(forms.ModelForm):
        class Meta:
            model = User_Profile
            exclude = ('user')

views.py

    if request.method == 'POST':
        profile_form = ProfileForm(request.POST, instance=request.user.profile)

        if profile_form.is_valid():
            profile_form.save()
            return HttpResponseRedirect('/panel/correct/')

    else:
        profile_form = ProfileForm(instance=request.user.profile)

내 HTML 양식에는 이미 포함되어 있습니다 enctype="multipart/form-data"

도움이 되었습니까?

해결책

당신은 아닌 것 같습니다 파일 데이터를 양식에 바인딩합니다.

profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile) 

다른 팁

사용하지 않는 이유는 무엇입니까? 장-아바타 프로젝트 (예를 바탕으로 프로젝트에 사용자 아바타를 추가 할 생각이라고 생각합니다)?

그들은 이미 이미지를 처음 표시하기 전에 이미지를 크기로 만드는 추가 태그가있는 깔끔한 솔루션을 가지고 있습니다. 원본 이미지를 저장하고 웹 사이트에서 수락하려는 이미지 크기를 정의하고 나머지는 자동으로 수행됩니다.

이것은 단지 다음을 따르는 문제입니다 문서.

게시물에서 올바른 양식 초기화를 사용하지 않습니다. 특히 당신은 누락되었습니다 request.files 매개 변수 :

 form = ProfileForm(request.POST, request.FILES)

위의 업로드 후 업로드 된 파일은 파일 배열에서 검색 할 수 있습니다.

 photo_file = request.FILES['photo']
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top