Media path settings of Django settings. py

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.