django 创建项目自定义配置文件
在安装虚拟环境之下找到自动生成的配置文件!我的虚拟环境在home目录之下
/home/sxu/.virtualenvs/django_temp/lib/python3.5/site-packages/django/conf
在上个目录下有两个文件夹,在project_template文件夹中含有配置文件可以增加自己要用的文件以setting.py.tql 为例增加以下部分!
""" Django settings for demo project. Generated by 'django-admin startproject' using Django 1.11.11. For more information on this file, see https://docs.djangoproject.com/en/1.11/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.11/ref/settings/ """ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) # os.path.abspath(__file__) 表示此文件所在的目录 BASE_DIR表示此文件的上一级的上一级位置! # BASE_DIR 表示当前项目的根目录! BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'g0s1tcq#9n-d*v6w*031ka@sbtb5_4!%e+t7n)uq^one2v534%' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # 将 DEBUG 设置为 False,必须也要设置ALLOWED_HOSTS 允许那些ip地址访问! # DEBUG = False # # ALLOWED_HOSTS = ['*'] # Application definition # 将user的自应用添加到注册文件之中! # 可以不注册应用,路由系统与注册无关,但是项目迁移需要注册! INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users.apps.DempConfig', ] # 中间件相当于请求钩子,即是给请求添加额外的功能! MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', # 'django.middleware.csrf.CsrfViewMiddleware', #跨站请求伪造! 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] # 指定全局路由,setting 文件是在这里寻找的项目路由 ROOT_URLCONF = 'demo.urls' # 指定模板文件的位置! TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', # 模板的路径设置! 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'demo.wsgi.application' # Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases # 数据库配置文件! DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # Password validation # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators # 用户认证,注册时候会自动认证! AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/1.11/topics/i18n/ # 文字设置为中文! LANGUAGE_CODE = 'zh-hans' # 时区设置为上海 TIME_ZONE = 'Asia/Shanghai' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ # 设置static文件的位置 '127.0.0.1:8000/static'!不过只是在 debug 模式之下,才会自动加载! # STATIC_URL = '/static/' # 图片的位置在根目录也就是BASE_DIR下的static_files文件之中! STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static_files'), ]
我增加了部分注释,以及增加了redis数据库配置,session可以自定保存到redis之中,project_template之中的urls.py.tql复制到app_template之中,这样每次创建子应用就可以加上urls.py文件!
接下来创建一个项目
django-admin startproject 工程名称
然后创建app应用!
python manage.py startapp 子应用名称