Django static file configuration Memorandum

Source: Internet
Author: User

I have heard that static file configuration for Django is troublesome. The key is that there is no good tutorial to talk about this. Even if there is a tutorial, it is also vague and I have been exploring for 2 days, finally, the problem of uploading and downloading via filefield in Admin is fixed. The record is as follows:

 

1. Modify settings. py.

# Physical root directory of the uploaded file
Media_root = 'd:/output/Django/landsupervisor/media'

# The uploaded file depends on this URL prefix to obtain the file. This prefix will eventually be mapped to the preceding media_root (in the URL. in Py), This prefix must be separated from admin_media_prefix below. I set the two to the same, so I always prompt that the uploaded file cannot be found.
Media_url = '/mymedia /'

# Admin's own static directory URL prefix. All static files in Admin are obtained through this prefix. The system will map it to a file similar to X: \ Django-1.2.1 \ Django \ contrib \ admin \ Media Directory
Admin_media_prefix = '/Media /'

 

2. Modify URL. py

Append this sentence to the urlpatterns variable (R' ^ mymedia /(? P <path>. *) $ ', 'django. Views. Static. serv', {'document _ root': settings. media_root }),

It is to map the media_url prefix set on the server to the physical path settings. media_root through the Django static service.

 

3. the admin model can be used.

XFile = filefield (upload_to = 'images ')

Admin automatically has a form control to upload files.

 

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.