from two-scoops-of-django
เนื้อหาที่จะพูดถึงในบทความนี้ ประกอบด้วย
1. เทคนิคในการเขียนโค้ดให้มีประสิทธิภาพ 2. ขั้นตอนการติดตั้ง Django
Making Your Code Readable code written yesterday or ten years ago that having code written in a clear, consistent style becomes extremely useful.
1. Follow PEP 8
PEP 8 is the official style guide for Python.
➤ “Use 4 spaces per indentation level.”
➤ “Separate top-level function and class del·nitions with two blank lines.”
➤ “Method definitions inside a class are separated by a single blank line.”
2. How to Import Class
2.1 Use when importing from outside the current app ➤ from core.views import FoodMixin
2.2 Use when importing from another modulein the current app ➤ from .models import WaffleCone
2.3 Avoid Using Import *
3. use underscores
Always try to use underscores (the “ ” character) over dashes. Good example “add_topping” Bad example "'add-topping”
4. Choose JS, HTML, and CSS Style Guides
How to Set up Django
STEP 1. $ mkdir project_name
STEP 2. set virtual environment $ cd project_name $ python3 -m venv myvenv
STEP 3. install Django $ source ./bin/activate $ pip install --upgrade pip #upgrade pip $ pip install django~=1.9.0
STEP 4. create repository $ hg init
STEP 5. create project on bitbucket
STEP 6. push to repository $ hg push http://...
STEP 7. pip freeze $ pip freeze > requirements.txt
เมื่อเราเตรียม environment ต่างๆ พร้อมแล้ว ขั้นตอนต่อไปก็สร้าง project กันเลย
STEP 8. create django project $ django-admin startproject scoops_project
ขั้นตอนนี้ django จะสร้าง Project Layout ลักษณะนี้
<django_root>/ <django_project_root>/ <configuration_root>/
Level1 = django_root : ประกอบด้วย หลายๆ โปรเจค จะมี hg repository ที่เราสร้างไว้อยู่ด้วย
Level2 = django_project_root : ประกอบด้วย All Python code files are inside
Level3 = configuration_root : urls.py, __init__.py, setting.py
ตัวอย่าง
djangogirls
├───manage.py
└───mysite
settings.py urls.py wsgi.py __init__.py
STEP 9. Create app $ python manage.py startapp scoops
two_scoops ├── scoops_app │ ├── __init__.py │ ├── admin.py │ ├── apps.py │ ├── migrations │ │ └── __init__.py │ ├── models.py │ ├── tests.py │ └── views.py ├── db.sqlite3 ├── manage.py └── scoops_project ├── __init__.py ├── settings.py ├── urls.py └── wsgi.py












