Django and Modern JS Libraries - Svelte

written by
Can Burak S.
Temmuz 22, 2020
Django Svelte

I'm a webmaster who buit websites and e-commerce stores. I like web more than mobile. Even though it is not a finished list of my works, you can check my business websites portfolio.

In the previous part, we built a Django backend and GraphQL API. In this part, we will integrate the Django project with Svelte.

Thus, it's necessary to follow Django and Modern JS Frameworks - Backend.


What is Svelte and How it differs from React?

I like Python and its ecosystem. I also like Just-In-Time compilers and language supersets like Cython, which really boosts Python performance. When I learned that JavaScript is an interpreted language, I tried to look Cython equivalent of it.

Because of different browser compilers, I couldn’t find what I want and it made a disappointment. Maybe it is the reason I feel the excitement when I give Svelte a chance.

If you didn't try Svelte before, you may give it a chance. Svelte's interactive API and tutorials are also worth to praise. Being familiar with Svelte API and Tutorials is definitely recommended.

When I’m talking about Svelte, I’m strictly speaking about Svelte 3. It is another JavaScript library written by Rich Harris. What makes Svelte special is:

  • It is truly a reactive library and it doesn’t use virtual DOM like React. Therefore, there are no VDOM diff calculations.
  • It has a compiler and when you build your application it produces optimized JavaScript code. In the end, Svelte code almost disappears and you have vanilla JavaScript.
  • You can write HTML, CSS, and JavaScript in a single file component and there will be no global CSS pollution.

Yes, React was revolutionary. However, how many times we have to deal with virtual DOM synchronization problems or the extra burden for even very small operations are the other side of the medallion.


Svelte Configuration with Webpack from Scratch

Step - 1: Configuring the development environment

(Note: if you already installed the node, you can skip this part)

We will use Node backend for the development environment.

Therefore, we need to install Node and Node package manager npm.

To prevent potential dependency problems, we will create a clean node environment.

I will use NVM which is a Node version manager, and it allows us to create isolated Node environments.

In your terminal, run the code below.

Setup Node Environment with NVM

In your terminal, run the code below.

# install node version manager wget -qO- <https: raw.githubusercontent.com="" nvm-sh="" nvm="" v0.35.3="" install.sh=""> | bash # check installation command -v nvm # should prints nvm, if it doesn't, restart your terminal </https:>
Code language: Bash (bash)
# install node # node" is an alias for the latest version # use the installed version nvm use node nvm install node
Code language: Bash (bash)

Now we can create a frontend directory in the Django project. Go to the root directory of the project. 'backend/'

In your terminal copy and paste the code.

# create frontend directory mkdir FRONTEND cd FRONTEND # now your terminal directory should be # backend/FRONTEND # create a node project npm init # you may fill the rest
Code language: PHP (php)

Now we can install front-end and development libraries.

# install svelte and other libs npm install --save-dev svelte serve cross-env graphql-svelte # install webpack and related libs npm install --save-dev webpack webpack-cli webpack-dev-server # install webpack loaders and plugins npm install --save-dev style-loader css-loader svelte-loader mini-css-extract-plugin npm install --save node-fetch svelte-routing
Code language: PHP (php)

Update package.json scripts part as below. Your file should look like this and ignore the versions.

{ "name": "django-svelte-template", "description": "Django Svelte template. ", "main": "index.js", "scripts": { "build": "cross-env NODE_ENV=production webpack", "dev": "webpack-dev-server --content-base ../templates" }, "devDependencies": { "cross-env": "^7.0.2", "css-loader": "^3.5.3", "graphql-svelte": "^1.1.9", "mini-css-extract-plugin": "^0.9.0", "serve": "^11.3.1", "style-loader": "^1.2.1", "svelte": "^3.22.3", "svelte-loader": "^2.13.6", "webpack": "^4.43.0", "webpack-cli": "^3.3.11", "webpack-dev-server": "^3.11.0" }, "dependencies": { "node-fetch": "^2.6.0", "svelte-routing": "^1.4.2" } }
Code language: JSON / JSON with Comments (json)

Let’s create application necessary files and folders for Svelte. In the root directory of the project 'backend/’ , open your terminal.

# create HTML file of the project cd templates touch index.html # change directory to backend/FRONTEND cd ../FRONTEND mkdir src touch index.js touch webpack.config.js # change directory to backend/FRONTEND/src cd src touch App.svelte touch MovieList.svelte touch MoviePage.svelte touch api.js
Code language: PHP (php)

Step 2 - Webpack configuration

What is webpack ?

Webpack is a module bundler and a task runner.

We will bundle all our JavaScript applications including CSS styling into two JavaScript files, if you prefer you can output only one file.

