การติดตั้งและกำหนดค่า DRF
เพิ่ม DRF และ API ที่เรียกดูได้
การติดตั้งและกำหนดค่า DRF เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
A REST API, Quickly
Django REST Framework, or DRF, layers a clean toolkit on top of Django so you can turn your models into a JSON API fast. 🚀
Install the Package
You add DRF the same way you add any Python library: with pip, then pin it in requirements so teammates get the same version.
pip install djangorestframeworkRegister the App
DRF only wakes up once you add rest_framework to your INSTALLED_APPS list in settings.py, right alongside your own apps.
INSTALLED_APPS = [
# ...
'rest_framework',
]Why INSTALLED_APPS Matters
Adding it to INSTALLED_APPS lets Django discover DRF's templates, static files, and management commands automatically.
The REST_FRAMEWORK Dict
You configure DRF through a single REST_FRAMEWORK dictionary in settings.py, keeping all of its options in one tidy place.
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [],
}Default Renderers
By default DRF can return both raw JSON and a friendly HTML view, thanks to its built-in renderers you rarely have to touch.
The Browsable API
One reason DRF feels magical is the browsable API: open an endpoint in your browser and you get a clickable, documented page. ✨
Wire Up the URLs
DRF ships login views for the browsable API; you include them in your urls.py so you can sign in while testing endpoints.
urlpatterns = [
path('api-auth/', include('rest_framework.urls')),
]Verify the Install
A quick way to confirm DRF is wired in is to open the Django shell and import it without errors before writing any code.
python manage.py shell
>>> import rest_frameworkKeep Settings Minimal
Start with an almost empty config. You only add keys to REST_FRAMEWORK when you genuinely need a behavior changed, not before.
You Are API-Ready
With DRF installed, registered, and its URLs included, your project is now ready to expose endpoints that speak JSON to any client.
Quick Check
Where does DRF need to be listed to activate?
Recap: DRF Set Up
You pip-installed DRF, added it to INSTALLED_APPS, included its auth URLs, and confirmed the import. Your API foundation is solid. 🎉
คำถามที่พบบ่อย
บทเรียน “การติดตั้งและกำหนดค่า DRF” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การติดตั้งและกำหนดค่า DRF” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การติดตั้งและกำหนดค่า DRF”
เพิ่ม DRF และ API ที่เรียกดูได้ คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การติดตั้งและกำหนดค่า DRF” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม
ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การติดตั้งและกำหนดค่า DRF
- ซีเรียลไลเซอร์: โมเดลเป็น JSON
- APIView และฟังก์ชัน @api_view
- API ที่เรียกดูได้และรหัสสถานะ