ImageField และการให้บริการไฟล์อัปโหลด
จัดการรูปภาพและลิงก์ไปยังสื่อในสภาพแวดล้อมพัฒนา
ImageField และการให้บริการไฟล์อัปโหลด เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why ImageField
For pictures, use ImageField instead of FileField. It works the same but adds image-specific checks and helpers on top.
avatar = models.ImageField(upload_to='avatars/')Pillow Is Required
ImageField depends on the Pillow library to read images. Install it first or migrations will refuse to run. 🖼️
pip install PillowValidation Built In
ImageField verifies an upload is a real image. A renamed .txt file gets rejected during form validation automatically.
Width and Height
Set width_field and height_field and Django records each image's dimensions into those columns on save.
photo = models.ImageField(
upload_to='pics/',
height_field='h', width_field='w')Display the Image
In a template, point an img tag at the field's .url to render the stored picture for the user.
<img src="{{ profile.avatar.url }}" alt="avatar">Guard the URL
Calling .url on an empty field raises an error. Wrap it in an if check so missing images do not crash your page.
{% if profile.avatar %}
<img src="{{ profile.avatar.url }}">
{% endif %}Provide a Default
Give the field a default image path so users without an upload still see a placeholder avatar.
models.ImageField(default='avatars/blank.png')Serving in Dev
Django will not serve media on its own. In development, add a route from MEDIA_URL to MEDIA_ROOT in your project urls.
urlpatterns += static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)Only When DEBUG
That media helper is for development only. In production a real web server serves uploads, so guard the route with a DEBUG check.
if settings.DEBUG:
urlpatterns += static(...)Production Serving
In production let Nginx or a storage service like S3 serve media directly. Routing every image through Django would be slow.
Thumbnails Later
Need resized versions? Libraries like django-imagekit generate thumbnails on top of ImageField when you are ready.
Quick Check
Let us check what ImageField needs to function.
Recap
You learned that ImageField needs Pillow, validates real images, exposes .url for display, and that production should serve media outside Django. 🎉
คำถามที่พบบ่อย
บทเรียน “ImageField และการให้บริการไฟล์อัปโหลด” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ImageField และการให้บริการไฟล์อัปโหลด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ImageField และการให้บริการไฟล์อัปโหลด”
จัดการรูปภาพและลิงก์ไปยังสื่อในสภาพแวดล้อมพัฒนา คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ImageField และการให้บริการไฟล์อัปโหลด” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- STATICFILES และแท็ก static
- collectstatic สำหรับใช้งานจริง
- MEDIA_ROOT และการอัปโหลดไฟล์
- ImageField และการให้บริการไฟล์อัปโหลด