DJANGO
Michelle @flywindy 2016.11.29
-
Taipei.py
python user group
PyCon Taiwan
Python Web Meetup
Django Girls Taipei
2014~
Django
Python Web Meetup
(2015.7~)
v.s.
v.s.
Django Girls Taipei Python 3.5
...
/
Facebook /
- 11/29
- 12/13
- 12/27
- 1/10
- 1/24
- 2/7
......
https://github.com/djangogirlstaipei/eshop
-
https://github.com/djangogirlstaipei/eshophttps://github.com/djangogirlstaipei/eshop
GETTING STARTED
venv
install django (1.10.3)
$ python -m django version
$ which django-admin.py
startproject
ex: bookshop
startapp
ex: books
MODELS
Book
name
description
original_price
price
author
translator
publisher
isbn
language
MODELS
Book
status
created
updated
published
category
tags
MODEL FIELD
Field types
CharField v.s. TextField
DateTimeField (ex: created, updated, published)
auto_now v.s. auto_now_add
PositiveIntegerField (ex: price)
PositiveSmallIntegerField (ex: status)
EmailField / URLField
CharField
SlugField
FileField / ImageField
MODEL FIELD
Field options
null=True v.s. blank=True
CharField & TextField only need to use blank=True
default
choices
ex: status
help_text
unique
db_index
db_column
MODELS
Relationship fields
ForeignKey
Many-to-one relationships
ManyToManyField
OneToOneField
Book
Category Tag
1
m m
n
MIGRATIONS
makemigrations & migrate
sqlmigrate, which displays the SQL statements for a migration.
ex: python manage.py sqlmigrate books 0001
showmigrations, which lists a projects migrations and their status.
New in 1.10
python manage.py migrate list (before 1.10)
ADMIN
createsuperuser
Password - It must contain at least 8 characters. (1.10)
admin.py
@admin.register(Book)
admin.site.register(Book)
__str__ method
ADMIN
ADMIN - PART I
settings.py
LANGUAGE_CODE = 'zh-hant'
MODEL META
Model Meta options
db_table
default: books_book
ordering
ex: ordering = [-published']
unique_together
ex: unique_together = (("name", "author"),)
ManyToManyField cannot be included in unique_together
ADMIN - PART II
Model Meta options
verbose_name & verbose_name_plural
ADMIN - PART III
books/apps.py
books/__init__.py
ADMIN - PART IV
Model Field options
verbose_name
VIEWS
Templates
settings.py
home.html
views.py
home function
urls.py
NEXT -