0Pricing
Django Academy · บทเรียน

เขียนวิวแบบฟังก์ชัน

สร้างวิวที่ส่งคืนการตอบกลับ

เขียนวิวแบบฟังก์ชัน เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Django Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What a View Really Is

A Django view is just a Python function that takes a request and returns a response. That is the whole contract. 🙂

The First Argument: request

Every view's first parameter is the request object. Django hands it to you automatically when a URL matches your view.

Importing HttpResponse

To send something back, you return an HttpResponse. Import it from django.http before you use it.

from django.http import HttpResponse

Your First View

Here is a complete, working view. It takes request and returns plain text in a response.

from django.http import HttpResponse

def home(request):
    return HttpResponse('Hello, Django!')

Where Views Live

By convention, views go in your app's views.py file. Keeping them there makes them easy to find and wire up later.

You Must Return Something

A view that returns None raises an error. Django expects an HttpResponse every single time, with no exceptions.

Returning HTML

The response body is just a string, so you can put HTML inside it. The browser renders it like any page.

def home(request):
    return HttpResponse('<h1>Welcome</h1>')

Views Are Plain Functions

A view is normal Python, so you can run logic inside it: build a message, check a condition, then return your response.

def greet(request):
    name = 'friend'
    return HttpResponse('Hi ' + name)

Naming Your View

Name views after what they do, like home or about. Clear names make your URL file readable when you connect them.

Function-Based vs Class-Based

This style is a function-based view, the simplest kind. Class-based views come later; functions are perfect to start with.

Nothing Runs Until It Is Mapped

A view does nothing on its own. It only runs when a URL points to it, which is the very next step you will learn.

Quick Check

Let's confirm the core contract of a Django view.

Recap: You Wrote a View

A view is a function that takes request and returns an HttpResponse. You import it, write your logic, and live in views.py. 🎉

คำถามที่พบบ่อย

บทเรียน “เขียนวิวแบบฟังก์ชัน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เขียนวิวแบบฟังก์ชัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เขียนวิวแบบฟังก์ชัน”

สร้างวิวที่ส่งคืนการตอบกลับ คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “เขียนวิวแบบฟังก์ชัน” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เขียนวิวแบบฟังก์ชัน
  2. รูปแบบ URL ด้วย path()
  3. จับพารามิเตอร์จาก URL
  4. รวม URL ของแอปและตั้งชื่อเส้นทาง
← กลับไปที่ Django Academy