###### tags: `Django` `python` `tutor` [TOC] # Django Note *Author : Jerry Hsieh Email : kilicapeto@gmail.com* ## Install Docker and Built Environment 1. Docker 2. Brain ## Docker basic CLI ```bash # Docker images process build # build the image run # use for the into the images images # display all the images in docker rmi # remove image # Docker Container process ps -a # display all the docker container stop # stop the container rm # remove the container exec # use for the container ``` ## Django Install in Docker ### 1. Create Python Docker environment * 1.1 create the requirements.txt This store the python package. ```bash # path : ./requirments.txt Django==3.0.7 djangorestframework==3.11.0 ``` P.S if you wanna check pipy newest version to [pipy](https://pypi.org/) and if you wanna run all the package you can try: ```bash # Don't run this command pip install -r requirements.txt ``` but you need to installed the python and pip. * 1.2 Create Docker file For example: ```dockerfile # Dockerfile # path : ./Dockerfile FROM python:3.7 ENV PYTHONUNBUFFERED 1 RUN mkdir /app COPY requirements.txt /app/ WORKDIR /app RUN pip install -r requirements.txt COPY . /app/ ``` ### 2. Built the docker images ```dockerfile docker build -t app:latest . # -t image of tag;. is Currently path # app:latest {image name}:{tag} # Check your image docker images ``` ![image name](https://i.imgur.com/7OHSlvU.png) ### 3. Into the Docker Container It's sync with local path. ```bash docker run --rm -it -v ${PWD}:/app -p 8000:8000 app /bin/bash # ${PWD} = Currently path # -v ${PWD}:/app v = volumn Local and docker sync ``` ![](https://i.imgur.com/4DkJPgF.png) ### 4. Create Django Project ```bash django-admin startproject mysite # mysite = your django name # Will be project folder move to previous folder ``` ![](https://i.imgur.com/UuAzyBV.png) ## Re-run ```bash # run the step 3 will be OK docker run --rm -it -v ${PWD}:/app -p 8000:8000 app /bin/bash ``` --- # Django Introduction ## Django CLI ### 1. Check you have in docker. ### 2. run the below CLI. ```bash python manage.py ``` This tutorial will be talking about the below the tag. ```bash [auth] changepassword createsuperuser // create superuser [contenttypes] remove_stale_contenttypes [django] check compilemessages createcachetable dbshell diffsettings dumpdata flush inspectdb loaddata makemessages makemigrations # create migrations file migrate # migrate to db sendtestemail shell showmigrations sqlflush sqlmigrate sqlsequencereset squashmigrations startapp # create application in project startproject test testserver [sessions] clearsessions [staticfiles] collectstatic findstatic runserver # run the server ``` ### 3. Try to run the django service ```bash python manage.py migrate python manage.py runserver 0.0.0.0:8000 ``` open the browser input url : [http://127.0.0.1:8000/](http://127.0.0.1:8000/) and you will look this interface: ![](https://i.imgur.com/XvQ5qiq.png) # Django Structure Introduction ## create super user ```bash python manage.py createsuperuser # for example # Username : admin # Email : admin@example.com # Password : admin # Password (again) : admin # y ``` ![](https://i.imgur.com/35uuTqi.png) ## urls (routes) ```bash # if you're in touch with any framework. # you can first try to find the routes in project that will help you understand and grasp the whole projects. ``` 此章節稍微閱讀即可 先將path exchange to url. path : mysite/urls.py ![](https://i.imgur.com/jrVrVz9.png) ```python # path : ./mysite/urls.py from django.conf.urls import url urlpatterns = [ url('^admin/', admin.site.urls), ] ``` try to look the page [click](http://0.0.0.0:8000/admin/) **path and url different** ```python # from django.urls import path # vs # from django.conf.urls import url # Description: url is support the regex so Like url(r'^book/$', ) # vs path('book/', ) # path is http://localhost:8000/book/ ``` ## settings ```bash # Main components 1. DEBUG 2. ALLOWED_HOSTS 3. INSTALLED_APPS 4. MIDDLEWARE 5. TEMPLATES 6. DATABASES 7. STATIC_URL ``` 1. Debug 如果非開發狀態建議設為False, 結果如下: ```python Debug = True ``` ![](https://i.imgur.com/yAtW28g.png) ``` Debug = False ``` ![](https://i.imgur.com/APR2Ey1.png) 2. ALLOWED_HOSTS 這是允許的IP 設置 ```python ALLOWED_HOSTS = ['*'] # * = all # example ['localhost', '127.0.0.1', '192.168.x.x'] ``` 3. INSTALLED_APPS 將需要使用的Applications 加入此中 ```python INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # your application 'first_applications', ] ``` 4. MIDDLEWARE 此為中間器,在進入application時會先做處理 如:驗證使用者 **目前預設即可** 5. TEMPLATES 前端渲染路徑設定**如沒有使用預設即可** 本課程也不考慮教授 如有需要付錢 6. DATABASES 使用的Database and Engine Default is use the sqlite3: ```python # DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } ``` ![](https://i.imgur.com/ZiSy8Zz.png) MySQL Database settings Example: 首先 ```bash pip install pymysql # 建議要使用可以加入 requirment.txt ``` ```python import pymysql ... DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'database', 'USER':'root', 'PASSWORD':'', 'HOST':'', 'PORT':'', } } ``` 7. STATIC_URL 前端會使用的一些類別 如 css bootstrap的路徑設定 本教學也不會談到這 # 正式開始寫API!! 開發Applications!! 建議使用django 創建一個app ```bash # python manage.py startapp {app_name} python manage.py startapp applications ``` ![](https://i.imgur.com/njnacMN.png) 在上一章節說到要將app 加入至settings ```python # ./mysite/applications INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # add application 'applications', # 因為有安裝djangorestframework in requirments.txt 所以加上 'rest_framework', ] ``` 這樣在啟動的時候就會先導入至Django中 ## API 撰寫 ## --- # To be continued ### ### views to be continued for example ```python # path : ./mysite/urls.py from applications import views from django.conf.urls import url url(r'^/$', views.ClassObject.as_view(), name="root_path") ``` ### how to sql-data add to admin manager ### Explain model ORM ### Detail description # Django common package & Introduction ```bash # Django restful djangorestframework==3.11.0 # Login ## Oauth by django django-oauth-toolkit ## Oauth by python python-oauth2 ## jwt by django djangorestframework-jwt djangorestframework_simplejwt ## jwt by python pyjwt jwt ``` ``` from django.http import JsonResponse ``` to be continued **沒有了還往下滑 不要急好嗎!!!!!!**