Django : File Uploading : first look

(op)hdknr@debuniid:~/.virtualenvs/op/src$ django-admin.py startproject uploadr
(op)hdknr@debuniid:~/.virtualenvs/op/src$ ls -R uploadr/
uploadr/:
__init__.py  manage.py  settings.py  urls.py

(op)hdknr@debuniid:~/.virtualenvs/op/src$ cd uploadr/
(op)hdknr@debuniid:~/.virtualenvs/op/src/uploadr$ python manage.py startapp upr
(op)hdknr@debuniid:~/.virtualenvs/op/src/uploadr$ vi upr/forms.py

from django import forms

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

    def handle_uploaded_file(self,file):
        print type(file),"file.name=",file.name
        print dir(file)
        destination = open(‘/tmp/’+file.name,’wb+’)
        for chunk in file.chunks():
            destination.write(chunk)

(op)hdknr@debuniid:~/.virtualenvs/op/src/uploadr$ vi upr/views.py

from django.conf.urls.defaults import *
from django.http import HttpResponse,HttpResponseRedirect
from django.shortcuts import render_to_response
from django import template

from uploadr.upr.forms import UploadFileForm

def upload_file(request):
    if request.method == ‘POST’:
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            form.handle_uploaded_file(request.FILES[‘file’])
            return HttpResponse(
                    template.Template(”’
                        <html><head><title>Uploaded</title></head> <body>
                        <h1>Uploaded</h1>
                        </body></html>
                        ”’
                        ).render( template.Context({}))
                    )
    else:
        form = UploadFileForm()
    return HttpResponse(
                    template.Template(”’
                        <html><head><title>Uploaded</title></head> <body>
                        <h1></h1>
                        {% if form.is_multipart %}
                             <form enctype="multipart/form-data" method="post" action="/upload_file/">
                        {% else %}
                            <form method="post" action="/upload_file/">
                        {% endif %}
                        {{ form }}
                         <input type="submit" value="Upload" />
                        </form>
                        </body></html>
                        ”’
                        ).render( template.Context({’form’:form}))
                    )

(op)hdknr@debuniid:~/.virtualenvs/op/src/uploadr$ vi urls.py

from django.conf.urls.defaults import *
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns(”,
    # (r’^admin/’, include(admin.site.urls)),
    (r’^upload_file/’, ‘uploadr.upr.views.upload_file’),
)

(op)hdknr@debuniid:~/.virtualenvs/op/src/uploadr$ python manage.py runserver 0.0.0.0:8000
Validating models…
0 errors found

Django version 1.1.1, using settings ‘uploadr.settings’
Development server is running at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

up-01

up-02

up-03

[02/Nov/2009 00:44:33] "GET /upload_file/ HTTP/1.1" 200 680
<class ‘django.core.files.uploadedfile.InMemoryUploadedFile’> file.name= svn-python-wininst.log
[‘DEFAULT_CHUNK_SIZE’, ‘__class__’, ‘__delattr__’, ‘__dict__’, ‘__doc__’, ‘__getattribute__’,
‘__hash__’, ‘__init__’, ‘__iter__’, ‘__len__’, ‘__module__’, ‘__new__’, ‘__nonzero__’,
‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__str__’, ‘__unicode__’,
‘__weakref__’, ‘_get_closed’, ‘_get_name’, ‘_get_size’, ‘_name’, ‘_set_name’, ‘_set_size’,
‘_size’, ‘charset’, ‘chunks’, ‘close’, ‘closed’, ‘content_type’, ‘encoding’, ‘field_name’,
‘file’, ‘fileno’, ‘flush’, ‘isatty’, ‘mode’, ‘multiple_chunks’, ‘name’, ‘newlines’, ‘open’,
‘read’, ‘readinto’, ‘readline’, ‘readlines’, ‘seek’, ‘size’, ‘softspace’, ‘tell’, ‘truncate’,
‘write’, ‘writelines’, ‘xreadlines’]
[02/Nov/2009 00:45:30] "POST /upload_file/ HTTP/1.1" 200 180

 

(op)hdknr@debuniid:~/.virtualenvs/op/src/uploadr$ more /tmp/svn-python-wininst.log

*** Installation started 2009/11/02 14:08 ***
Source: C:\Users\hdknr\Downloads\svn-python-1.6.6.win32-py2.5.exe
999 Root Key: HKEY_LOCAL_MACHINE
040 Reg DB Value: [Software\Microsoft\Windows\CurrentVersion\Uninstall\svn-python-py2.5]DisplayName=Python 2.5 svn-p
ython-1.6.6
040 Reg DB Value: [Software\Microsoft\Windows\CurrentVersion\Uninstall\svn-python-py2.5]UninstallString="C:\Share\Py
thon25\Removesvn-python.exe" -u "C:\Share\Python25\svn-python-wininst.log"
200 File Copy: C:\Share\Python25\Lib\site-packages\svn_python-1.6.6-py2.6.egg-info
100 Made Dir: C:\Share\Python25\Lib\site-packages\libsvn
200 File Copy: C:\Share\Python25\Lib\site-packages\libsvn\client.py
200 File Copy: C:\Share\Python25\Lib\site-packages\libsvn\core.py
200 File Copy: C:\Share\Python25\Lib\site-packages\libsvn\delta.py
200 File Copy: C:\Share\Python25\Lib\site-packages\libsvn\diff.py
200 File Copy: C:\Share\Python25\Lib\site-packages\libsvn\fs.py

カテゴリー: 未分類 パーマリンク

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト / 変更 )

Twitter 画像

Twitter アカウントを使ってコメントしています。 ログアウト / 変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト / 変更 )

Google+ フォト

Google+ アカウントを使ってコメントしています。 ログアウト / 変更 )

%s と連携中