2017-04-12 11 views
0

私はdjangoにイマージュを保存したいができません。これは私が試したものです。..イメージをdjangoに保存

upload.html

<form name="saveImage" enctype="multipart/form-data" action="{% url 'Reports:saveimage'%}" method="post"> 
    {% csrf_token %} 
    <div style = "max-width:470px;"> 

     <center> 
      <input type = "file" style = "margin-left:20%;" 
        placeholder = "Picture" name = "picture" /> 
      <input type="submit" value="Store"></input> 
     </center> 
    </div> 

</form> 

model.py

後、私は画像を記憶していますので、
def vo_image_upload(inatance,filename): 
    return os.path.join('Volunteer',filename) 

class Volunteer(models.Model): 
    myphoto = models.ImageField(db_column='MyPhoto', upload_to=vo_image_upload, blank=True, null=True) 

views.py

def saveimage(request): 
    image= request.FILES['picture'] 
    vo = Volunteer.objects.using('NSS_VO').update(myphoto=image). 
    return HttpResponse("<h1>Success</h1>") 

Updateが使用されています登録。 このコードでは、データベースのファイル名はmyphotoフィールドに保存されますが、フォルダの画像はProject/project/media/に保存されません。

プロジェクト/プロジェクト/ url.py

from django.conf.urls import url ,include 
    from django.contrib import admin 
    from django.conf import settings 
    from django.conf.urls.static import static 

    urlpatterns = [ 
     url(r'^ReportsData/', include('Reports.urls')), 
    ] 

    if settings.DEBUG : 
     urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) 
     urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT) 

とupload.html以上、views.py、models.pyは、レポートをアプリケーションに属しています。 イメージを正常に保存するために必要な変更

答えて

2

update()では、データベースでのみ動作し、実際のファイル保存は行われないため、これを行うことはできません。オブジェクトを取得し、ファイルフィールドを割り当てて保存する必要があります。

vo = Volunteer.objects.using('NSS_VO').get(id=whatever) 
vo.myphoto=image 
vo.save() 
+0

はい、それは働いています。ボランありがとう – Rajadip

関連する問題