pythonのdjango

Django(醤糕) 是基于MTV的框架

安装:
pip3 install django

重要性:相当于出门带不带腿

创建工程
django-admin startproject 【工程名称】

工程目录结构
mysite
-mysite # 对整个程序进行配置
-init
-settings # 配置文件 加盐、权限等处理
-url # URL对应关系,
-wsgi # wsgi就是一套规则,这里django就是遵循了它的规则,来创建socket的;uwsgi+nginx
-manage.py # 管理django程序:
- python manage.py
- python manage.py startapp xx
- python manage.py makemigrations
- python manage.py migrate

# 运行Djangog功能 端口可以自定义。
python manage.py runserver 127.0.0.1:8080


# 创建APP
python manage.py startapp cmdb
python manage.py startapp openstart

app目录结构
migrations # 数据库修改表结构
__init__.py # py3里边有没有改玩意儿都无所谓,但是Py2里边必须得有它。
admin.py # Django 为我们提供的后台管理
apps.py # 配置当前app
models.py # ORM,写指定的类,通过命令就可以创建数据库结构。
tests.py # 单元测试。
views.py # 业务代码。

setting.py中需要操作的步骤:
1.配置模板目录

 1 TEMPLATES = [
 2 {
 3 'BACKEND': 'django.template.backends.django.DjangoTemplates',
 4 'DIRS': [os.path.join(BASE_DIR, 'templates')]
 5 ,
 6 'APP_DIRS': True,
 7 'OPTIONS': {
 8 'context_processors': [
 9 'django.template.context_processors.debug',
10 'django.template.context_processors.request',
11 'django.contrib.auth.context_processors.auth',
12 'django.contrib.messages.context_processors.messages',
13 ],
14 },
15 },
16 ]

2.配置静态目录

STATICFILES_DIRS=[(os.path.join(BASE_DIR,'static'))]

注意:在3.5之前 配置静态目录是STATICFILES_DIRS=(os.path.join(BASE_DIR,'static'))这样进行配置的,但是3.7必须得是在数组中。如果还是依照之前配置会报错:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000000003CB48C8>
Traceback (most recent call last):
  File "D:PythonStudycharmday18django_01venvlibsite-packagesdjangoutilsautoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "D:PythonStudycharmday18django_01venvlibsite-packagesdjangocoremanagementcommands
unserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "D:PythonStudycharmday18django_01venvlibsite-packagesdjangocoremanagementase.py", line 425, in check
    raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
?: (staticfiles.E001) The STATICFILES_DIRS setting is not a tuple or list.
    HINT: Perhaps you forgot a trailing comma?

System check identified 1 issue (0 silenced).
原文地址:https://www.cnblogs.com/pengpengzhang/p/9699510.html