current in process record the version will be update += 1, so when in the same time another one do same thing will updated_rows == 0.
Django Pessimistic Locking (悲觀鎖定)
when select task by id, the database lock the record. if other one to do same time the second one will get the “Task is already claimed or completed”
We use transaction.atomic()
as a context manager to ensure that all database operations within the block are atomic, meaning they are either all committed or all rolled back in case of an error.
Digango Views
- Understanding Django Views
- avoid add business logic into the view, if embedding business logic directly into the view will make it nonreusable, not testability and coupling.
- Django’s Generic Views
- from django.views.generic import ListView, DetailView, TemplateView
- from django.views.generic.dates import ArchiveIndexView, YearArchiveView, MonthArchiveView, DayArchiveView, TodayArchiveView, DateDetailView
- from django.views.generic.edit import FormView, CreateView, DeleteView, UpdateView, DeleteView
- use help(ListView) to check detail property and method and so on
- Mixins
- refers to a class that provides additional functionality to other classes through inheritance.
- Django Mixin
- ContextMixin: Adds extra context data to the view
- TemplateResponseMixin: Renders template and returns an HTTP response
- FormMixin: Used to handle form submission and validation
- CreateModelMixin: Used to save a new object to database
- UpdateModelMixin: Used to upate an existing object in database
- DeleteModelMixin: Used to delete an object
- URL Configuration
- ulrpatterns = [ re_path(r’^tasks/(?P<by_date>[0-9]{4}-[0-9]{2}-[0-9]{2})/$’, views.query_by_date, name=”task_list”),]
^: indicating the begin
$: indicateing the last
[0-9]{4}: 4 characters and each character is 0~9.
(?P<by_date>…….), will pass the value as by_date parameter to the view.
r preceding the string is a flag, informing Python that this is a raw string
- url path built-in convert:
- str
- int
- slug
- uuid
- path
- url path custom convert
- URL Namespace and Name tasks:task-detail
to use namespace for urls, use app_name in the urls.py
- HttpRequest object
- request.method
- request.user
- request.path
- request.GET
- request.POST
- request.FIELS
- request.headers
- HttpResponse object
- the object accept a parameter that contains the content destined for client.
Use Docker-Compose to help Django Development
cd ~
mkdir practice_folder
#create a virtualenv
python -m venv .venv
.venv\scripts\activate.bat
pip install django~=4.0.0 psycopg2-binary~=2.9.9
django-admin startproject django_project .
python manage.py runserver 0.0.0.0:8000
pip freeze > requirements.txt
create a Dockfile
create docker-compose.yml
docker-compose up -d –build
vim django_project/settings.py
In the Windows Host Computer Modify django source code and
- docker-compose.exe web python manage.py startapp accounts
- docker-compose.exe web python manage.py makemigrations accounts
- docker-compose.exe web python manage.py migrate
- docker-compose.exe web python manage.py createsuperuser
- …………
Use Docker to help Django Development
if you have pyenv Dockerfile, you can use docker built -t django:latest . to build a docker image.
command: docker run –rm -p 8000:8000 -p 3000:3000 -v c:\Users\Fluber\Project\Docker\django\book_django_beginners\:/home/fluber/code -it django:latest
enter a docker console environment to develop.
virtualenv .venv to build a python project environment.
pip install django==4.0.10
ajango-admin startproject new_project
python manage.py startapp new_app
modify new_project/settings.py and new_project/urls.py
modify new_app/models.py new_app/views.py new_app/urls.py
use python manage.py makemigrations
use python manage.py migrate
using python manage runserver 0.0.0.0:8000
I suggest to study the following books to help you know how to use django.
Dockerfile for pyenv environment
- Use a base image with a specific version of Ubuntu
FROM ubuntu:20.04
ENV TZ = Asia\Taipei
- Set environment variables for non-interactive apt installation
ENV DEBIAN_FRONTEND=noninteractive
- Install essential packages
RUN apt-get update && \
apt-get install -y \
build-essential \
libssl-dev \
zlib1g-dev \
libbz2-dev \
libreadline-dev \
libsqlite3-dev \
wget \
curl \
llvm \
libncurses5-dev \
libncursesw5-dev \
xz-utils \
tk-dev \
libffi-dev \
liblzma-dev \
python3-venv \
sudo \
git \
tree \
vim \
&& rm -rf /var/lib/apt/lists/*
- Switch to the /home directory
RUN useradd -m -s /bin/bash fluber && \
usermod -aG sudo fluber
- Set a password for the ‘fluber’ user (change ‘password’ to your desired password)
RUN echo ‘fluber:123456’ | chpasswd
- Switch to the ‘fluber’ user
USER fluber
- Set the working directory to the home directory of the ‘fluber’ user
WORKDIR /home/fluber
- Install pyenv
RUN git clone https://github.com/pyenv/pyenv.git ~/.pyenv
- Add pyenv to the PATH
ENV PATH=”/home/fluber/.pyenv/bin:${PATH}”
RUN echo ‘export PYENV_ROOT=”/home/fluber/.pyenv”‘ >> ~/.bashrc
RUN echo ‘export PATH=”$PYENV_ROOT/bin:$PATH”‘ >> ~/.bashrc
RUN echo ‘eval “$(pyenv init –path)”‘ >> ~/.bashrc
RUN echo ‘eval “$(pyenv init -)”‘ >> ~/.bashrc
- Install Python 3.8.12 and set it as the default version
RUN pyenv install 3.8.12 && \
pyenv global 3.8.12
- Create a user named ‘fluber’ with sudo permissions
- Expose any ports you need for your application (if applicable)
- EXPOSE …
- Start a bash shell when the container runs
CMD [“/bin/bash”]
使用npx react-native init project的方式時,當加入@react-navigation/bottom-tabs時其圖示無法顯示的問題?
安裝Package
npm install --save react-native-vector-icons @types/react-native-vector-icons
在 android > app > build.gradle 檔案加入下列一行∶
apply from: file("../../node_modules/react-native-vector-icons/fonts.gradle")
使用xode打開 ios > project.xcworkspace,如下圖所示新建資料夾Fonts。再將node_modules > react-native-vector-icons > Fonts >Ionicons.ttf 檔案拖拉到xcode專案中的Fonts資料夾下。
在下圖XCode專案中的Info中,新建Fonts provided by application類別,並在此類別下新增一個Item 0值為Ionicons.ttf
下方Tab的圖示就可以正常顯示。
Java版本工具Sdkman
執行指令∶ curl -s “https://get.sdkman.io” | bash
下列Statement將會加入~/.zshrc中
查詢可安裝的Java版本
安裝Java
sdk install java Identifier (上表最後一欄的值)
切換Java版本
sdk use java Identifier
安裝nvm環境
作業環境∶ OSX,Homebew
安裝指令∶brew install nvm
執行指令:nvm -h, 瞭解nvm 命令功能
執行指令:nvm ls-remote –lts, 查詢官網長期維護版本
執行指令∶ nvm install v16.20.2
執行指令:nvm ls, 查詢目前電腦系統上所安裝的版本清單及正在使用的是那個版本
執行指令:nvm use, 指定所要使用的版本