Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Web Development with Django

You're reading from  Web Development with Django

Product type Book
Published in Feb 2021
Publisher Packt
ISBN-13 9781839212505
Pages 826 pages
Edition 1st Edition
Languages
Authors (5):
Ben Shaw Ben Shaw
Profile icon Ben Shaw
Saurabh Badhwar Saurabh Badhwar
Profile icon Saurabh Badhwar
Andrew Bird Andrew Bird
Profile icon Andrew Bird
Bharath Chandra K S Bharath Chandra K S
Profile icon Bharath Chandra K S
Chris Guest Chris Guest
Profile icon Chris Guest
View More author details

Table of Contents (17) Chapters

Preface
1. Introduction to Django 2. Models and Migrations 3. URL Mapping, Views, and Templates 4. Introduction to Django Admin 5. Serving Static Files 6. Forms 7. Advanced Form Validation and Model Forms 8. Media Serving and File Uploads 9. Sessions and Authentication 10. Advanced Django Admin and Customizations 11. Advanced Templating and Class-Based Views 12. Building a REST API 13. Generating CSV, PDF, and Other Binary Files 14. Testing 15. Django Third-Party Libraries 16. Using a Frontend JavaScript Library with Django

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
arrow left Previous Chapter
You have been reading a chapter from
Web Development with Django
Published in: Feb 2021 Publisher: Packt ISBN-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.
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 $15.99/month. Cancel anytime}