「基于Python技术的智慧中医商业项目」Django后端系统配置

<p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/24367109-13b06998c4da2c95.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><h1><strong>内容简介</strong></h1><p>本文内容为该项目V1.0版本的内容。</p><p>Django搭建康养智慧中医项目平台服务的系统配置内容,根据实际情况和具体需求进行修改该即可。由于V1.0版本和2.0版本从API接口业务逻辑设计上有着很大的变化,这里就做一个简单的介绍。不过Django项目的基础配置还是大同小异的。</p><h1><strong>创建项目准备</strong></h1><p><strong>1.创建项目</strong> </p><p>打开CMD使用命令行创建项目</p><pre>django-admin.exe startproject TCM_ManageMent</pre><p><strong>2.添加Xadmin和DjangoUeditor</strong> </p><p>在项目目录下创建extra_apps文件夹,将下载好的Xadmin和DjangoUeditor复制即可。最终配置好文件夹的目录应该是下图这样。</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/24367109-41e95f4356404000.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p><strong>3.添加其他文件夹</strong> </p><p>根据自己需要进行添加,可以先添加后面使用到了再做说明。  </p><ul><li><p>apps 网站应用使用</p></li><li><p>conf 配置nigix使用  </p></li><li><p>media 配置多媒体文件使用  </p></li><li><p>static 配置静态文件使用  </p></li><li><p>templates 配置html模板使用</p></li></ul><h1><strong>修改配置文件setting.py</strong></h1><p><strong>1.配置项目文件</strong> </p><p>在配置文件中声明自己为项目新增的文件目录。</p><pre>import osimport sys

sys.path.insert(0, os.path.join(BASE_DIR, 'apps'))sys.path.insert(0, os.path.join(BASE_DIR, 'extra_apps'))</pre><p>
</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/24367109-6e229315898cbf85.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p><strong>2.配置调试参数</strong> </p><p>开启调试功能和网络访问限定功能。</p><pre>DEBUG = True # 这里未来搭建好网站之后使用False关闭即可ALLOWED_HOSTS = ['*']  # 修改成任何域名都可以访问</pre><p><strong>3.配置应用信息</strong> </p><p>为Django项目添加项目需要的应用模块,此处暂添加 <strong>articles</strong> 应用,后续添加应用部分,此处均省略。</p><pre>INSTALLED_APPS = [
    ...
    # 添加功能模块
    'rest_framework',   # 前后端分离使用
    'rest_framework_swagger',  # 前后端分离使用

    'DjangoUeditor',  # 富文本编辑器
    'xadmin',  # xadmin主体
    'crispy_forms',  # 渲染表格模块 用于验证码部分
    'reversion',  # 为模型通过版本设置提供数据回滚功能

    # 添加应用模块 
    'articles', # 后续根据实际需要进行添加]</pre><p><strong>4.配置前端模板</strong> </p><p>为Django项目添加 <strong>TEMPLATES</strong> 模板设置,未来有时间开发自己的Admin后台,需要在这里配置,这里直接复制过来就行了,目的可以解决大部分前端无法展示的问题,保证Xadmin后台显示。</p><pre>TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
            'libraries': {  # Adding this section should work around the issue.
                'staticfiles': 'django.templatetags.static',
            }
        },
    },]</pre><p><strong>5.配置用户类</strong> </p><p>在配置文件中直接添加声明用户模型是继承model类,用于继承Django中User模型的声明。</p><pre>AUTH_USER_MODEL = 'user.TcmUser'</pre><p><strong>6.配置数据仓库</strong> </p><p>根据我的习惯使用的 <strong>Mysql</strong> 数据仓库。里面的参数信息填写你的参数信息即可。</p><pre>DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'tcm_manage',
        'USER': '你的用户名',
        'PASSWORD': '你的密码',
        'HOST': '你的IP地址',
        'PORT': '你的端口号',
    }}</pre><p><strong>7.配置语言和时区</strong> </p><p>设置Django项目语言为汉语,时区为东八区上海时间。</p><pre># 设置语言、时区LANGUAGE_CODE = 'zh-hans'TIME_ZONE = 'Asia/Shanghai'USE_I18N = TrueUSE_L10N = TrueUSE_TZ = False  # 数据库存储使用时间,True时间会被存为UTC的时间</pre><p><strong>8.配置文件目录</strong> </p><p>配置静态文件目录和媒体文件目录,用于API接口调用相应的资源数据信息,以及上传数据。</p><pre># 添加静态文件路径STATIC_URL = '/static/'STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),]# 这个在配置nigix时候会使用,暂时不需要# STATIC_ROOT = os.path.join(BASE_DIR, "static")# 设置我们上传文件的路径MEDIA_URL = '/media/'MEDIA_ROOT = os.path.join(BASE_DIR, 'media')</pre><p><strong>9.配置REST_FRAMEWORK</strong> </p><p>根据自己的需要进行调整接口应用选择。</p><pre>REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        # 'rest_framework.permissions.IsAuthenticated',  # 必须有
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        # 'rest_framework.authentication.BasicAuthentication',
        # 'rest_framework.authentication.TokenAuthentication',#系统已有的
        # 'article.auth.MyTokenAuthentication',  # 自定义的带过期的认证
    ),
    'DEFAULT_RENDERER_CLASSES': ('rest_framework.renderers.JSONRenderer',),
    'DEFAULT_PARSER_CLASSES': ('rest_framework.parsers.JSONParser',),
    'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
    # 新版drf schema_class默认用的是rest_framework.schemas.openapi.AutoSchema}</pre><p><strong>10.配置Mysql版本</strong> </p><p>修改以下目录的文件,否则mysql版本过高无法使用。</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/24367109-efb217d3c9c8992e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p><p>直接添加下面内容即可</p><pre>import pymysql
pymysql.version_info = (1, 3, 13, "final", 0)pymysql.install_as_MySQLdb()</pre><h1><strong>创建超级用户</strong></h1><p>要记得自己设置的用户名和密码</p><pre>python manage.py createsuperuser # 创建超级用户, 根据提示输入用户名、邮箱、密码,用于django后台管理系统登陆</pre><p>
</p>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容