安装及相关命令

  1. # 命令方式:
  2. # 安装Django
  3. $ pip3 install django
  4. # 创建项目
  5. $ django-admin startproject 项目名
  6. # 创建应用
  7. $ python manage.py startapp app
  8. # 启动
  9. $ python manage.py runserver [ip:port] # 默认是127.0.0.1:8000
  10. # 创建Admin 账号
  11. python manage.py createsuperuser
  12. # 也可使用PyCharm创建
  13. # 整合static文件
  14. # 1. 在settings.py中添加
  15. STATIC_ROOT = os.path.join(BASE_DIR, 'static')
  16. # 2. >> python manage.py collectstatic

uwsgi启动

uwsgi.ini文件

  1. [uwsgi]
  2. # Django-related settings
  3. # the base directory (full path)
  4. chdir = /alidata/www/erpapi
  5. # Django's wsgi file
  6. module = erpapi.wsgi
  7. # the virtualenv (full path)
  8. home = %(chdir)/venv
  9. # process-related settings
  10. # master
  11. master = true
  12. # maximum number of worker processes
  13. processes = 2
  14. # the socket (use the full path to be safe
  15. socket = 127.0.0.1:8084
  16. # ... with appropriate permissions - may be needed
  17. # chmod-socket = 664
  18. # clear environment on exit
  19. vacuum = true
  20. # 禁止记录请求日志
  21. disable-logging = true
  22. # 日志文件位置
  23. daemonize = %(chdir)/log/uwsgi.log
  24. # 日志文件最大20M
  25. log-maxsize = 20971520
  26. # uwsgi运行相关文件路径
  27. stats = %(chdir)/uwsgi.status
  28. pidfile = %(chdir)/uwsgi.pid

uwsgi命令

  1. # 启动
  2. ./venv/bin/uwsgi --ini uwsgi.ini
  3. # 重新加载
  4. ./venv/bin/uwsgi --reload uwsgi.pid
  5. # 停止
  6. ./venv/bin/uwsgi --stop uwsgi.pid

Django目录

  1. mysite
  2. |— manage.py 对当前Django程序所有操作
  3. |— mysite
  4. |— settings.py Django配置文件
  5. |— urls.py 路由系统
  6. |— wsgi.py 用于定义Djangosocketwsgirefuwsgi
  7. |— static 静态文件目录,手动创建,settings中配置
  8. |— templates 模板目录
  9. |— app01 app 目录
  10. |— admin.py Django自带后台管理相关配置
  11. |— models.py 写类,根据类创建数据库表
  12. |— test.py 单元测试
  13. |— views.py 业务处理,可以换成views目录

路由文件urls.py

  1. # Django 1.x
  2. from django.conf.urls import url
  3. urlpatterns = [
  4. url(r'^index.html/?$', views.index),
  5. ]
  6. # Django 2.x
  7. from django.urls import path, re_path
  8. urlpatterns = [
  9. path('index.html/', views.index),
  10. # django 2中 要用正则表达式则需要使用re_path
  11. ]

request

  1. request.method # 请求方式 POST GET ...
  2. request.POST # 用户POST提交的数据 dict
  3. request.GET # 用户GET提交的数据 dict
  4. request.path_info # 当前的url路径

视图返回

  1. from django.shortcuts import HttpResponse, render, redirect
  2. return HttpResponse('返回内容')
  3. # 模块放在templates中,可在settings.py中修改配置
  4. return render(request, '模板文件.html', {...})
  5. return redirect('url') # 跳转到指定url

配置

模板文件目录templates配置

  1. # settings.py中配置 默认自动配置
  2. TEMPLATES = [
  3. {
  4. 'BACKEND': 'django.template.backends.django.DjangoTemplates',
  5. 'DIRS': [os.path.join(BASE_DIR, 'templates')] # 指定模板目录名
  6. ,
  7. 'APP_DIRS': True,
  8. 'OPTIONS': {
  9. 'context_processors': [
  10. 'django.template.context_processors.debug',
  11. 'django.template.context_processors.request',
  12. 'django.contrib.auth.context_processors.auth',
  13. 'django.contrib.messages.context_processors.messages',
  14. ],
  15. },
  16. },
  17. ]

静态文件目录static配置

  1. # 在settings.py中配置
  2. STATIC_URL = '/static/' # 使用时前缀
  3. STATICFILES_DIRS = (
  4. os.path.join(BASE_DIR, 'static'), # 时间目录名
  5. )

本地化配置

  1. LANGUAGE_CODE = 'zh-hans' # 默认 en-us
  2. # 设置时区
  3. TIME_ZONE = 'Asia/Shanghai'
  4. # 国际化,多语言
  5. USE_I18N = True
  6. # 格式本地化
  7. USE_L10N = True
  8. # True TIME_ZONE设置无效为UTC
  9. USE_TZ = False