Skip to content

Boilerplate for a simple app using Django with REST + MySQL + Webpack + Vue.js

License

Notifications You must be signed in to change notification settings

g-viet/djangovue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

A simple app using Django with REST + MySQL + Webpack + Vue.js

Tutorial

  1. Init project & run development server:
# Ref: https://docs.djangoproject.com/en/2.1/intro/tutorial01/
# Init project
django-admin startproject djangosample

# Migrate Django app
python3 manage.py migrate

# Try running development server
python3 manage.py runserver
  1. Create Django app:
# Create Django app
python3 manage.py startapp invoice

# Create model
# https://docs.djangoproject.com/en/2.0/topics/db/models/

# Make migration
python3 manage.py makemigrations

# Migrate
python3 manage.py migrate
  1. Install Django-rest-framework
# Ref: http://www.django-rest-framework.org/
pip3 install djangorestframework
  1. Create Serializer, Viewset and Routers
  • Django-rest-framework: ref
  • Viewsets: ref
  • Routers: ref

Then check in browser: http://127.0.0.1:8000/invoice

  1. Install VueJs with webpack template
# Install in frontend folder
vue init webpack frontend

# Confirm installation finished:
cd frontend && npm run dev

# then access http://127.0.0.1:8080 in browser
  1. Enabling CORS on Django
pip3 install django-cors-headers
  • Add to your_project/settings.py:
INSTALLED_APPS = [
    # ...
    'corsheaders',
]

MIDDLEWARE = [
    # ...
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
]

CORS_ORIGIN_WHITELIST = (
    'localhost:8080',
    '127.0.0.1:8080',
)
  1. Make a sample REST API:
  • Add below code to invoice/views:
from rest_framework.decorators import api_view
from django.http import HttpResponse

def sayHello(request):
    return HttpResponse("Hello")
  • Add below code to your_project/urls:
from django.conf.urls import url
from invoice import views

urlpatterns = [
    url(r'^api/say_hello/', views.sayHello),
]
  1. Make sample interface in Vue:
  • Install axios :
cd frontend && npm install --save axios
  • Create file ./frontend/src/model/ModelService.js:
import axios from 'axios'

export default class ModelService {
  sayHello () {
    return axios.get('http://localhost:8000/api/public/', {headers: {}}).then((response) => {
      return response.data || 'default value'
    })
  }
}
  1. Create sample home page: Check App.vue file

  2. Confirm:

python manage.py runserver

cd frontend

# run Vue.js in another terminal tab
npm run dev

Ref:

About

Boilerplate for a simple app using Django with REST + MySQL + Webpack + Vue.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published