Setting up Django media files for development is about understanding what MEDIA_URL is and MEDIA_ROOT.
MEDIA_URL is simply a URL prefix (or “slug”) that tells Django what the URL should look like when accessing media files from the MEDIA_ROOT.
For example, if you wanted your MEDIA_URL to be: “/uploads/”, whenever Django attempted to access files in your MEDIA_ROOT, it would prefix the URL with “/uploads/some-file.whatever”.
Since we know that, understanding the MEDIA_ROOT is simple. It’s just the directory that holds the files that users upload. One directory. Holds user uploads. That’s it.
When setting up Django media files locally, we have to define MEDIA_URL and MEDIA_ROOT in the settings.py. We also need to add the following to the project’s main urls.py file:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
# … the rest of your URLconf goes here …
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
This mysterious static () thing just generates a URL pattern in regex and appends it to the urlpatterns list.
Helpful links:
–
–
–
–
#Django #Python
Nguồn: https://valuedandloved.com
Xem thêm bài viết khác: https://valuedandloved.com/cong-nghe/
Xem thêm Bài Viết:
- Adobe Character Animator là gì? Top cấu hình máy tính chạy Character Animator
- Tìm hiểu CRM bất động sản và những điều cần biết
- Binary Option là gì? Giao dịch trên sàn Binary Option thực hiện như thế nào?
- Top CPU Tốt Nhất 2020 Cho Người Build PC
- Những lưu ý khi cài đặt và sử dụng phần mềm Microsoft Toolkit
best explanation on internet !! thanks buddy
Great work mate, thanks
I'm starting to believe Django is only used for local servers… there are 1001 tutorials on how to setup media files in development but NONE for production 😀