2016-08-12 5 views
0

私はformです。Userは、ファイルを翻訳または添付するためのテキストを入力できます。翻訳するテキストがいっぱいになったら、Usertxtというファイルになっているので、そのファイルからtxtファイルを作成したいと思います。TextAreaからテキストに変換するには?

if job_creation_form.is_valid(): 
      cleaned_data_job_creation_form = job_creation_form.cleaned_data 
      try: 
       with transaction.atomic(): 
         text = cleaned_data_job_creation_form.get('text_to_translate') 
         if text: 
          cleaned_data_job_creation_form['file']=create_txt_file(text) 

         Job.objects.create(
           customer=request.user, 
           text_to_translate=cleaned_data_job_creation_form['text_to_translate'], 
           file=cleaned_data_job_creation_form['file'].... 
           ) 
      except Exception as e: 
       RaiseHttp404(request, 'Something went wrong :(') 
      return HttpResponseRedirect(reverse('review_orders')) 

私も同じようtxtファイルの作成について:

with open('name.txt','a') as f: 
    ... 

しかし、多くの問題があることができます - ファイルが保存されているディレクトリ、アップロードが自動的などを扱うファイルの名前

あなたはより良い方法をご存知ですか?要するに

翻訳するテキストが満たされた場合には、偽のtxtファイルがアップロードされているように、それはそれが見えます。

答えて

2

tempfileをおそらく使用しますか?

import tempfile 
tmp = tempfile.TemporaryFile() 
tmp.write("Hello World!\n") 
Job.objects.create(file=File(tmp),...) 

希望、これはそれを返し

+0

おかげで、U、など、それは動作しますが、私はCのような文字を渡すときにエラーが発生したのに役立ちます:例外値を:\t 「ASCII」コーデックは、文字のuをエンコードすることはできません'\ xfa'は24桁目:序数は範囲外(128)です。私はtmp.writeしようとした(unicode(cleaned_data_job_creation_form.get( 'text_to_translate'))))しかし、それは同じエラーです。 –

+1

try http://stackoverflow.com/questions/6048085/writing-unicode-text-to-a-text-file – pleasedontbelong

関連する問題