Django-File Upload

Source: Internet
Author: User

1 Modifying the settings.pyof project, adding

Media_url ' /media/ '    #这个是浏览器访问的地址, for example: Http://127.0.0.1:8000/media/covers/21.png

' Media ') #这个是文件在服务器上保存的根目录, this is the media directory under the Proje root directory, there is covers/21.png, you can access the file by the way above

2 Modify Project's urls.py file to add

...  from Import  fromimport = ... if settings. DEBUG:    + = static (settings. Media_url, Document_root=settings. Media_root)  #使得浏览器可以通过MEDIA_URL访问MEDIA_ROOT下的文件

3 Add a field of the file type to the app's models.py , specify the upload location, default to the project root directory

 from Import Models class Album (models. Model):    = models. Filefield (upload_to="covers")   #文件会上传到/media/covers/directory    = models. Filefield (upload_to="songs")    #文件会上传到/media/songs/directory     ...

4 using uploaded files in a template

For example <img src="{{album.album_logo. URL }}"/>

<a href= "{{album.audio_file.url}}" ...

Django-File Upload

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.