2016-04-10 7 views
0

モデルなしのフォームから画像をアップロードしたいと思います。テンプレートでモデルなしのdjangoフォーム

を::私が作るしようとした ビューで

Upload image 
    <form enctype="multipart/form-data" action="" method="post">{% csrf_token %} 
     <input type="file" name="myfile" id="myfile" /> 
     <input type="submit" value="Upload image"> 
    </form> 

det.html、私が持っている:

def fileupload(request): 

    return responsewrapper('personne/det.html', locals(),request) 

def handle_uploaded_file(f): 
    filename = file._get_name() 
    destination = open('/personne/static/personne/%s'%filename, 'wb+') 
    for chunk in f.chunks(): 
     destination.write(chunk) 
    destination.close() 

def submitfilecontent(request): 
    handle_uploaded_file(request.FILES['myfile']) 
    return HttpResponseRedirect("/successupload") 

をエラーがある:メソッドは許可されていません(POST) :/ FR /検出/

私のURL:

urlpatterns += i18n_patterns(
    url(r'^admin/', admin.site.urls, name="admin"), 
    url(r'^$', views.IndexView.as_view(), name="homepage"), 
    url(r'^detect/$', views.DetectView.as_view(), name="detection"), 
    url(r'^login/$', auth_views.login, name="login"), 
    url(r'^logout/$', auth_views.logout, name="logout"), 
    url(r'^register/',views.addUser, name='register'), 

)+ i18n_patterns('', (r'^(?P<path>.*)$', 'django.views.static.serve', {'document_root': MEDIA_ROOT, 'show_indexes': True})) 

更新:urls.pyで

は、私が追加:

<form enctype="multipart/form-data" action="/submitfilecontent/" method="post"> 

新しいエラー:MultiValueDictKeyError:テンプレートで

url(r'^submitfilecontent/$', submitfilecontent, name="submit-file-content"), 

" 'myfileを'"

+0

こと。 CORSやcsrfのようなものやスラッシュのない単純なURLが必要です。 urls.pyにURLを表示できます – AceLearn

+0

また、フォームタグのaction属性にurlがありません。 – AceLearn

+0

私はアクションに何を入れるべきですか? – Lilia

答えて

0

Views.pyアクションURLを追加します。私は問題は認証の問題のようだと思う

def submitfilecontent(request): 
    ext_allowed = ['gif', 'jpg', 'jpeg', 'png'] 
    today = datetime.datetime.today() 
    save_dir = 'personne/static/personne/%d/%d/%d/' % (today.year, today.month, today.day) 
    save_path = os.path.join(settings.MEDIA_ROOT, save_dir) 
    save_url = os.path.join(settings.MEDIA_URL, save_dir) 

    if request.method == 'POST': 
     file = request.FILES['myfile'] 
     ext = file.name.split('.').pop() 
     if not os.path.isdir(save_path): 
      os.makedirs(save_path) 

     new_file = '%s.%s' % (int(time.time()), ext) 

     destination = open(save_path+new_file, 'wb+') 
     for chunk in file.chunks(): 
      destination.write(chunk) 
     destination.close() 
     return HttpResponse("Upload Succsefull to URL:%s" % (save_url+new_file) ) 
    else: 
     raise Http404 
1

してくださいURLにURL(下)を追加します。

url(r'^submitfilecontent/$', submitfilecontent, name="submit-file-content"), 

とHTMLで、

<form enctype="multipart/form-data" action="/submitfilecontent/" method="post">{% csrf_token %} 
+0

私の知識が不足して申し訳ありませんが、私は今このエラーがあります:handle_uploaded_file(request.FILES ['myfile'])#エラーがここにスローされます。 – Lilia

+0

エラーは何ですか? – AceLearn

+0

MultiValueDictKeyError: "'myfile'" – Lilia

関連する問題