1. 程式人生 > 實用技巧 >django 學習 (一) 簡單試用

django 學習 (一) 簡單試用

主要記錄關於環境搭建的問題

安裝django

推薦使用venv,virtualenv 也是一個不錯的選擇

python -m venv venv
source  venv/bin/activate
python -m pip install Django

建立一個簡單的project

使用django-admin

django-admin startproject demoapp

效果

執行

python manage.py runserver

包含admin 執行

先執行db 建立

python manage.py migrate

賬戶建立

python manage.py createsuperuser

重啟服務登陸效果



docker 執行

  • requirements.txt
Django==3.1.4
  • Dockerfile
FROM python:3.8.7-slim
WORKDIR /app
COPY . /app
RUN pip install -i https://mirrors.aliyun.com/pypi/simple/ -r requirements.txt
EXPOSE 8000
CMD [ "python","manage.py","runserver","0:8000" ]

參考命令

  • django-admin
Type 'django-admin help <subcommand>' for help on a specific subcommand.
Available subcommands:
[django]
  check
  compilemessages
  createcachetable
  dbshell
  diffsettings
  dumpdata
  flush
  inspectdb
  loaddata
  makemessages
  makemigrations
  migrate
  runserver
  sendtestemail
  shell
  showmigrations
  sqlflush
  sqlmigrate
  sqlsequencereset
  squashmigrations
  startapp
  startproject
  test
  testserver
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
  • manage.py
Type 'manage.py help <subcommand>' for help on a specific subcommand.
Available subcommands:
[auth]
  changepassword
  createsuperuser
[contenttypes]
  remove_stale_contenttypes
[django]
  check
  compilemessages
  createcachetable
  dbshell
  diffsettings
  dumpdata
  flush
  inspectdb
  loaddata
  makemessages
  makemigrations
  migrate
  sendtestemail
  shell
  showmigrations
  sqlflush
  sqlmigrate
  sqlsequencereset
  squashmigrations
  startapp
  startproject
  test
  testserver
[sessions]
  clearsessions
[staticfiles]
  collectstatic
  findstatic
  runserver

說明

django 腳手架工具提供的命令還是比較多的,可以都試試,加深瞭解

參考資料

https://docs.djangoproject.com/en/3.1/intro/tutorial01/