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>")
更新的使用,因爲我之後存儲圖像註冊。 此代碼僅將數據庫中的文件名保存爲myphoto
字段,但不會將該圖像保存在Project/project/media/
的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屬於應用報告。 我必須做什麼才能成功存儲圖像
是的,它工作。謝謝你 – Rajadip