To use filefield or imagefield in a models, follow these steps:
1. In your settings file, define a complete pathMedia_rootSo thatDjangoSave the upload file here. (For performance considerations, these files are not stored in the database .) define media_url as the public URL of the directory. make sure that the directory is writable to the Web server user account.
2. In yourModels. pyAdd filefield or imagefield, and make sure thatUpload_toTo tell Django which subdirectory of media_root is used to save the uploaded file.
3. All you need to save in your database is the file path (relativeMedia_root). You can use the. url method to obtain the path of this file. For example, if models contains an imagefield named "photo", you can use {object. Photo. url} in the HTML template to obtain the path of the image.
Settings. py settings
Absolute path of a media file:
# Absolute path to the directory that holds media.
# Example: "/home/Media/media.lawrence.com /"
Media_root = 'd:/python26/lib/Site-packages/Django/bin/newproj/media'
Relative Path of the media file:
# URL that handles the media served from media_root.
# Example: "http://media.lawrence.com"
Media_url = '/site_media /'
Set static file path
Static_path = 'd:/python26/lib/Site-packages/Django/bin/newproj/media'
URL. py shadow path
(R' ^ site_media /(? P <path>. *) $ ', 'django. Views. Static. serv', {'document _ root': settings.Static_path}),
SetMedia_urlOfSite_mediaToSettings. static_path
Models. py upload image field settings
Photo = models. imagefield ('upload image', upload_to = 'photos ')
It is equivalentD:/python26/lib/Site-packages/Django/bin/newproj/Media/photosDirectory./Site_media/photos/Access.