Explore 1.5M+ audiobooks & ebooks free for days

Only $12.99 CAD/month after trial. Cancel anytime.

Building Websites with Django: Build and Deploy Professional Websites with Python Programming and the Django Framework (English Edition)
Building Websites with Django: Build and Deploy Professional Websites with Python Programming and the Django Framework (English Edition)
Building Websites with Django: Build and Deploy Professional Websites with Python Programming and the Django Framework (English Edition)
Ebook400 pages3 hours

Building Websites with Django: Build and Deploy Professional Websites with Python Programming and the Django Framework (English Edition)

Rating: 0 out of 5 stars

()

Read preview

About this ebook

‘Building Websites with Django’ book teaches readers to develop their high-quality, feature-rich website by learning Django and its various tools. You will learn the best techniques to develop a dynamic website, right from scratch. This book focuses not only on just creating a particular application but rather develops a strong understanding of theoretical concepts with rich examples. You will learn to troubleshoot errors, develop navigation panels and add advanced functionalities like deploying on heroku server. You will read about models, templates, different types of views. You will learn to create apps and learn how to integrate different apps.

By the end of this book, You will create a project from scratch and will deploy it as a public website by yourself.
LanguageEnglish
PublisherBPB Online LLP
Release dateFeb 16, 2021
ISBN9789389328295
Building Websites with Django: Build and Deploy Professional Websites with Python Programming and the Django Framework (English Edition)

Related to Building Websites with Django

Related ebooks

Internet & Web For You

View More

