2014-12-31 2 views
0

을 장고 :uploadede 파일 경로가 forms.py에서

settings.py에서
image = forms.ImageField(upload_to="images/") 

:

views.py에서
MEDIA_ROOT = (
os.path.join(os.path.dirname(BASE_DIR), "media") 
) 
MEDIA_URL = '/media/' 

:

c_image = UploadedFile(request.FILES.get('image')) 
pathe=c_image.temporary_file_path 


sql = "INSERT INTO client (name, address, email, gender, country, sub_priod) VALUES \ 
          ('%s', '%s', '%s','%s', '%s', '%s')" % \ 
          (c_name, c_address, c_email, c_gender, c_country, pathe) 

오류 :

Exception Type: AttributeError 
Exception Value:type object 'UploadedFile' has no attribute 'temporary_file_path' 

어떻게해야합니까?

+0

이 - https://docs.djangoproject.com/en/1.7/ref/files/uploads/ 당이 수도 'InMemoryUploadedFile' (기본적으로 파일 <2.5MB)에서 발생합니다. 따라서, 거기에 없을 수도있는 "경로"를 유지할 수 없습니다 :-) –

+2

이 코드의 요점은 무엇입니까? 당신은 django에서 이미 사용할 수있는 기능을 다시 만들고 있습니다. –

+0

내가 먼저 견인 라인을 바꿔야한다고 생각해.하지만, 무엇을 바꿀 지. 잘 모르겠습니다 ... –

답변

2

당신은 다음과 같이 시도 할 수 있습니다 : 전혀 파일 경로를하지 않을 수

from django.http import HttpResponseRedirect 
from django.shortcuts import render 
from .forms import ModelFormWithFileField 

def upload_file(request): 
    if request.method == 'POST': 
     form = ModelFormWithFileField(request.POST, request.FILES) 
     if form.is_valid(): 
     # file is saved 
      form.save() 
      return HttpResponseRedirect('/success/url/') 
    else: 
     form = ModelFormWithFileField() 
    return render(request, 'upload.html', {'form': form}) 
관련 문제