GET /hello/ HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0
Accept: text/html
200 OK, 404 Not Found
HTTP/1.1 200 OK
Content-Type: text/html
<html><body>Hello Client</body></html>
HttpRequest และ HttpResponse สำหรับรับและตอบสนองคำร้องจาก Client
# views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("ข้อความจาก Django!")
เมื่อสร้างโปรเจกต์ Django จะมีโครงสร้างไดเรกทอรีที่ช่วยจัดการระบบได้อย่างเป็นระบบ
myproject/
├── manage.py # ตัวจัดการคำสั่งหลัก
├── myproject/ # โฟลเดอร์การตั้งค่าโปรเจกต์
│ ├── __init__.py
│ ├── settings.py # การตั้งค่าโปรเจกต์
│ ├── urls.py # เส้นทาง URL หลัก
│ └── wsgi.py / asgi.py # จุดเริ่มต้นของเซิร์ฟเวอร์
├── myapp/ # โฟลเดอร์แอปพลิเคชัน (สร้างแยก)
│ ├── admin.py
│ ├── apps.py
│ ├── models.py # ฐานข้อมูล
│ ├── views.py # ฟังก์ชันสำหรับจัดการคำร้อง
│ ├── urls.py # เส้นทางของแอป
│ └── templates/ # เทมเพลต HTML
Django จัดการคำร้อง (Request) ด้วยขั้นตอนที่ชัดเจน ตั้งแต่ผู้ใช้ส่ง URL ไปยังการแสดงผล
พิมพ์ URL หรือกดลิงก์ → เบราว์เซอร์ส่ง HTTP Request ไปยัง Server
urls.py ทำหน้าที่จับคู่ URL ที่ร้องขอกับ View ที่ต้องเรียกใช้
views.py จะรับคำร้องและประมวลผลข้อมูล ส่งกลับ HttpResponse
เซิร์ฟเวอร์ส่งข้อมูลกลับไปยังผู้ใช้ในรูปแบบ HttpResponse
http://localhost:8000/
เป็นวัตถุ (object) ที่ Django สร้างขึ้นเมื่อผู้ใช้ส่งคำร้องมายังเซิร์ฟเวอร์ โดย Django จะรวบรวมข้อมูลทั้งหมดจากคำร้องนั้นให้อยู่ในรูปของ HttpRequest
บอกว่าเป็นการร้องขอแบบใด เช่น GET, POST
ข้อมูลที่ผู้ใช้ส่งมาผ่าน URL หรือแบบฟอร์ม
URL path ที่ร้องขอ เช่น /hello/
ข้อมูลหัวข้อของคำร้อง เช่น User-Agent, Content-Type
เป็นวัตถุที่ Django ใช้ส่งข้อมูลกลับไปยังฝั่งผู้ใช้ (Client) หลังจากประมวลผลคำร้อง (HttpRequest) เสร็จเรียบร้อยแล้ว
เนื้อหาที่จะถูกส่งกลับ เช่น HTML, JSON, Text
สถานะการตอบกลับ เช่น 200 OK, 404 Not Found
ข้อมูลส่วนหัว เช่น Content-Type: text/html
สามารถแนบคุกกี้ไปกับการตอบกลับได้
HttpResponse ช่วยให้เราควบคุมสิ่งที่ผู้ใช้จะเห็นหรือได้รับเมื่อเรียกใช้งานระบบ
ตัวอย่างฟังก์ชันใน views.py ที่รับคำร้องและตอบกลับข้อความง่าย ๆ
from django.http import HttpResponse
def hello_view(request):
if request.method == "GET":
name = request.GET.get('name', 'Guest')
return HttpResponse(f"สวัสดี, {name}!")
else:
return HttpResponse("โปรดใช้วิธี GET ในการร้องขอข้อมูล")
name เพื่อแสดงข้อความต้อนรับ
ทดสอบความเข้าใจเกี่ยวกับระบบ Client-Server, HttpRequest, HttpResponse และ HTML
สร้างพื้นฐานสำหรับการเริ่มต้นพัฒนาเว็บด้วย Django
สร้างโครงงาน mysite
สร้างสภาพแวดล้อมแยกสำหหรับโครงงานในโฟลเดอร์ .venv
เปิดใช้งานสำหรับการพัฒนาโครงงาน
Framework สำหรับพัฒนาเว็บด้วย Python
create django project
start a django application
start django web server
requirements.txt เพื่อเก็บรายการแพ็กเกจที่ใช้myproject, homepage
from django.http import HttpResponse
def request_info_view(request):
print("=== HttpRequest Information ===")
print(f"Method: {request.method}")
print(f"Path: {request.path}")
print(f"Full Path: {request.get_full_path()}")
print(f"Scheme: {request.scheme}")
print(f"Host: {request.get_host()}")
print(f"User Agent: {request.META.get('HTTP_USER_AGENT', '')}")
print(f"Remote Address: {request.META.get('REMOTE_ADDR')}")
print(f"GET Parameters: {request.GET}")
print(f"POST Data: {request.POST}")
print(f"Cookies: {request.COOKIES}")
print(f"Headers:")
for header, value in request.headers.items():
print(f" {header}: {value}")
return HttpResponse("HttpRequest information printed to console.")