Reviews for Building Websites with Django

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Building Websites with Django - Awanish Ranjan

    CHAPTER 1

    What is Django?

    Introduction

    Python is one of the most versatile programming languages used in the industry today. Because of Python’s simplicity, a programmer gets to focus more on the solution to the problem than on the solution to be implemented. Since it is open-source, we have a huge collection of free and open-source libraries, frameworks, modules, etc. available to use. Django is one of them. Before moving forward, you must have an understanding of frameworks and what Django can offer so that the outcomes match your expectations.

    In short, Django is a framework build on Python and it is used for web application development. It is available for free and is open source. You will learn about the basic concepts of a framework. You will know what a framework is and see how Django provides a solution to your web application development requirements.

    Structure

    What is a framework?

    Why do we need a web framework?

    What are some famous web frameworks?

    Is learning Django worth it?

    What is a virtual environment?

    How to create and use a virtual environment?

    How to install Django?

    How to create a Django project?

    Overview of your Django project files

    Objectives

    Understanding the concept of frameworks.

    Understanding the need for frameworks.

    Familiarizing with the benefits of Django.

    Understanding the concept of virtual environments.

    Learning to install Django.

    Creating a Django project

    Understanding of the file system of Django

    Django overview

    Django is famous for its ability to create web applications rapidly. How does this happen? Django has inbuilt middleware and other stuff needed to run a web application. All you need to do is focus on your application. Django very elegantly provides the settings and connections in a configurable manner. You can simply create your applications and configure their settings as per your requirement and you are good to go.

    Django was developed in 2003 by web developers at Lawrence Journal-World newspaper, Adrian Holovaty, and Simon Willison. It was released publicly under a BSD license in July 2005. The framework is named after the guitarist Django Reinhardt. Now, the Django Software Foundation has been maintaining Django. This official website for Django is https://www.djangoproject.com/. This is where you can get all the latest updates about Django.

    The best thing about Django is that it is very elegantly documented. It is easy to learn and implement. You can find the latest documentation at https://docs.djangoproject.com/en/2.2/. Currently, as of September 2019, Django 2.2 is the latest version. The other widely used version is Django 1.8. I would recommend that you stick to the latest version.

    What is a framework?

    Let us suppose you have a simple task of drawing a square of side measuring 5cm. You can take your instruments, measure 5cms, and draw your square. Now, if you are asked to draw 1000 such squares, will you follow the same process, not likely? You would create a square frame of the given measurements and use that frame to draw the 1000 squares.

    A software framework is very similar to this. Software development has seen a significant history and the requirements of a kind of software are pretty much the same. In a nutshell, a web application needs authentication and authorization, homepage, database connectivity, HTML pages for frontend, CSS for styling, a URL mapper, and a controller to choose what action is to be performed based on the request, etc.

    These needs of a software developer or in this case, a web application developer has been identified and the setup has been created (like the frame in the squares example) where a web app developer can easily configure the settings (like the measurements in the squares example) and get the job done quickly and with ease.

    Why do we need a framework?

    The understanding of the framework makes it pretty much clear why we use frameworks. Let’s quantify the advantages of using a framework:

    It sets an industry-recognized software design structure. If every developer uses different designs, then it becomes difficult for the software owner or the client to maintain the software once the initial developer leaves the project. Thus, using a framework ensures that a standard design is followed.

    Re-writing code for common functionalities that are already available in frameworks does not make sense. Also, frameworks allow us to modify the functionalities as per our needs. So, customization is easy.

    The frameworks have been present for quite some time and extensively tested by developers worldwide, issues are reported, and bug fixes are continuously rolled. This ensures the software is bug-free.

    The frameworks keep updating with new features as the technology advances. Using a framework keeps your software updated. If you choose not to use a framework, then it becomes the developer’s responsibility to keep the software updated with the latest technology advancements.

    Now, you are acquainted with what frameworks are and why we use it. Let’s take a look at what web frameworks are available in the market exception.

    What are some famous web frameworks?

    Apart from Django, there are several other web application frameworks available in the industry which you should be aware of which are as follows:

    Flask: Flask is a web application framework written in Python. Flask is based on the WSGI toolkit and Jinja2 template engine.

    Ruby on Rails: Ruby on Rails is a productive web application framework based on Ruby. One can develop an application at least quite faster with Rails than a typical Java framework.

    Angular: Angular is a framework on JavaScript by Google which helps us in building powerful web apps. It is a framework to build large scale and high-performance web applications while keeping them as easy-to-maintain.

    ASP.NET: ASP.NET is a framework developed by Microsoft, which helps us to build robust web applications for PCs, as well as mobile devices. It is a high-performance and lightweight framework for building web applications using .NET.

    Spring: Spring is the most popular application development framework for enterprise Java. Developers around the globe use Spring to create high-performance and robust web apps.

    PLAY: Play is one of the modern web application frameworks written in Java and Scala. It follows the MVC architecture and aims to optimize the developer’s productivity by using convention over configuration, hot code reloading, and display of errors in the browser.

    Other frameworks worth mentioning are Laravel, Symfony, CodeIgniter, Express, React.js, Node.js, Hibernate, etc.

    Is learning Django worth it?

    Before you dive into Django and dedicate your time and effort to it, you should ask if it’s worth it. Is it used in industry? You will be glad to know that some of the most famous websites use Django. Examples: Disqus, Instagram, Knight Foundation, MacArthur Foundation, Mozilla, National Geographic, Open Knowledge Foundation, Pinterest, Open Stack, etc. So yes, learning Django is completely worth it. The best part is the more learn, the more it grows over you.

    What is a virtual environment?

    While developing software or web applications, you will be using a lot of pre-written code legally available from different sources. These sets of code are called libraries, packages, add-ons, plugins, etc. If you are using a framework, then that framework will also have a huge set of pre-written code which will help you implement different features in your application. To use these pre-written codes, you will need to install the libraries, plugins, etc. in your project/application.

    The catch here is that different applications are expected to have different features. So, to implement those different features, you will need different set of frameworks, libraries, add-ons, plugins, etc.

    Let’s assume you are developing two different applications on your personal computer. Project 1 is using Django 2.1, Python 3.5, bootstrap 4, etc. Whereas Project 2 is using Flask, Python 2.7, bootstrap 3, etc. Now, you have a requirement to install both (3.5 and 2.7) the versions on Python, Django, Flask, different versions of bootstrap, etc. When you run your projects, the interpreter will be confused about which version of the Python to use. For Project 1, installation of Project 2 will be of no use and will create conflicts and vice-versa for Project 2.

    To avoid such conflicts and confusions, we use virtual environments. We create a setup that takes some space on the server or your computer and use it for all the installations related to a project. When you work on that project, you just need to activate the corresponding setup and the interpreter uses the installations in that setup only. This setup is called a virtual environment.

    Different setups or virtual environments are created for different projects. Therefore, there is no conflict between the installations of one project with another. You can run different projects simultaneously using virtual environments. All you need to do is to set the path of an interpreter to the correct virtual environment. All your projects will use their different interpreters and will run without any conflicts.

    How to create and use a virtual environment?

    So now you have an overview of virtual environments. Let’s create one and see how it looks like. You will need an active internet connection.

    Install Python:

    Let’s begin by installing Python. Go to python official website - www.python.org, and then go to the Downloads tab.

    Select your operating system (In this book, we will be using Windows.):

    Figure 1.1: Operating system choices for python installation

    Select your operating system and download the Python installable:

    Figure 1.2: Select Add Python to the PATH checkbox

    Once downloaded, run the installable file. You will see a similar window. Choose your location for the installation and make sure that you checkmark the last checkbox (Add Python x.x to PATH). This will configure your operating system to use this Python installation for executing Python programs as default if any other installations are not mentioned.

    Installing a virtual environment:

    Open your Windows Power Shell or CMD.

    Run the ‘pip install virtualenv’ command without the quotes.

    This will install the virtual environment on your machine.

    If you face errors like pip not found, you need to install pip. Or you can uninstall Python and again install it by following the preceding steps.

    Creating a virtual environment:

    Create a root folder for your project. A folder that will contain everything related to the project. Let’s call it Project_root.

    Change the directory to Project_root in PowerShell or CMD or your Linux terminal and run the ‘virtualenv prenv’ command without the quotes. Here, virtualenv is the command used to create a virtual environment and prenv is the name of your virtual environment. You can choose any name for your virtual environment, although it is recommended to keep it similar to the name of the project so that one can easily relate the virtual environment to its corresponding project:

    Figure 1.3: Creating a virtual environment

    Your virtual environment is now ready. Let’s explore it and see what it has to offer. Run the ‘cd prenv’ command without the quotes:

    Figure 1.4: Virtual environment folder

    Now, run the dir command to see the contents of your virtual environment folder. You can see the folders like Include, Lib, Scripts, tcl, and the LICENSE.txt file. The Lib folder contains all your library files and installations. The Scripts folder has the scripts which perform certain specific tasks. The Include folder contains the interpreter files.

    Now, once you have your virtual environment ready, you need to activate it. It is activated and deactivated by scripts present in the Scripts folder. Run the ‘cd Scripts’ command without the quotes. Then, run ‘activate’ without the quotes:

    Figure 1.5: Activating a virtual environment

    Your virtual environment is active now. The indication of activation or deactivation of any virtual environment is the name of the virtual environment at the beginning of the line in the terminal.

    Let’s install the Python package and check whether this works. Run the pip install numpy command. Once the installation completes, go to your Lib/site-packages folder and check for numpy installations.

    This is how you can install packages and libraries in your virtual environments:

    Figure 1.6: Checking installations

    Now, any installations made using this terminal will be confined to this environment only. Any projects that run from the current terminal will use the installations in this environment.

    If you have a project anywhere on your server or computer and you want to execute it using the installations of the environment, then simply activate this environment as shown earlier and then change the directory (cd) to your project directory and run your project:

    Figure 1.7: Deactivating the virtual environment folder

    To deactivate this environment, run the deactivate command.

    Now, you have a virtual environment installed in your project and you are good to go. Let’s install Django and move one step closer to create your web application.

    How to install Django?

    In the previous section, you learned about virtual environments and how to use them. Now you have a fair understanding of how to use the terminal to install packages in virtual environments. Now, in this chapter, you will use the virtual environment created earlier and install Django on it.

    We used pip twice in the previous sections. You must be wondering what is a pip? Well, pip is like an official Python package installer. If there is any validated Python library or package present at https://pypi.org/, then pip can be used to install it. PIP gets installed by default with Python

    Enjoying the preview?
    Page 1 of 1