Because of the rich plugins, you can also do many things with Webpack like compressing with different algorithms, eliminate unused CSS code, extracting your CSS to different files, uploading your bundle to cloud providers like S3, etc…

I made two different Webpack settings in one file.

One is for a development environment, and the other one is for a production environment.

Also, note that we do not optimize these configurations.

Copy/Paste the following code into the webpack.config.js file.

const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const path = require('path'); const mode = process.env.NODE_ENV || 'development'; const isEnvProduction = mode === 'production'; const productionSettings = { mode, entry: { bundle: ['./index.js'] }, resolve: { alias: { svelte: path.resolve('node_modules', 'svelte') }, extensions: ['.mjs', '.js', '.svelte'], mainFields: ['svelte', 'browser', 'module', 'main'] }, output: { path: path.resolve(__dirname, '../static'), filename: 'js/[name].js', chunkFilename: 'js/[name].[id].js' }, optimization: { minimize: true, runtimeChunk: false, }, module: { rules: [ { test: /\.svelte$/, use: { loader: 'svelte-loader', options: { emitCss: true, hotReload: true } } }, { test: /\.css$/, use: [ /** * MiniCssExtractPlugin doesn't support HMR. * For developing, use 'style-loader' instead. * */ MiniCssExtractPlugin.loader, 'css-loader' ] } ] }, devtool: false, plugins: [ new MiniCssExtractPlugin({filename: '[name].css'}) ], }; const devSettings = { mode, entry: { bundle: ['./index.js'] }, resolve: { alias: { svelte: path.resolve('node_modules', 'svelte') }, extensions: ['.mjs', '.js', '.svelte'], mainFields: ['svelte', 'browser', 'module', 'main'] }, output: { publicPath: "/", filename: 'static/js/bundle.js', chunkFilename: 'static/js/[name].chunk.js', }, devtool: 'source-map', devServer: { historyApiFallback: true, stats: 'minimal', }, module: { rules: [ { test: /\.svelte$/, use: { loader: 'svelte-loader', options: { emitCss: true, hotReload: true } } }, { test: /\.css$/, use: [ /** * MiniCssExtractPlugin doesn't support HMR. * For developing, use 'style-loader' instead. * */ 'style-loader', 'css-loader' ] } ] }, mode, plugins: [ ], } module.exports = isEnvProduction ? productionSettings : devSettings;
Code language: JavaScript (javascript)

Step 3 - Create a Single-Page-App witth Svelte

First, fill the 'backend/FRONTEND/index.js’.

import App from './src/App.svelte'; const app = new App({ target: document.body, }); window.app = app; export default app;
Code language: JavaScript (javascript)

Next, fill the ‘App.svelte’ file with proper logic.

<!-- App.svelte --> <script> import { Router, Link, Route } from "svelte-routing"; import MovieList from "./MovieList.svelte"; import MoviePage from "./MoviePage.svelte"; export let url = ""; </script> <Router url="{url}"> <nav class="navbar"> <Link to="/">Home</Link> </nav> <div class="main-container"> <Route path="movie/:slug" component="{MoviePage}" /> <Route path="/"><MovieList /></Route> </div> </Router> <style> .navbar { background-color:rgba(0,0,0,0.6); display: flex; padding: 16px 64px; font-weight: bold; color:white; } .main-container { margin-top:32px; display:flex; justify-content: center; align-items: center; background-color: rgba(0,0,0, 0.15); } </style>
Code language: HTML, XML (xml)

Before routing pages, I will first write the client-side queries. Please open api.js and copy/paste the code below.

<code>import { GraphQLProvider, reportCacheErrors } from "graphql-svelte"; const client = GraphQLProvider({ url: 'http://127.0.0.1:8000/graphql', headers: () => ({ "content-type": "application/json", Accept: 'application/json' }) }) client.graphql.on('cache', reportCacheErrors) // our first query will requests all movies // with only given fields // note the usage of string literals (`) export const MOVIE_LIST_QUERY = `query movieList{movieList{name, posterUrl, slug}} ` // Note the usage of argument. // the exclamation mark makes the slug argument as required // without it , argument will be optional export const MOVIE_QUERY = `query movie($slug:String!){ movie(slug:$slug){ id, name, year, summary, posterUrl, slug } } ` // This is generic query function // We will use this with one of the above queries and // variables if needed export async function get(query, variables = null) { const response = await client.get({ query , variables }) console.log("response", response); return response } </code>
Code language: JavaScript (javascript)

Now, route pages: MovieList.svelte will be shown on the homepage as we defined in the above. If the user clicks any movie card, then MoviePage.svelte file will be rendered.

