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

15. Django Third-Party Libraries

Overview

This chapter introduces you to Django third-party libraries. You will configure your database connection using URLs with dj-database-urls and inspect and debug your application with the Django Debug Toolbar. Using django-crispy-forms, you will enhance the look of your forms, as well as reduce the amount of code you have to write by using the crispy template tag. We will also cover the django-allauth library, which lets you authenticate users against third-party providers. In the final activity, we will enhance Bookr's forms with the use of django-crispy-forms.

Introduction

Because Django has been around since 2007, there is a rich ecosystem of third-party libraries that can be plugged into an application to give it extra features. So far, we have learned a lot about Django and used many of its features, including database models, URL routing, templating, forms, and more. We used these Django tools directly to build a web app, but now we will look at how to leverage the work of others to quickly add even more advanced features to our own apps. We have alluded to apps for storing files, (in Chapter 5, Serving Static Files, we mentioned an app, django-storages, that can store our static files in a CDN), but in addition to file storage, we can also use them to plug into third-party authentication systems, integrate with payment gateways, customize how our settings are built, modify images, build forms more easily, debug our site, use different types of databases, and much more. Chances are, if you want to add a certain feature, an app exists...

django-crispy-forms

In Bookr, we are using the Bootstrap CSS framework. It provides styles that can be applied to forms using CSS classes. Since Django is independent of Bootstrap, when we use Django forms, it does not even know that we are using Bootstrap and so has no idea of what classes to apply to form widgets.

django-crispy-forms acts as an intermediary between Django Forms and Bootstrap forms. It can take a Django form and render it with the correct Bootstrap elements and classes. It not only supports Bootstrap but also other frameworks such as Uni-Form and Foundation (although Foundation support must be added through a separate package, crispy-forms-foundation).

Its installation and setup are quite simple. Once again, it is installed with pip3:

pip3 install django-crispy-forms 

Note

For Windows, you can use pip instead of pip3 in the preceding command.

Then there are just a couple of settings changes. First, add crispy_forms to your INSTALLED_APPS. Then...

Summary

In this chapter, we introduced five third-party Django apps that can enhance your website. We installed and set up django-configurations, which allowed us to easily switch between different settings and change them using environment variables. dj-database-url also helped with settings, allowing us to make database settings changes using URLs. We saw how the Django Debug Toolbar could help us see what our app was doing and help us debug problems we were having with it. django-crispy-forms can not only render our forms using the Bootstrap CSS but also lets us save code by defining their behavior as part of the form class itself. We briefly looked at django-allauth and saw how it can be integrated into third-party authentication providers. In the activity for this chapter, we updated our ModelForm instances to use the django-crispy-forms FormHelper and remove some logic from the template by using the crispy template tag.

In the next chapter, we will look at how to integrate...

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 $15.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