2017-07-20 15 views
0

私はあるビューから別のビューにイメージオブジェクトを送信しようとしています。1つのビューから別のビューにファイルを送信する方法(djangoセッション)

データを変更せずに、View1からView2にイメージファイルを取得する方法はありますか。 イメージデータを文字列に変換してJSONシリアライズ可能にしようとしましたが、これを行うと文字列を操作する際にいくつかの問題が発生します。だから私は最良の方法は、全体の画像(手付かずの)を送信することになると思った。

次のコードは、エラーを吐く: <InMemoryUploadedFile: image.jpg (image/jpeg)> is not JSON serializable

def View1(request): 

    form = FileForm(request.POST or None, request.FILES or None) 
    if request.method == 'POST': 
     if form.is_valid(): 
      image = request.FILES.get('image') 
      request.session['image_file'] = image 

      return redirect('View2') 

def View2(request): 
    img_string = request.session.get('image_file') 
+0

可能な二重に余分な作業を行う必要がありますhttps://stackoverflow.com/questions/27426594/passing-a-file-from-one-view-to-another-in-django-using-sessions – Bijoy

答えて

0

は、パラメータとして画像を渡してみてください。

def View1(request): 

    form = FileForm(request.POST or None, request.FILES or None) 
    if request.method == 'POST': 
     if form.is_valid(): 
      image = request.FILES.get('image') 

      return redirect('View2', image) 

def View2(request, image=None): 
    img_string = image 

あなたはあなたのurls.py

関連する問題