2016-11-09 17 views
0

我想將用戶上傳的圖像存儲在views.py中的一個變量中(並對其應用一些opencv操作),然後顯示結果圖像。 我不想將圖像存儲在數據庫中。所以,我不認爲模型對於實現這一點是必要的。如何進一步進行?以下是index.htmlviews.py。對不起,如果有任何編碼錯誤,我是新的Django。顯示上傳/選擇的圖像,而不將它保存在Django的數據庫中

的index.html

<form method="POST" enctype="multipart/form-data">{% csrf_token %} 
    <div class="file-field input-field left"> 
     <div class="btn black-text waves-effect waves-dark" style="background-color: #F5DF73"> 
      <div>Upload</div> 
      <input type="file" name = "input_image" onchange="readURL(this);"> 
     </div> 
     <div class="file-path-wrapper"> 
      {#<input class="file-path validate" type="text">#} 
     </div> 
    </div> 
    {{ image }} 
</form> 

views.py

def upload_pic(request): 

    if request.method == 'POST': 
     form = ImageUploadForm(request.POST, request.FILES) 
     image = form.cleaned_data['image'] 

    return render(request, 'html/index.html', {"image": image}) 

urls.py

urlpatterns = [ 
    url(r'^$', views.index), 
] 
+0

那麼,你似乎已經做到了。你的問題是什麼? –

+0

@DanielRoseman我應該如何在index.html中顯示圖片?無論我迄今爲止所做的事情都不會顯示圖像 – hulkinBrain

+0

圖像需要存儲在您的文件系統中才能夠作爲靜態文件提供。 –

回答

0

我做到了,做這個:

views.py

def process_image(request): 

    # inputImage is the name attribute of the <input> tag in the html 
    inImg = request.FILES["inputImage"].read() 

    encoded = b64encode(inImg) 
    mime = "image/jpg" 
    mime = mime + ";" if mime else ";" 
    input_image = "data:%sbase64,%s" % (mime, encoded)   

    return render(request, "../templates/index.html", {{ "input_image": input }}) 

的index.html:

<input src = '{{ input_image }}' name = 'inputImage' type="text"> 
1

試試這個

form = ImageUploadForm(request.POST) 
image = form.cleaned_data['image'] 
b64_img = base64.b64encode(image.file.read()) 

return render(request, 'html/index.html', {"image": b64_img}) 

在HTML

<img src="data:image/png;base64,{{image}}"> 
+0

你能告訴我什麼應該在表單的動作attr? – hulkinBrain

+0

action屬性是您必須發佈映像的「url」 – itzMEonTV

相關問題