Fill the MovieList.svelte.

<script> import { Router, Link, Route } from "svelte-routing"; import { get, MOVIE_QUERY, MOVIE_LIST_QUERY } from "./api.js"; var movielist = get(MOVIE_LIST_QUERY); </script><script> import { Router, Link, Route } from "svelte-routing"; import { get, MOVIE_QUERY, MOVIE_LIST_QUERY } from "./api.js"; var movielist = get(MOVIE_LIST_QUERY); </script> <div class="wrapper"> <!-- promise is pending --> {#await movielist} loading <!-- promise was fulfilled --> {:then response} {#if response.data.movieList.length > 0} {#each response.data.movieList as movie} <div class="card"> <Link to={`/movie/${movie.slug}`}> <img class="poster" alt={movie.name} src={movie.posterUrl} /> <p class="movie-title">{movie.name}</p> </Link> </div> {/each} {/if} <!-- promise was rejected --> {:catch error} <p>Something went wrong: {error.message}</p> {/await} </div> <style> .wrapper { width:100%; height: auto; display:flex; flex-direction: row; flex-wrap: wrap; } .card { box-sizing: border-box; position: relative; width:200px; height:auto; margin:16px; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 4px rgba(0,0,0,0.25); } .poster { width:100%; height:auto; cursor: pointer; } .movie-title { padding:4px 8px; font-weight: bold; text-decoration: none; cursor: pointer; } </style>
Code language: HTML, XML (xml)

Also, fill MoviePage.svelte according to this.

<code><code>import { Router, Link, Route } from "svelte-routing"; import { get, MOVIE_QUERY, MOVIE_LIST_QUERY } from "./api.js"; var movielist = get(MOVIE_LIST_QUERY); </script> <div class="wrapper"> <!-- promise is pending --> {#await movielist} loading <!-- promise was fulfilled --> {:then response} {#if response.data.movieList.length > 0} {#each response.data.movieList as movie} <div class="card"> <Link to={`/movie/${movie.slug}`}> <img class="poster" alt={movie.name} src={movie.posterUrl} /> <p class="movie-title">{movie.name}</p> </Link> </div> {/each} {/if} <!-- promise was rejected --> {:catch error} <p>Something went wrong: {error.message}</p> {/await} </div> <style> .wrapper { width:100%; height: auto; display:flex; flex-direction: row; flex-wrap: wrap; } .card { box-sizing: border-box; position: relative; width:200px; height:auto; margin:16px; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 4px rgba(0,0,0,0.25); } .poster { width:100%; height:auto; cursor: pointer; } .movie-title { padding:4px 8px; font-weight: bold; text-decoration: none; cursor: pointer; } </style> </code> </code>
Code language: JavaScript (javascript)

Start the Svelte App in Development Environment

In the development environment, we will run two different servers.

When our Svelte app is running, it requests data from the Django server.

After the response comes, the Webpack Development server renders the page with proper data. This is only for the development stage.

When we finish the front-end development, we will build and bundle a client-side app.

After then we will start the Django server, and it will be the only server we will use in the production environment, as I promise before.

Execute the below command and make the Django server ready for front-end requests.

# execute it on the root folder of Django 'backend/' python manage.py runserver
Code language: PHP (php)

Open another terminal and change the directory to the ‘backend/FRONTEND’

# On another terminal npm run start
Code language: PHP (php)

When Svelte app successfully compiled, open your browser ‘localhost:8080/’.

You should see similar screen like the image below.

MovieList.svelte will render the screen

MoviePage.svelte screen will render this if the user clicks any movie card

What will be happened at the moment?

At this moment, “/“ root page will be rendered. Because of our routing configurations, MovieList.svelte file will be rendered first. If the user clicks any film card, then the MoviePage.svelte file will be rendered regarding its slug value.

We succesfully integrated Django and Svelte. Now make the production build.


Django and Svelte Integration in Production Environment

Now you can stop webpack server while keeping the Django server alive.

In the backend/FRONTEND/ directory, execute the command below.

npm run build

This will build and bundle all your Svelte app in bundle.js file. When bundling process is over, go to the URL of Django server in your browser. --> “127.0.0.1:8000/”

You should see the same screens above. Also note the static folder which has new files coming from webpack bundling.


FINISHED


Şubat 3, 2021
Is it Safe to Travel through Istanbul Airport? Read More
Şubat 1, 2021
Works Read More
Temmuz 23, 2020
25+ En İyi Blog Siteleri ve Blog Açacaklara Tavsiyeler [2021] Read More
cbsofyalioglu logo
DESIGN•DEVELOPMENT•SEO