Reader small image

You're reading from  Web Development with Django

Product typeBook
Published inFeb 2021
Reading LevelIntermediate
PublisherPackt
ISBN-139781839212505
Edition1st Edition
Languages
Tools
Right arrow
Authors (5):
Ben Shaw
Ben Shaw
author image
Ben Shaw

Ben Shaw is a software engineer based in Auckland, New Zealand. He has worked as a developer for over 14 years and has been building websites with Django since 2007. In that time, his experience has helped many different types of companies, ranging in size from start-ups to large enterprises. He is also interested in machine learning, data science, automating deployments, and DevOps. When not programming, Ben enjoys outdoor sports and spending time with his partner and son.
Read more about Ben Shaw

Saurabh Badhwar
Saurabh Badhwar
author image
Saurabh Badhwar

Saurabh Badhwar is an infrastructure engineer who works on building tools and frameworks that enhance developer productivity. A major part of his work involves using Python to develop services that scale to thousands of concurrent users. He is currently employed at LinkedIn and works on infrastructure performance tools and services.
Read more about Saurabh Badhwar

Andrew Bird
Andrew Bird
author image
Andrew Bird

Andrew Bird is the data and analytics manager of Vesparum Capital. He leads the software and data science teams at Vesparum, overseeing full-stack web development in Django/React. He is an Australian actuary (FIAA, CERA) who has previously worked with Deloitte Consulting in financial services. Andrew also currently works as a full-stack developer for Draftable Pvt. Ltd. He manages the ongoing development of the donation portal for the Effective Altruism Australia website on a voluntary basis. Andrew has also co-written one of our bestselling titles, "The Python Workshop".
Read more about Andrew Bird

Bharath Chandra K S
Bharath Chandra K S
author image
Bharath Chandra K S

Bharath Chandra K S lives in Sydney, Australia, and has over 14 years of software industry experience. He is very passionate about software development on the Python stack, including frameworks such as Flask and Django. He has experience working with both monolithic and microservice architectures and has built various public-facing applications and data processing backend systems. When not cooking up software applications, he likes to cook some nice food.
Read more about Bharath Chandra K S

Chris Guest
Chris Guest
author image
Chris Guest

Chris Guest is based in Melbourne and started programming in Python 24 years ago, when it was an obscure academic language. He has since used his Python knowledge in the publishing, hospitality, medical, academic and financial sectors. Throughout his career, he has worked with many Python web development frameworks, including Zope, TurboGears, web2py, and Flask, although he still prefers Django.
Read more about Chris Guest

View More author details
Right arrow

16. Using a Frontend JavaScript Library with Django

Overview

This chapter introduces the basics of JavaScript and ends with building an interactive web frontend for Bookr using the React JavaScript framework. You will learn how to include the React JavaScript framework in a Django template, and how to build React components. This chapter also includes an introduction to JSX, a special format that combines JavaScript code and HTML – you will also learn how Babel transpiles JSX into plain JavaScript. Later, you will learn about the fetch JavaScript function which is used to retrieve information from a REST API. Toward the end of the chapter, you will be introduced to the Django {% verbatim %} template tag, which is used to include unparsed data in a Django template.

Introduction

Django is a great tool for building the backend of an application. You have seen how easy it is to set up the database, route URLs, and render templates. Without using JavaScript, though, when those pages are rendered to the browser, they are static and do not provide any form of interaction. By using JavaScript, your pages can be transformed into applications that are fully interactive in the browser.

This chapter will be a brief introduction to JavaScript frameworks and how to use them with Django. While it won't be a deep dive into how to build an entire JavaScript application from scratch (that would be a book in itself), we will give enough of an introduction so that you can add interactive components to your own Django application. In this chapter, we will primarily be working with the React framework. Even if you do not have any JavaScript experience, we will introduce enough about it so that, by the end of this chapter, you will be comfortable writing your...

JavaScript Frameworks

