Django+Vue前后端分离开发

未经许可请勿转载。
Please do not reprint this article without permission.

Project repository - NEURO-LEARN

Django+Vue+Celery

Environment

  • For Django: Python, Django, MySQL, etc. Using pip to install modules including Django and MySQL is recommended;
  • For Vue: Node.js. Using npm to install modules including Element-UI is recommended;
  • For celery: rabbitmq, celery, django-celery. Using apt-get to install rabbitmq-server, and pip to install celery and django-celery;

Build Project

  • Create project;
$ django-admin startproject neurolearn
  • Create Django app as backend;
$ cd neurolearn
$ python manage.py startapp backend
'NAME': 'neurolearn'
'USER': 'root'
'PASSWORD': 'root'
  • Change the default database to mysql and add backend to apps in settings.py;
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'neurolearn',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '127.0.0.1',
    }
}
  • Initialize database and start server to test;
$ python manage.py makemigrations backend
$ python manage.py migrate
$ python manage.py runserver
  • Install vue-cli to initialize a vue project;
$ npm install -g vue-cli
$ npm view vue-cli version // check the package version
  • Create VueJS project as frontend;
$ vue-init webpack frontend
  • Install vue dependencies and build the vue project;
$ cd frontend
$ npm install # install dependencies
$ npm run build # build project
  • Add 'backend' to the INSTALLED_APPS in neurolearn/neurolearn/settings.py;
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'backend',
]
  • Create superuser of django in order to use django admin.
$ python manage.py createsuperuser
# username: root
# email: leibingye@outlook.com
# password: root
# localhost:8000/admin/

References
整合Django+Vue.js框架快速搭建web项目
后端Django+前段Vue.js快速搭建项目
vue使用npm run build命令打包项目

Architecture Design

architecture_design_web.png

Integrate Vue into Django

  • Configure url paths in neurolearn/neurolearn/urls.py;
from django.conf.urls import url
from django.contrib import admin
from django.views.generic import TemplateView

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', TemplateView.as_view(template_name="index.html")),
]
  • Configure the 'DIRS' in neurolearn/neurolearn/settings.py;
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': ['frontend/dist'],
        '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',
            ],
        },
    },
]
  • Change time zone and language code;
# LANGUAGE_CODE = 'en-us'
# TIME_ZONE = 'UTC'
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
  • Add the path of static files rendered by django;
# add at the end of the file
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "frontend/dist/static")
]
  • Run server to test frontend rendering by django;
$ python manage.py runserver
  • Run the following command each time frontend is modified;
$ cd frontend
$ npm run build
  • Using the following command allows debugging in Vue environment;
$ cd frontend
$ npm run dev
  • Using Vue environment to visit Django API will result in cross-domain issues, one solution is using proxyTable in Vue, and the other is using django-cors-headers;
$ pip install django-cors-headers
  • After installing django-cors-headers, we need to configure it in settings.py;
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware', # added
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
CORS_ORIGIN_ALLOW_ALL = True # added

References
Vue+Django+MySQL搭建指南(个人全栈快速开发)
我如何使用Django+Vue.js快速构建项目
Django与Vue之间的数据传递

Integrate Celery into Django

  • Add 'djcelery' to the INSTALLED_APPS in neurolearn/neurolearn/settings.py;
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'backend',
    'djcelery',
]
  • Configure RabbitMQ by add the following codes at the end of neurolearn/neurolearn/settings.py;
import djcelery
djcelery.setup_loader()

BROKER_HOST = "localhost"
BROKER_PORT = 5672
BROKER_USER = "guest"
BROKER_PASSWORD = "guest"
BROKER_VHOST = "/"
  • Running $ python manage.py will result in several new commands to control worker in celery;
[djcelery]
    celery
    celerybeat
    celerycam
    celeryd
    celeryd_detach
    celeryd_multi
    celerymon
    djcelerymon
  • Add a file backend/tasks.py;
from celery.decorators import task

@task
def add(x, y):
  return x + y
# @ is the decorator, making the add function a callback function
# when calling add in a webapp, the add function doesn't execute immediately
# instead the function name and parameters will be packed as a message and
# sent to the queue, then the worker will process these messages one by one
  • Create a worker awaiting task messages;
