根据前面讲的基础开始搭建机器学习的网络平台。
Step 1 : 新建ML文件夹,在其中新建django项目,新建app
django-admin startproject server_MLs
python manage.py startapp MLWebService
Step 2 : 修改ML/server_MLs/server_MLs文件夹中的urls.py文件
from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from django.conf.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
url(r'MLWebService/',include('MLWebService.urls')),
]
Step 3 : 修改ML//server_MLs/server_MLs下setting.py文件
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'MLWebService.apps.MlwebserviceConfig', #新增
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 修改此行
'DIRS': [os.path.join(BASE_DIR, 'MLWebService', 'templates').replace('\\','/')],
'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',
],
},
},
]
#新增
STATIC_ROOT =os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
# 部署时删除
STATICFILES_DIRS = [
("css", os.path.join(STATIC_ROOT, 'css')),
# ("img", os.path.join(STATIC_ROOT, 'img')),
("js", os.path.join(STATIC_ROOT, 'js')),
]
# 新增
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
到这一步 server_MLs中的配置文件就已经基本配置完成了。
Step 4:修改ML//server_MLs/下新建static文件夹,并在其中添加css文件夹和js文件夹。这个地方用于分别添加网页需要的css和js包。这里我添加了bootstrap.min.css,bootstrap.min.js,jquery.min.js
Step 5 :分析一下要添加的网页
Step 6 : 分析一下业务逻辑
Step 7:分析一下ORM
根据业务逻辑,主要需要两个实体模型,一个是用户表,一个是训练模型表。在MLWebService下的models.py中输入代码:
from django.db import models
# Create your models here.
class trainingTask(models.Model):
oid = models.IntegerField(primary_key=True, db_column='FId')
trainingName = models.CharField(max_length=200)
trainingDataFile = models.CharField(max_length = 200)
typeOfModel = models.CharField(max_length = 100)
onTraining = models.IntegerField(default=-1)
uploadTime = models.DateTimeField(auto_now="True")
username = models.CharField(max_length=128,default = "")
class MLUser(models.Model):
'''用户表'''
username = models.CharField(max_length=128,unique=True)
password = models.CharField(max_length=256)
email = models.EmailField()
c_time = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.username
class Meta:
ordering = ['c_time']
verbose_name = '用户'
verbose_name_plural = '用户'
之后别忘了用
python manage.py makemigrations MLWebService
python manage.py migrate
to be continued …