These days, real-time interactivity is a fundamental part of web applications. While simple interactions can be added without a framework (developing without a framework is often called Vanilla JS), as your web application grows, it can be much easier to manage with the use of a framework. Without a framework, you would need to do all these things yourself:

  • Manually define the database schema.
  • Convert data from HTTP requests into native objects.
  • Write form validation.
  • Write SQL queries to save data.
  • Construct HTML to show a response.

Compare this to what Django provides. Its ORM (Object Relational Mapping), automatic form parsing and validation, and templating drastically cut down on the amount of code you need to write. JavaScript frameworks bring similar time-saving enhancements to JavaScript development. Without them, you would have to manually update the HTML elements in the browser as your data changes. Let's take a simple...

JavaScript Introduction

In this section, we will briefly introduce some basic JavaScript concepts, such as variables and functions. Different operators will be covered as we introduce them.

Loading JavaScript

JavaScript can either be inline in an HTML page or included from a separate JavaScript file. Both methods use the <script> tag. With inline JavaScript, the JavaScript code is written directly inside the <script> tags in an HTML file; for example, like this:

<script>
    // comments in JavaScript can start with //
    /* Block comments are also supported. This comment is multiple
      lines and doesn't end until we use a star then slash:
    */
    let a = 5; // declare the variable a, and set its value to 5
    console.log(a); // print a (5) to the browser console
</script>

Note that the console.log function...

Summary

In this chapter, we introduced JavaScript frameworks and described how they work with Django to enhance templates and add interactivity. We introduced the JavaScript language and covered some of its main features, variable types, and classes. We then introduced the concepts behind React and how it builds HTML by using components. We built a React component using just JavaScript and the React.createElement function. After that, we introduced JSX and saw how it made the development of components easier, by letting you directly write HTML in your React components. The concepts of promises and the fetch function were introduced, and we saw how to get data from a REST API using fetch. The chapter finished with an exercise that retrieved reviews from Bookr using the REST API and rendered them to the page in an interactive component.

In the next chapter, we will look at how to deploy our Django project to a production web server. You can download the chapter from the GitHub repository...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Web Development with Django
Published in: Feb 2021Publisher: PacktISBN-13: 9781839212505
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (5)

author image
Ben Shaw

Ben Shaw is a software engineer based in Auckland, New Zealand. He has worked as a developer for over 14 years and has been building websites with Django since 2007. In that time, his experience has helped many different types of companies, ranging in size from start-ups to large enterprises. He is also interested in machine learning, data science, automating deployments, and DevOps. When not programming, Ben enjoys outdoor sports and spending time with his partner and son.
Read more about Ben Shaw

author image
Saurabh Badhwar

Saurabh Badhwar is an infrastructure engineer who works on building tools and frameworks that enhance developer productivity. A major part of his work involves using Python to develop services that scale to thousands of concurrent users. He is currently employed at LinkedIn and works on infrastructure performance tools and services.
Read more about Saurabh Badhwar

author image
Andrew Bird

Andrew Bird is the data and analytics manager of Vesparum Capital. He leads the software and data science teams at Vesparum, overseeing full-stack web development in Django/React. He is an Australian actuary (FIAA, CERA) who has previously worked with Deloitte Consulting in financial services. Andrew also currently works as a full-stack developer for Draftable Pvt. Ltd. He manages the ongoing development of the donation portal for the Effective Altruism Australia website on a voluntary basis. Andrew has also co-written one of our bestselling titles, "The Python Workshop".
Read more about Andrew Bird

author image
Bharath Chandra K S

Bharath Chandra K S lives in Sydney, Australia, and has over 14 years of software industry experience. He is very passionate about software development on the Python stack, including frameworks such as Flask and Django. He has experience working with both monolithic and microservice architectures and has built various public-facing applications and data processing backend systems. When not cooking up software applications, he likes to cook some nice food.
Read more about Bharath Chandra K S

author image
Chris Guest

Chris Guest is based in Melbourne and started programming in Python 24 years ago, when it was an obscure academic language. He has since used his Python knowledge in the publishing, hospitality, medical, academic and financial sectors. Throughout his career, he has worked with many Python web development frameworks, including Zope, TurboGears, web2py, and Flask, although he still prefers Django.
Read more about Chris Guest