Django Upload Images

Source: Internet
Author: User


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 :

    • A. Add {{Media_url}} in front

{{Media_url}} {{Course_org.image}}
    • B. Add/dimples/in front

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

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.