<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by William Lee on Medium]]></title>
        <description><![CDATA[Stories by William Lee on Medium]]></description>
        <link>https://medium.com/@williamgnlee?source=rss-593bf0e3cc12------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*cXbB-hzICsTGZjvQstaa7g.jpeg</url>
            <title>Stories by William Lee on Medium</title>
            <link>https://medium.com/@williamgnlee?source=rss-593bf0e3cc12------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 06:30:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@williamgnlee/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Simple Integrated Django/Vue.js Web Application Configured for Heroku Deployment]]></title>
            <link>https://medium.com/@williamgnlee/simple-integrated-django-vue-js-web-application-configured-for-heroku-deployment-c4bd2b37aa70?source=rss-593bf0e3cc12------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4bd2b37aa70</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[William Lee]]></dc:creator>
            <pubDate>Wed, 11 Jul 2018 15:14:27 GMT</pubDate>
            <atom:updated>2018-07-11T15:14:27.733Z</atom:updated>
            <content:encoded><![CDATA[<p>This post will:</p><ul><li>Show you how to deploy an integrated <a href="https://www.djangoproject.com/">Django</a> (backend APIs) and <a href="https://vuejs.org/">Vue.js</a> (frontend SPA) web application to a single <a href="https://www.heroku.com/">Heroku</a> Python server. The Vue.js single page application (SPA) will be built using the <a href="https://webpack.js.org/">Webpack</a> module bundler. Separate URL paths will expose the SPA (e.g. <a href="https://django-vue-template.herokuapp.com/">https://django-vue-template.herokuapp.com/</a>) and the Django APIs (e.g. <a href="https://django-vue-template.herokuapp.com/api_example/">https://django-vue-template.herokuapp.com/api_example/</a>).</li><li>Demonstrate how to configure Webpack environment variables for automatically handling API requests in development vs. production.</li></ul><p><em>The GitHub repository containing the completed template can be found </em><a href="https://github.com/Williamlee12/django_vue_template"><strong><em>here.</em></strong></a></p><p>I recently set about building a Django and Vue.js web application to be deployed on a <a href="https://www.heroku.com/">Heroku</a> Python server. I chose Django for the backend as it allows for the easy integration of numerous Python machine learning libraries. Vue.js was chosen for the frontend because it’s a simple component-based and reactive framework with great documentation. From my personal experience I can vouch that it takes minimal time to become familiar with its fundamental concepts. Furthermore, Vue.js’ GitHub star count recently overtook React’s and its ecosystem is expanding quickly:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*vLzLZWJeWXTy6P4ObPiP1A.png" /><figcaption>Vue.js overtakes React GitHub star count</figcaption></figure><h4>Step 1 — Set up Django:</h4><p>Firstly, set up and activate a Python virtual environment by running conda create --name django_vue_template python=3, followed bysource activate django_vue_template.*</p><p><em>* This paragraph outlines how to set up a Python virtual environment using </em><a href="https://conda.io/docs/"><em>Conda</em></a><em>. However, you should feel free to set up your Python virtual environment in your normal way.</em></p><p>Once the virtual environment is activated, run pip install django, then django-admin startproject django_vue_template**, and finally cd django_vue_template. You should now be located in the root of a Django project template.</p><p><em>** This project is called ‘django_vue_template’ but you should feel free to call your project whatever you like; just substitute your project name for ‘django_vue_template’ everywhere it appears later in this post.</em></p><h4>Step 2 — Set up Vue.js with Webpack:</h4><p><em>This tutorial assumes that you already have Vue CLI tools installed. If not, you can install them very simply by running </em><em>npm install -g vue-cli.</em></p><p>From the root of the Django project template run vue init webpack, then respond to the following prompts as follows:</p><ul><li><em>“Generate project in current directory?” — </em>Y</li><li><em>“Vue build” </em>— Runtime + Compiler: recommended for most users</li><li><em>“Should we run ‘npm install’ for you after the project has been created? (recommended)”</em> — Yes, use NPM</li></ul><p>For all of the other prompts respond as you would like; it’s not important for this tutorial. Once this has executed the file structure should look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/293/1*Fu_qMxkpmqjs0iOyVA2Cog.png" /></figure><h4>Step 3 — Configure a Django URL to render the Vue.js SPA:</h4><p>To configure Django to render the Vue.js SPA, in the project’s root first run npm run build. This will create a directory named <em>/dist/</em> containing an <em>index.html </em>template, which is a production-ready version of the Vue.js SPA.</p><p>Secondly, in the Django settings file <em>(/django_vue_template/settings.py) </em>add the <em>/dist/ </em>directory as a location that Django can search for the <em>index.html</em> template. Do this by adding <strong>os.path.join(BASE_DIR, ‘dist’) </strong>to the DIRS list in the TEMPLATES setting like this:</p><pre>TEMPLATES = [<br>    {<br>        &#39;BACKEND&#39;: &#39;django.template.backends.django.DjangoTemplates&#39;,<br>        &#39;DIRS&#39;: [<strong>os.path.join(BASE_DIR, &#39;dist&#39;)</strong>],<br>        &#39;APP_DIRS&#39;: True,<br>        &#39;OPTIONS&#39;: {<br>            &#39;context_processors&#39;: [<br>                &#39;django.template.context_processors.debug&#39;,<br>                &#39;django.template.context_processors.request&#39;,<br>                &#39;django.contrib.auth.context_processors.auth&#39;,<br>                &#39;django.contrib.messages.context_processors.messages&#39;,<br>            ],<br>        },<br>    },<br>]</pre><p>Next, define the location that the staticfiles app should travserse if collectstatic is run (this will happen automatically when the app is deployed to Heroku) by adding the following code to the Django settings file (<em>/django_vue_template/settings.py</em>):</p><pre>STATICFILES_DIRS = [<br>  os.path.join(BASE_DIR, &#39;dist/static&#39;),<br>]</pre><p>Finally, edit /<em>django_vue_template/urls.py </em>so that Django renders the <em>index.html </em>Vue.js SPA template located in <em>/dist/</em>when a request is made to the root URL. Do this by editing /<em>django_vue_template/urls.py </em>so that it looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6f5af9d97d7db770d075576dce75fb1/href">https://medium.com/media/c6f5af9d97d7db770d075576dce75fb1/href</a></iframe><p>Now, when the Django development server is started by executing python manage.py runserver, the Vue.js default project application template should be rendered at <a href="http://localhost:8000/">http://localhost:8000/</a>.</p><h4>Step 4 — Set up a simple Django API:</h4><p>In order to demonstrate how to make requests for data to a Django backend API from the Vue.js SPA, an example JSON API needs to be set up. To accomplish this run python manage.py startapp api_example from the project root, then replace the contents of the <em>/api_example/views.py </em>file with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/604a4c08b9c10e3d2e1477b71e47b7bd/href">https://medium.com/media/604a4c08b9c10e3d2e1477b71e47b7bd/href</a></iframe><p>Secondly, add a <em>urls.py</em> file to the <em>/api_example/ </em>directory containing:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e11657f7149b4a10fbcb24f1749867a7/href">https://medium.com/media/e11657f7149b4a10fbcb24f1749867a7/href</a></iframe><p>Finally, edit the /<em>django_vue_template/urls.py </em>so that it looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b116a55eeea7e516dea5cd43b0b9d99/href">https://medium.com/media/5b116a55eeea7e516dea5cd43b0b9d99/href</a></iframe><p>Now, when the Django development server is started by executing python manage.py runserver, a very basic API that responds with a basic JSON object when called — {“names”: [“William”, “Rod”, “Grant”]} — should be located at <a href="http://localhost:8000/api_example/">http://localhost:8000/api_example/</a>.</p><h4>Step 5 — Request Django API data from Vue.js SPA:</h4><p>Now that a basic JSON API is set up, we can create a basic Vue.js SPA that makes requests to it. To do this, delete the <em>/assets/ </em>and <em>/components/ </em>directories from the <em>/src/</em> directory. The file structure should now look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/291/1*PG20Uh7N_Rk_XTtZQz6M_w.png" /></figure><p>Next, install axios (a HTTP client for the browser and node.js that will be used for making requests) by running npm install axios --save from the project root.</p><p>Now create a basic Vue.js SPA by editing <em>/src/App.vue</em> so that it looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d040b88db5f29b1ba1db57c0fa77bb99/href">https://medium.com/media/d040b88db5f29b1ba1db57c0fa77bb99/href</a></iframe><p>Finally, run npm run build from the project root to update the <em>/dist/ </em>directory with a production-ready version of the new Vue.js SPA.</p><p>At this point, when the Django development server is started by executing python manage.py runserver, a very basic Vue.js SPA should be located at <a href="http://localhost:8000/">http://localhost:8000/</a> that will ‘Select a Winner’ when the button is clicked.</p><h4>Step 6 — Configure Webpack environment variables and for development vs. production:</h4><p>Developing a Vue.js SPA with the <a href="https://webpack.js.org/">Webpack</a> module bundler is great, as it comes with a built in development sever allowing developers to view edits to their frontend code in their browser in real time.</p><p>However, when you fire up the Webpack development server by running npm run dev from the project root you will notice that it starts up on a separate port to the Django development server.</p><p>This is a problem because the Vue.js SPA is currently written to request API data from the root URL + /api_example/:</p><pre>FetchData: function() {<br>          .....<br>          axios.get(<strong>&quot;/api_example/&quot;</strong>).then(.....);<br>      },</pre><p>Therefore, when using the Webpack development server the Vue.js SPA is making a request to <a href="http://localhost:8080/api_example">http://localhost:8080/api_example</a>/, which is not where the JSON API is located. This means the SPA does not work and no “winner” can be selected.</p><p>Luckily, by using <a href="https://webpack.js.org/guides/environment-variables/">Webpack environment variables</a> the Vue.js SPA can be configured to make requests to different locations in production vs. development.</p><p>To set these up, firstly edit <em>/config/dev.env.js </em>so that it looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68c8b7d87a3c9c4877ae1a1d89c517f5/href">https://medium.com/media/68c8b7d87a3c9c4877ae1a1d89c517f5/href</a></iframe><p>Secondly, edit <em>/config/prod.env.js </em>so that it looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/67bb966e868b1269e2c0392b97e77cf4/href">https://medium.com/media/67bb966e868b1269e2c0392b97e77cf4/href</a></iframe><p>Thirdly, edit the FetchData() method in <em>/src/App.vue</em> so that it looks like this:</p><pre>FetchData: function() {<br>          var app = this;<br>          axios.get(<strong>process.env.API_URL + &quot;/api_example/&quot;</strong>).then(response =&gt; {<br>              app.names = response.data.names;<br>          });<br>      }</pre><p>Finally, run npm run build. The Vue.js SPA will now make requests to the root URL+ /api_example/ in production and <a href="http://localhost:8000/api_example/">http://localhost:8000/api_example/</a> when using the Webpack development server.</p><h4>Step 7 — Allow cross-origin resource sharing in development:</h4><p>Fire up the Django and Webpack development servers simultaneously in two separate emulator windows from the root of the project by running python manage.py and npm run dev respectively. You will notice that the Vue.js SPA served by the Webpack development server at <a href="http://localhost:8080/">http://localhost:8080/</a> is still failing to request data successfully from the JSON API located at <a href="http://localhost:8000/api_example">http://localhost:8000/api_example</a>. This is because the Django backend is not locally configured to allow cross-origin resource sharing from <a href="http://localhost:8080/">http://localhost:8080</a>.</p><p>To resolve this, add the <a href="https://github.com/ottoyiu/django-cors-headers">django-cors-headers</a> library to the project by running pip install django-cors-headers, then add <strong>corsheaders</strong> to INSTALLED _APPS in <em>/django_vue_template/settings.py</em> like this:</p><pre>INSTALLED_APPS = (<br>    .....<br>    <strong>&#39;corsheaders&#39;,</strong><br>    .....<br>)</pre><p>Next, add <strong>corsheaders.middleware.CorsMiddleware</strong> to the MIDDLEWARE setting in <em>/django_vue_template/settings.py. </em>This should go above ‘django.middleware.common.CommonMiddleware’ like this:</p><pre>MIDDLEWARE = [<br>    .....<br>    <strong>&#39;corsheaders.middleware.CorsMiddleware&#39;,</strong><br>    &#39;django.middleware.common.CommonMiddleware&#39;,<br>    .....<br>]</pre><p>As we only want to allow cross-origin resource from <a href="http://localhost:8080/">http://localhost:8080</a> locally, add the following setting to the <em>/django_vue_template/settings.py </em>file:</p><pre>CORS_ORIGIN_ALLOW_ALL = False</pre><p>Following this, at the very bottom of the <em>/django_vue_template/settings.py </em>file insert:</p><pre>try:<br>    from local_settings import *<br>except ImportError:<br>    pass</pre><p>The above statement will try to import additional settings stored in a <em>/local_settings.py </em>file if one is present and pass if one does not exist. To take advantage of this, in the root of the project create a <em>local_settings.py </em>file containing:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74070c4d14ba1a5584a6ff667363f087/href">https://medium.com/media/74070c4d14ba1a5584a6ff667363f087/href</a></iframe><p>When the Django and Webpack development servers are now simultaneously fired up there should be a fully functioning version of the Vue.js SPA, served by the Webpack development server, located at <a href="http://localhost:8080/">http://localhost:8080/</a>.</p><h4>Step 8 — Configure and deploy to Heroku server:</h4><p>Before deploying the app to Heroku a couple more items need to be configured. Firstly, add Gunicorn and WhiteNoise to the project by running pip install gunicorn whitenoise.</p><p>Gunicorn operates as a Python WSGI HTTP server for UNIX and Whitenoise allows the application to serve its own static files when deployed, as Django does not support this in production.</p><p>At this point, because no more Python packages will be added, it makes sense to create a <em>/requirements.txt </em>file listing all of those that the project is dependent on. Do this by running pip freeze &gt; requirements.txt from the the project root.</p><p>Whitenoise should then be set up by editing the <em>/django_vue_template/wsgi.py</em> file so that it looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5faeb55fbdbf629c4c926eab552303e/href">https://medium.com/media/b5faeb55fbdbf629c4c926eab552303e/href</a></iframe><p>To make sure that no local resources are deployed to Heroku that should not be, replace the contents of the <em>/.gitignore</em> file with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/064627c05a17556fd244298c993df753/href">https://medium.com/media/064627c05a17556fd244298c993df753/href</a></iframe><p>Next, add a file called <em>Procfile </em>in the root of the project. This specifies the commands that are executed by the app’s <a href="https://devcenter.heroku.com/articles/dynos">dynos</a> when deployed on Heroku and in this case should contain:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c1f97771a91e24e7b8085892befa926/href">https://medium.com/media/2c1f97771a91e24e7b8085892befa926/href</a></iframe><p>It’s a good idea to use environment variables when deploying to Heroku so that sensitive pieces of information are not stored directly in your code. In the case of this project, this should be configured locally by creating an <em>.env </em>file in the project root containing:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f0e31cf85386d75a2c8b1fc410eefa49/href">https://medium.com/media/f0e31cf85386d75a2c8b1fc410eefa49/href</a></iframe><p>When working on this project in development now source .env will need to be run from the project root first to configure the environment variables.</p><p>Following these steps, visit <a href="https://www.heroku.com/">Heroku.com</a> and create a new app. Then visit the app’s ‘Settings’ tab and add the ‘heroku/nodejs’ and ‘heroku/python’ buildpacks, making sure that they are listed in this order:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tf7XafmK-W6HKYaHWo57rw.png" /></figure><p>The Python buildpack should be listed second as Heroku uses the last buildpack to determine the <a href="https://devcenter.heroku.com/articles/procfile">process types</a> for the application.</p><p>Next, make a note of the domain where the app will be located (also listed in the ‘Settings’ tab) and edit the Config Vars so that they contain the SECRET_KEY environment variable like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*trvzHH7nqZYW_NWKXHPFUQ.png" /><figcaption>Replace ‘YOUR_SECRET_KEY’ with the SECRET_KEY that was recorded in the local .env file earlier</figcaption></figure><p>Now return to the local project and adjust the DEBUG, SECRET_KEY, and ALLOWED_HOSTS settings in <em>/django_vue_template/settings.py </em>like this:</p><pre>DEBUG = False<br>SECRET_KEY = os.environ[&#39;SECRET_KEY&#39;]<br>ALLOWED_HOSTS = [&#39;django-vue-template.herokuapp.com&#39;, &#39;localhost&#39;]</pre><p><em>Instead of ‘django_vue_template.herokuapp.com’ list the Heroku domain your app will be located at, which you made a note of earlier</em></p><p>Next, add STATIC_ROOT (the absolute path to the directory where collectstatic will collect static files for deployment) and STATICFILES_STORAGE (The file storage engine to use when collecting static files with the <a href="https://docs.djangoproject.com/en/2.0/ref/contrib/staticfiles/#django-admin-collectstatic">collectstatic</a> management command) settings to <em>django_vue_template/settings.py </em>like this:</p><pre>STATIC_ROOT = os.path.join(BASE_DIR, &#39;staticfiles&#39;)<br>STATICFILES_STORAGE = &#39;whitenoise.django.GzipManifestStaticFilesStorage&#39;</pre><p>Now, add <strong>“postinstall”: “npm run build”</strong> to the /<em>package.json</em> scripts object so that Node.js knows to build the Vue.js SPA when deployed like this:</p><pre>&quot;scripts&quot;: {<br>    &quot;dev&quot;: &quot;webpack-dev-server --inline --progress --config build/webpack.dev.conf.js&quot;,<br>    &quot;start&quot;: &quot;npm run dev&quot;,<br>    &quot;build&quot;: &quot;node build/build.js&quot;,<br>    <strong>&quot;postinstall&quot;: &quot;npm run build&quot;</strong><br>  }</pre><p>Next, push the entire project to GitHub in the normal way and then link to the appropriate repository in the Heroku app’s ‘Deploy’ tab like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BPQJgu-kr3Y4jayBmZ_4PQ.png" /></figure><p>Finally, deploy the application by clicking ‘Deploy Branch’:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y02KE2cuGO4NcWpr_1DKrw.png" /></figure><p>I hope that you found this post useful. Please feel free to comment with any thoughts!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4bd2b37aa70" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>