$ python manage.py migrate
$ python manage.py celeryd -l info
  • Open another console and use the following command to open interactive console;
$ python manage.py shell
>>> from backend.tasks import add
>>> r = add.delay(3, 5)
>>> r.wait()
8

References
使用django+celery+RabbitMQ实现异步执行


NEURO-LEARN-WEB

UI Design

User Interface

Vue and Element-UI

  • Refer to official site for installation and usage guide;
  • Use template from this repository, which looks like this;
  • Replace frontend in the project with template, type the following commands to build the vue project, integrating it into django framework;
$ npm install
$ npm install node-sass // if needed
$ npm run dev // serve with hot reload at localhost:8080
$ npm run build // build for Django to serve at localhost:8000
  • To customize navigation menu and router, change the code in NavMenu.vue to configure the navigation, routes.js to configure the router, and index.vue in each page in pages to configure the template;
  • The pages folder consists of home (routed by NEURO-LEARN title), overview, newtask, submissions, viewer, and help, which are routed by items in NavMenu except for home;
Note: 
The template is develped using element-ui 1.4, which is out of date. Use element-ui 2.7 when develop frontend.
  • The way to pass eslint check is to add a comment like below at the end of the code;
//eslint-disable-line
  • To use scss, install node-sass and sass-loader;
$ npm install --save-dev sass-loader style-loader css-loader
  • To change the theme colors in element-ui, refer to this site for help;
  • When defining the style of a page by css, name the class carefully or use a nested css since it is effective across files;
  • Use this.$router.replace to realize in-page redireting;
this.$router.replace({
  path: '/submissions',
  component: resolve => require(['@/pages/analysis/submissions'], resolve)
})
  • Use this.$router.go(0) to realize in-page refreshing;

References
Element-UI Documentation

