「Django Fileアップロード例」の版間の差分
ナビゲーションに移動
検索に移動
(同じ利用者による、間の1版が非表示) | |||
1行目: | 1行目: | ||
− | ==Django Fileアップロード例== | + | ==[[Django Fileアップロード例]]== |
− | [[Django]][[Python]] | + | [[Django]] | [[Python]] | |
− | ===fileuptest/ | + | ===fileuptest/[[vi]]ews.py=== |
− | from django.http import | + | from django.http import Http[[R]]esponse |
from django.template import Context, loader | from django.template import Context, loader | ||
from django.forms import * | from django.forms import * | ||
25行目: | 25行目: | ||
c['form'] = form | c['form'] = form | ||
c['msg'] = msg | c['msg'] = msg | ||
− | return | + | return Http[[R]]esponse(t.render(c)) |
class UploadFileForm(Form): | class UploadFileForm(Form): | ||
32行目: | 32行目: | ||
===template/fileuptest/index.html === | ===template/fileuptest/index.html === | ||
− | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> | + | <!DOCTYPE [[HTML]] PUBLIC "-//W3C//DTD [[HTML]] 4.01 Transitional//EN"> |
<html> | <html> | ||
<head> | <head> |
2020年2月16日 (日) 04:24時点における最新版
Django Fileアップロード例
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>
結果
© 2006 矢木浩人