Set in 1.seeting
# upload file, resource configuration Media_url = '/dimples/' media_root = Os.path.join (Base_dir, ' dimples ')
2.model
Store only paths
Image = Models. ImageField (upload_to= ' image/%y/%m ', default=u ' image/default.png ', max_length=100)
Upload_to=: Indicates the path of the file (image/year/month)
Get pictures in 3.html
For example:
{{Course_org.image}} only acquired relative address
The right way :
{{Media_url}} {{Course_org.image}}
Data-url = "/dimples/{{Course_org.image}}"
4. Configuring the Media Internal processing class (setting)
OPTIONS--->context_processors add:
' Django.core.context_processors.media '
As follows:
' OPTIONS ': { ' context_processors ': [ ' Django.template.context_processors.debug ', ' Django.template.context_processors.request ', ' Django.contrib.auth.context_processors.auth ', ' Django.contrib.messages.context_processors.messages ', ' Django.core.context_processors.media ' ], },
The media_url is automatically registered in the HTML
5. Configure URLs
From django.views.static Import serve
Configuration (Access handler function for uploading files) dimples URLs:
URL (r ' ^dimples/(? p<path>.*) $ ', serve, {' Document_root ': Media_root}),
This article is from the "13261842" blog, please be sure to keep this source http://13271842.blog.51cto.com/13261842/1965727
Django Upload Images