Data Transaction

  • Refer to django_with_vue and django-vue for examples of using axios and database for data transaction between Vue and Django;
  • As mentioned above, the configuration of databases is in the settings.py, and by default the name of table created by models.py is 'appname_modelclassname', in which the modelclassname refer to the class defined in models.py;
  • Models are called and instantiated by views.py, which received the http request from frontend and return a response;
  • The urls of functions in views are defined in urls.py, which is included in the urls.py in project folder;
  • The urls.py in project folder contains the urls when frontend sends request to http://127.0.0.1:8000/;
  • As $http in vue requires an out-of-date module named vue-resource, it is recommended to use axios instead;
  • Using axios and django JsonResponse:
    • Excecute POST request with axios to submit a task:
    // import axios from 'axios'
    axios.post('http://127.0.0.1:8000/api/new_task', JSON.stringify(this.newform))
          .then(response => {
            var res = response.data
            if (res.error_num === 0) {
              this.$router.replace({
                path: '/submissions',
                component: resolve => require(['@/pages/analysis/submissions'], resolve)
              })
              console.log(res)
            } else {
              this.$message.error('Failed submission!')
              console.log(res['msg'])
            }
          })
    
    Note: the 'newform' object defined in data object in vue can't be within any other object, as it may not satisfy two-way data-binding with v-model.
    
    • Excecute GET request with axios to get all submissions list:
    // import axios from 'axios'
    axios.get('http://127.0.0.1:8000/api/show_submissions')
          .then(response => {
            var res = response.data
            if (res.error_num === 0) {
              console.log(res)
              this.submissions_table = res['list']
              console.log(res['list'])
            } else {
              this.$message.error('Failed!')
              console.log(res['msg'])
            }
          })
    
    Note: axios support promise, which is the function().then.then ... structure.
    
    • Uniform Resource Locator is defined in urls in backend app, which is included in urls in neurolearn_dev project:
    # neurolearn_dev/urls.py
    from django.conf.urls import url, include
    from django.contrib import admin
    from django.views.generic import TemplateView
    import backend.urls
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^api/', include(backend.urls)), # added
        url(r'^$', TemplateView.as_view(template_name="index.html")),
    ]
    
    # backend/urls.py
    from django.conf.urls import url, include
    from . import views
    
    urlpatterns = [
        url(r'add_book$', views.add_book, ),
        url(r'show_books$', views.show_books, ),
        url(r'new_task$', views.new_task, ),
        url(r'show_submissions$', views.show_submissions, ),
        ]
    
    • Receive request and generate response in views inside django:
    @require_http_methods(["POST"])
    def new_task(request):
        response = {}
        postBody = json.loads(request.body)
        try:
            task = Submissions_Demo(
                task_name=postBody.get('task_name'),
                task_type=postBody.get('task_type'),
                train_data=postBody.get('train_data'),
                test_data=postBody.get('test_data'),
                label=postBody.get('label'),
                feat_sel=postBody.get('feat_sel'),
                estimator=postBody.get('estimator'),
                cv_type=postBody.get('cv_type'),
                note=postBody.get('note'),
                verbose=postBody.get('verbose'),
                task_status='Submitted',
                task_result=''
            )
            task.save()
            response['post_body'] = postBody
            response['msg'] = 'success'
            response['error_num'] = 0
        except Exception as e:
            response['post_body'] = postBody
            response['msg'] = str(e)
            response['error_num'] = 1
    
        return JsonResponse(response)
    
    @require_http_methods(["GET"])
    def show_submissions(request):
        response = {}
        try:
            submissions = Submissions_Demo.objects.filter()
            response['list']  = json.loads(serializers.serialize("json", submissions))
            response['msg'] = 'success'
            response['error_num'] = 0
        except  Exception as e:
            response['msg'] = str(e)
            response['error_num'] = 1
    
        return JsonResponse(response)
    
    • To initialize and manipulate databases, use models in django:
    class Submissions_Demo(models.Model):
        task_id = models.DateTimeField('Edit the date', auto_now=True)
        task_name = models.CharField(max_length=64)
        task_type = models.CharField(max_length=64)
        train_data = models.CharField(max_length=64)
        test_data = models.CharField(max_length=64)
        label = models.CharField(max_length=64)
        feat_sel = models.CharField(max_length=64)
        estimator = models.CharField(max_length=64)
        cv_type = models.CharField(max_length=64)
        note = models.CharField(max_length=64)
        verbose = models.BooleanField()
        task_status = models.CharField(max_length=64)
        task_result = models.CharField(max_length=1024)
    
        def __unicode__(self):
            return self.task_id
    
    Note: after each altering of models, type 'python manage.py makemigrations' and 'python manage.py migrate' in commandline to alter databases.
    
  • View databases and tables inside them:
# mysql -u root -p
# password: root
mysql> use neurolearn_dev;
mysql> select * from backend_submissions_demo

references
Django模型Model自定义表名和字段列名
axios全攻略
vue $http请求服务
Vue:axios中的POST请求传参问题
Vue + Django

File Uploading

  • Refer to this blog for the tutorial of using Django forms to realize file uploading;
  • First add an api redirecting to the view funciton upload_data;
url(r'upload_data$', views.upload_data, ),
  • Add a table in the database containing data information;
class Data_Demo(models.Model):
    data_id = models.DateTimeField('Edit the date', auto_now=True)
    data_name = models.CharField(max_length=64)
    data_path = models.CharField(max_length=128)

    def __unicode__(self):
        return self.task_id
  • Add view funciton to process request and save uploaded file;
@require_http_methods(['POST'])
def upload_data(request):
    response = {}
    try:
        obj = request.FILES.get('test')
        data = Data_Demo()
        data.data_name = 'test'
        data.data_path = obj.name
        data.save()
        handle_uploaded_file(obj)
        
        response['msg'] = 'success'
        response['error_num'] = 0
    
    except Exception as e:
        response['msg'] = str(e)
        response['error_num'] = 1

    return JsonResponse(response)

def handle_uploaded_file(f):
    try:
        path = 'data/'
        if not os.path.exists(path):
            os.makedirs(path)
        else:
            file_name = str(path + f.name)
            destination = open(file_name, 'wb+')
            for chunk in f.chunks():
                destination.write(chunk)
            destination.close()
    except Exception as e:
        print(e)
    return f.name, path
  • Create a front-end template to upload file;
