2012-06-21 69 views
0

形式HTML:的ImageField(upload_to = 'MyFolder文件')不保存圖像文件夾 - 'MyFolder文件' 中的django

<form action='/register/' method = 'post'>{% csrf_token %} 
    ... 
    <label>Avatar: </label><input type='file' name='avatar' value='' /><br /> 
    <input type = 'submit' name='submit' value='Sign up' /> 
</form> 

models.py

class Employee(models.Model): 
    ... 
    avatar = models.ImageField(upload_to = 'avatar', blank = True, null = True) 
    ... 

和的views.py

def register(request): 
    success = False 
    message = '' 
    try: 
     newE = Employee.objects.create(... 
           avatar = request.POST['avatar']) 
     success = True 
     message = 'Register successful!' 
     return HttpResponse(json.dumps({'success':str(success).lower(), 'message':message})) 
    except: 
     Employee.objects.filter(email = request.POST['email']).delete() 
     message = 'Can\'t create a new account!' 
     return HttpResponse(json.dumps({'success':str(success).lower(), 'message':message})) 

settings.py

MEDIA_ROOT = '/home/dotcloud/data/media/' 
MEDIA_URL = '/media/' 

當我使用Django管理頁面,圖像將加載和保存: http://training-hongquan156.dotcloud.com/media/化身 /image.png 但是當我使用HTML表單上傳照片,圖像不上傳並保存在文件夾「在頭像',但保存路徑: http://training-hongquan156.dotcloud.com/media/image.png 我無法加載圖片... 有什麼問題嗎?

回答

3

您應該糾正

<form action="/register/" method="post"> 

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

處理上傳的文件與

request.FILES['avatar'] 

然後找到它的dir $upload_to/home/dotcloud/data/media/photo

+0

非常感謝你許多...! –

+0

它有效嗎?您可以選擇它作爲正確答案。 – iMom0

+0

它工作...:D –

相關問題