For marketers
who love technology
Home » , » Using files in a django models.py

Using files in a django models.py

Django logo
Django logo (Photo credit: Wikipedia)
Django is a great web development framework. It provides a lot of features to setup a professional website in minutes. However, sometimes the errors messages that it provides are complex to understand. I am describing below how to solve an error that occurs if you want the user to upload a file to a django-based website. In this case, you need to define a file field in models.py:

    originalTextFile = models.FileField(upload_to='documents/%Y/%m/%d') # original text if it was submitted as a file

If you forget the upload_to attribute you will have the following error:


./manage.py syncdb
Error: One or more models did not validate:
textModif.texttask: "originalTextFile": FileFields require an "upload_to" attribute.

To solve this error, you need to define the following elements in the settings.py Django configuration file:

MEDIA_ROOT = '/path/to/myproject/media/'
MEDIA_URL = '/media/'

With all these parameters correctly positioned, Django will store files to "media/documents/2012/11/24/" based on the current date and the content of the MEDIA_ROOT variable.

Acknowledgment: this post is largely inspired from the following page.



SHARE

About Gilles

0 comments :

Post a Comment