<template>
  <div style="background-color: #FFFFFF; margin: 14px; padding: 14px">
    <el-upload
      class="upload-demo"
      action="http://127.0.0.1:8000/api/upload_data"
      name="test"
      :on-change="handleChange"
      :on-success="uploadSuccess"
      :file-list="fileList">
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
    </el-upload>
  </div>
</template>

<script>
export default {
  data () {
    return {
      fileList: []
    }
  },
  methods: {
    handleChange (file, fileList) {
      this.fileList = fileList.slice(-4)
    },
    uploadSuccess (response) {
      console.log(response.error_num, response.msg)
    }
  }
}
</script>

references
Django快速实现文件上传
Element-UI Upload上传
Django多文件上传,只能保存最后一个文件
Django文件上传到后台的三种方式

File Downloading

  • Refer to this site for three ways of realizing file downloading in Django;
@require_http_methods(["GET"])
def download_templates(request):
    response = {}
    template_type = request.GET.get('template_type')

    template_file=open('templates/' + template_type + '.zip', 'rb')
    response =FileResponse(template_file)
    response['Content-Type']='application/octet-stream'
    response['Content-Disposition']='attachment;filename=\"' + template_type + '.zip\"'
    return response
<a href="http://127.0.0.1:8000/api/download_templates?template_type=dataset_templates"></a>

Visualization

  • Refer to this site for the implementation of image transaction, to be specific,
import io
from PIL import Image

@require_http_methods(["GET"])
def show_roc(request):
    response = {}
    task_id = request.GET.get('task_id')

    buf = io.BytesIO()
    img = Image.open('results/' + task_id + '/190514_ROC_curve_rfe_svm_test_data.png')
    img.save(buf, 'png')

    return HttpResponse(buf.getvalue(), 'image/png')
<img :src="'http://127.0.0.1:8000/api/show_roc?task_id=' + this.taskid" style="width: 700px">
  • To use this taskid, we have to realize parameter passing between modules (in this case, submissions and viewer) using v-router;
// Source module
onRowClick (row) {
      this.$router.push({
        path: '/analysis/viewer',
        query: {taskid: row.fields.task_id}
      })
    }

// Destination module
taskid: this.$route.query.taskid,

Service

Celery and RabbitMQ

  • Refer to this site as before to get the basic idea of celery workers;
  • Define own tasks;
@task
def new_ml_task():
    test_task()
    return
  • Call the task in views.py;
@require_http_methods(["POST"])
def new_task(request):
    response = {}
    postBody = json.loads(request.body)
    try:
        task = Submissions_Demo(
            task_name=postBody.get('task_name'),
            task_type=postBody.get('task_type'),
            train_data=postBody.get('train_data'),
            test_data=postBody.get('test_data'),
            label=postBody.get('label'),
            feat_sel=postBody.get('feat_sel'),
            estimator=postBody.get('estimator'),
            cv_type=postBody.get('cv_type'),
            note=postBody.get('note'),
            verbose=postBody.get('verbose'),
            task_status='Submitted',
            task_result=''
        )
        task.save()

        # create new celery task
        new_ml_task.delay()
        ...
  • Initiate workers using the following command;
$ python manage.py celeryd -l info

Databases

  • To use Django-Model to manipulate databases, refer to this site;
  • To visualize the database in a descending order, which means the frontmost item being the most recent one, refer to this site

Deployment

Local Server

  • Refer to this site and this site to deploy a Django+Vue project on production environment;
  • If the console of the browser shows something like 'Resource interpreted as Stylesheet but transferred with MIME type text/plain', refer to this site to solve it.

GitHub

  • To deploy a repository on GitHub, refer to this site, basically after creating a project, pull a branch from it and replace all files with your own, then just commit;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
禁止转载,如需转载请通过简信或评论联系作者。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,547评论 4 374
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,787评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,175评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,752评论 0 223
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,169评论 3 297
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,056评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,195评论 2 321
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,980评论 0 214
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,724评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,872评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,344评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,667评论 3 264
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,379评论 3 245
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,202评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,992评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,189评论 2 286
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,987评论 2 279

推荐阅读更多精彩内容