ติดตั้ง Django Framework
pip install django==1.10
สร้าง Django Project
# django-admin startproject [project-name] django-admin startproject first_project
เมื่อทำการสร้างแล้วเราจะได้โครงสร้าง Project แบบนี้ครับ
/ first_project | |--/ first_project | |-- __init__.py # ไฟล์เปล่า เพื่อให้ Python รู้ว่าเป็นโฟลเดอร์แพ็กเกจ | |-- settings.py # ไฟล์ที่เก็บค่า Setting เกี่ยวกับ Project ไว้ | |-- urls.py # ไฟล์ที่เก็บข้อมูลเกี่ยวกับ URL Mapping | |-- wsgi.py # ไฟล์ที่ใช้งานตอน Deployment | |-- manage.py # ไฟล์จัดการต่างๆ เช่น run server, create application
ทดสอบว่าทำงานได้ปกติโดยการใช้คำสั่ง runserver ใน Folder Project
python manage.py runserver
สร้าง Django Application
Django Project เราสามารถเพิ่ม Application เข้าไปได้หลายตัว# python manage.py startapp [application-name] python manage.py startapp first_app
เมื่อสร้างแล้วจะได้โฟลเดอร์ Application เพิ่มเข้ามาโครงสร้างแบบนี้
/ first_app |--/ migrations # เก็บข้อมูล database ที่ใช้กับ data models | |-- __init__.py # ไฟล์เปล่า เพื่อให้ Python รู้ว่าเป็นโฟลเดอร์แพ็กเกจ |-- admin.py # ไฟล์ข้อมูล models เพื่อให้ Django ใช้กับ admin interface |-- apps.py # ไฟล์ข้อมูล application เพื่อตั้งค่าต่างๆ |-- models.py # ไฟล์ข้อมูล data models |-- tests.py # ไฟล์เก็บ function ที่ใช้ในการทดสอบ |-- views.py # ไฟล์เก็บ function จัดการ request และ response
เมื่อทำการสร้าง first_app แล้วให้แก้ไขไฟล์ views.py เพื่อไว้ครูก่อน
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("Hello World!")
เสร็จแล้วให้ไปเพิ่มข้อมูลที่ไฟล์ settings.py และ urls.py ใน Project folder
INSTALLED_APPS = [ ..., 'first_app' ]
... from django.conf.urls import url from first_app import views urlpatterns = [ ... url(r'^$', views.index, name='index'), ]
การทำ URL Mappings
ตัวอย่างนี้เป็นสร้างไฟล์ urls.py ที่ Application เพื่อให้เราไม่ต้องไปเพิ่มข้อมูลไฟล์ urls.py ที่ตัว Project ทุกครั้งที่สร้าง method ใหม่สร้างไฟล์ urls.py ใน Application folder
from django.conf.urls import url from first_app import views urlpatterns = [ url(r'^$', views.index, name='index'), ]
เปิดไฟล์ urls.py ใน Project folder เพื่อเพิ่มข้อมูล url mapping ของ Application ลงไป
...
from django.conf.urls import include, url
urlpatterns = [
...
url(r'^first_app/', include('first_app.urls')),
]
...
การใช้งาน Template และ Static
สร้าง Templates folder ใน Project folder และสร้าง Application folder ข้างใน Templates folder อีกที จะได้หน้าตาประมาณนี้/ first_project |--/ first_app |--/ first_project |--/ templates | |--/ first_app | |-- index.html # Template file with Django template tag
แก้ไขไฟล์ settings.py ใน Project folder เพิ่มตัวแปร TEMPLATE_DIR และเพิ่มข้อมูลที่ TEMPLATE = []
...
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,"templates")
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
...
เสร็จแล้วสร้างไฟล์ index.html ใน Templates folder
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>First Apps</title>
</head>
<body>
<h1>Hello this is index.html</h1>
{{ insert_me }}
</body>
</html>
แก้ไขไฟล์ views.py ใน Application folder
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
my_dict = {'insert_me': "Hello I am from views.py"}
return render(request, 'first_app/index.html', context=my_dict)
สร้าง Static folder และ Images folder ไว้ข้างใน เสร็จแล้วแก้ไขไฟล์ settings.py เพิ่มตัวแปร STATIC_DIR และข้อมูล STATICFILES_DIRS =[]
...
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,"templates")
STATIC_DIR = os.path.join(BASE_DIR, "static")
...
STATIC_URL = '/static/'
STATICFILES_DIRS = [
STATIC_DIR,
]
...
แก้ไข Template file เพื่อให้โหลดไฟล์ Static ได้
<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
<meta charset="utf-8">
<title>Django Static File</title>
</head>
<body>
<h1>Hi, this is a picture of Django himself!</h1>
<img src="{% static "images/static.jpg" %}" alt="">
</body>
</html>
ไม่มีความคิดเห็น:
แสดงความคิดเห็น