nedeľa 6. septembra 2009

Uploading files via Django application

There are two field types in Django for uploading files:
  • FileField (basic field type for any file type), see FileField reference
  • ImageField (advanced variant of FileField, able to do some additional operations useful for images), see ImageField reference
There is also third type - FilePathField, but this cannot be used for uploading, only for selecting already uploaded files, so it won't be covered here.

End of theory, let's look, how to use it in practise.

Environment prepared was documented in previous article: Django + mod_wsgi + Apache2 + PostgreSQL. Following changes need to be done:

settings.py


# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = '/home/django/DSBIS/media/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = 'http://localhost/DSBIS-media/'

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/admin-media/'

...

# application specific settings:
ACCOUNTING_DOCUMENTS_FILE_PATH = 'accounting_documents/'


core/models.py

class AccountingDocument(models.Model):
...
scanned_items_image = models.ImageField(null=True, blank=True, upload_to=settings.ACCOUNTING_DOCUMENTS_FILE_PATH)
scanned_items_file = models.FileField(null=True, blank=True, upload_to=settings.ACCOUNTING_DOCUMENTS_FILE_PATH)

scanned_items_file_path = models.
FilePathField(null=True, blank=True, path=settings.ACCOUNTING_DOCUMENTS_FILE_PATH)

...


core/admin.py

...
admin.site.register(AccountingDocument)
admin.site.register(AccountingDocumentType)
...

urls.py

  ...
# Uncomment the next line to enable the admin:
(r'^admin/(.*)', admin.site.root),
...

/etc/apache2/conf.d/security

AliasMatch /([^/]*\.css) /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/css/$1

# to be able serve images for admin site (backslash at the end of next line is important):
Alias /admin-media/ /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media/
<Directory /usr/local/lib/python2.6/dist-packages/django/contrib/admin/media>
Order deny,allow
Allow from all
</Directory>

# to be able serve images for DSBIS application:
Alias /DSBIS-media/ /home/django/DSBIS/media/
<Directory /home/django/DSBIS/media>
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias / /home/django/DSBIS/apache/django.wsgi

<Directory /home/django/DSBIS/apache>
Order allow,deny
Allow from all
</Directory>
Do not forget restart Apache after those changes.
sudo /etc/init.d/apache2 restart
If user under which Apache runs is not allowed to write to destination directory, fix it (e.g. by using chown, chgrp or chmod).


Now you can test it.
Log into admin site and try to upload file by saving "Accounting Document" record.


Žiadne komentáre:

Zverejnenie komentára

Archív blogu