Skip to content

Commit 8a5c552

Browse files
committed
Django + webpack + Vue.js - setting up a new project that's easy to develop and deploy (part 1)
https://ariera.github.io/2017/09/26/django-webpack-vue-js-setting-up-a-n ew-project-that-s-easy-to-develop-and-deploy-part-1.html
1 parent 087464e commit 8a5c552

File tree

9 files changed

+73
-20
lines changed

9 files changed

+73
-20
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ local_settings.py
2929
db.sqlite3
3030

3131

32+
webpack-stats.json
33+
public/
34+
dist/

README.md

+24-11
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,36 @@
22

33
> A Vue.js and Django example
44
5-
## Build Setup
5+
This repo goes along with the first part series of a blogpost I wrote some time ago on how to effectively [setup a project that's easy to develop and deploy using Django, Vue and webpack](https://ariera.github.io/2017/09/26/django-webpack-vue-js-setting-up-a-new-project-that-s-easy-to-develop-and-deploy-part-1.html).
66

7-
``` bash
8-
# install dependencies
9-
npm install
7+
## Installation
8+
9+
```bash
10+
yarn install
11+
python3 -m venv .venv
12+
source .venv/bin/activate
13+
pip install -r requirements.txt
14+
```
1015

11-
# serve with hot reload at localhost:8080
16+
## Development
17+
18+
* On one terminal run your webpack dev server
19+
20+
```bash
1221
npm run dev
22+
```
1323

14-
# build for production with minification
15-
npm run build
24+
* On another terminal launch the Django application
1625

17-
# build for production and view the bundle analyzer report
18-
npm run build --report
26+
```bash
27+
python manage.py runserver
28+
```
29+
30+
And point your browser to [http://localhost:8000](), but test that you can also access the pure Django world, for example by going to the admin panel [http://localhost:8000/admin]()
31+
32+
## Tests
1933

34+
``` bash
2035
# run unit tests
2136
npm run unit
2237

@@ -26,5 +41,3 @@ npm run e2e
2641
# run all tests
2742
npm test
2843
```
29-
30-
For a detailed explanation on how things work, check out the [guide](http://vuejs-templates.github.io/webpack/) and [docs for vue-loader](http://vuejs.github.io/vue-loader).

build/webpack.base.conf.js

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path')
33
const utils = require('./utils')
44
const config = require('../config')
55
const vueLoaderConfig = require('./vue-loader.conf')
6+
const BundleTracker = require('webpack-bundle-tracker')
67

78
function resolve (dir) {
89
return path.join(__dirname, '..', dir)
@@ -13,6 +14,9 @@ module.exports = {
1314
entry: {
1415
app: './src/main.js'
1516
},
17+
plugins: [
18+
new BundleTracker({filename: './webpack-stats.json'}),
19+
],
1620
output: {
1721
path: config.build.assetsRoot,
1822
filename: '[name].js',
@@ -25,6 +29,7 @@ module.exports = {
2529
alias: {
2630
'vue$': 'vue/dist/vue.esm.js',
2731
'@': resolve('src'),
32+
'__STATIC__': resolve('static'),
2833
}
2934
},
3035
module: {

build/webpack.dev.conf.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ const devWebpackConfig = merge(baseWebpackConfig, {
3232
quiet: true, // necessary for FriendlyErrorsPlugin
3333
watchOptions: {
3434
poll: config.dev.poll,
35-
}
35+
},
36+
headers: {
37+
"Access-Control-Allow-Origin": "\*",
38+
},
3639
},
3740
plugins: [
3841
new webpack.DefinePlugin({

config/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99

1010
// Paths
1111
assetsSubDirectory: 'static',
12-
assetsPublicPath: '/',
12+
assetsPublicPath: 'http://localhost:8080/',
1313
proxyTable: {},
1414

1515
// Various Dev Server settings
@@ -47,15 +47,15 @@ module.exports = {
4747
// just be aware of this issue when enabling this option.
4848
cssSourceMap: false,
4949
},
50-
50+
5151
build: {
5252
// Template for index.html
5353
index: path.resolve(__dirname, '../dist/index.html'),
5454

5555
// Paths
56-
assetsRoot: path.resolve(__dirname, '../dist'),
57-
assetsSubDirectory: 'static',
58-
assetsPublicPath: '/',
56+
assetsRoot: path.resolve(__dirname, '../dist/'),
57+
assetsSubDirectory: '',
58+
assetsPublicPath: '/static/',
5959

6060
/**
6161
* Source Maps
@@ -64,14 +64,14 @@ module.exports = {
6464
productionSourceMap: true,
6565
// https://webpack.js.org/configuration/devtool/#production
6666
devtool: '#source-map',
67-
67+
6868
// Gzip off by default as many popular static hosts such as
6969
// Surge or Netlify already gzip all static assets for you.
7070
// Before setting to `true`, make sure to:
7171
// npm install --save-dev compression-webpack-plugin
7272
productionGzip: false,
7373
productionGzipExtensions: ['js', 'css'],
74-
74+
7575
// Run the build command with an extra argument to
7676
// View the bundle analyzer report after build finishes:
7777
// `npm run build --report`

project/settings.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
# Application definition
3232

3333
INSTALLED_APPS = [
34+
'webpack_loader',
3435
'django.contrib.admin',
3536
'django.contrib.auth',
3637
'django.contrib.contenttypes',
@@ -54,7 +55,9 @@
5455
TEMPLATES = [
5556
{
5657
'BACKEND': 'django.template.backends.django.DjangoTemplates',
57-
'DIRS': [],
58+
'DIRS': [
59+
os.path.join(BASE_DIR, 'templates'),
60+
],
5861
'APP_DIRS': True,
5962
'OPTIONS': {
6063
'context_processors': [
@@ -118,3 +121,14 @@
118121
# https://docs.djangoproject.com/en/1.11/howto/static-files/
119122

120123
STATIC_URL = '/static/'
124+
STATICFILES_DIRS = (
125+
os.path.join(BASE_DIR, 'dist'),
126+
os.path.join(BASE_DIR, 'static'),
127+
)
128+
STATIC_ROOT = os.path.join(BASE_DIR, 'public')
129+
WEBPACK_LOADER = {
130+
'DEFAULT': {
131+
'BUNDLE_DIR_NAME': '',
132+
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
133+
}
134+
}

project/urls.py

+2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"""
1616
from django.conf.urls import url
1717
from django.contrib import admin
18+
from django.views.generic import TemplateView
1819

1920
urlpatterns = [
2021
url(r'^admin/', admin.site.urls),
22+
url(r'^$', TemplateView.as_view(template_name='project/spa.html'), name='home'),
2123
]

templates/project/base.html

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% load render_bundle from webpack_loader %}
2+
<html>
3+
<body>
4+
{% block content %}
5+
{% endblock %}
6+
{% render_bundle 'app' %}
7+
</body>
8+
</html>

templates/project/spa.html

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<!-- ./templates/my_project/spa.html -->
2+
{% extends "project/base.html" %}
3+
{% block content %}
4+
<div id="app"></div>
5+
{% endblock %}

0 commit comments

Comments
 (0)