| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「Django Fileアップロード例」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
(ページの作成:「==Django Fileアップロード例== [Django][Python] ===fileuptest/views.py=== from django.http import HttpResponse from django.template import Context, loader f…」)
 
1行目: 1行目:
 
==Django Fileアップロード例==
 
==Django Fileアップロード例==
[Django][Python]
+
[[Django][Python]]
 
===fileuptest/views.py===
 
===fileuptest/views.py===
 
  from django.http import HttpResponse
 
  from django.http import HttpResponse
32行目: 32行目:
  
 
===template/fileuptest/index.html    ===
 
===template/fileuptest/index.html    ===
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
  <html>
+
  &lt;html&gt;
  <head>
+
  &lt;head&gt;
   <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
+
   &lt;META http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
   <script type="text/javascript">
+
   &lt;script type="text/javascript"&gt;
   </script>
+
   &lt;/script&gt;
  </head>
+
  &lt;/head&gt;
  <body>
+
  &lt;body&gt;
 
   {{msg}}
 
   {{msg}}
   <form enctype="multipart/form-data" action='/fileuptest/' method='POST'>
+
   &lt;form enctype="multipart/form-data" action='/fileuptest/' method='POST'&gt;
     <table>
+
     &lt;table&gt;
 
     {{form}}
 
     {{form}}
     </table>    
+
     &lt;/table&gt;    
     <input type='submit' value=" 添 付 ">
+
     &lt;input type='submit' value=" 添 付 "&gt;
   </form>
+
   &lt;/form&gt;
  </body>
+
  &lt;/body&gt;
  </html>
+
  &lt;/html&gt;
 
===結果===
 
===結果===
 
[[File:0385_fileup_form01.jpg]]
 
[[File:0385_fileup_form01.jpg]]

2020年2月15日 (土) 08:01時点における版

Django Fileアップロード例

[[Django][Python]]

fileuptest/views.py

from django.http import HttpResponse
from django.template import Context, loader
from django.forms import *

def index(request):
    msg = 
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            updir = r'c:\\website\\django\\mysite\\upload\\' + form.cleaned_data['title']
            destination = open(updir, 'wb+')
            upfile = request.FILES['file']
            for chunk in upfile.chunks():
                destination.write(chunk)
            destination.close()
            msg = 'Uploaded.'
    else:
        form = UploadFileForm()

    t = loader.get_template('fileuptest/index.html')
    c = Context()
    c['form'] = form
    c['msg'] = msg
    return HttpResponse(t.render(c))

class UploadFileForm(Form):
    title = CharField(max_length=50)
    file  = FileField()

template/fileuptest/index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <script type="text/javascript">
  </script>
</head>
<body>
  テンプレート:Msg
  <form enctype="multipart/form-data" action='/fileuptest/' method='POST'>
    <table>
    テンプレート:Form
    </table>    
    <input type='submit' value=" 添 付 ">
  </form>
</body>
</html>

結果

0385 fileup form01.jpg