<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nick</title>
    <description>The latest articles on DEV Community by Nick (@odhiambo).</description>
    <link>https://dev.to/odhiambo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1206812%2F21c29285-8ffb-4110-aa05-676c49b2b5c4.jpeg</url>
      <title>DEV Community: Nick</title>
      <link>https://dev.to/odhiambo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/odhiambo"/>
    <language>en</language>
    <item>
      <title>Integrate Google OAuth2 Social Authentication into your Django Web App</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Wed, 18 Sep 2024 11:25:40 +0000</pubDate>
      <link>https://dev.to/odhiambo/integrate-google-oauth2-social-authentication-into-your-django-web-app-1bk5</link>
      <guid>https://dev.to/odhiambo/integrate-google-oauth2-social-authentication-into-your-django-web-app-1bk5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Many modern web apps are using social authentication methods as an authentication option. Social authentication involves using third party apps, usually social media apps or Google, as a sign up or login option. The use of third-party apps such as Google, Facebook, Twitter/X, or Github to authenticate users improves user experience. Users do not have to go through the long and tedious process of manually creating an account. As a result, authentication is fast and effortless.&lt;/p&gt;

&lt;p&gt;Moreover, on the business side, authenticating with trusted third parties significantly buffers a web app’s security. Facebook, X, and Google have rigorous authentication processes themselves. If a prospective user account is deemed as "not malicious" by Google, then the account most likely poses a lower security risk for your app.&lt;/p&gt;

&lt;p&gt;This article walks Django developers through the process of adding Google social sign in as a login option to their authentication flow. Let us dig in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A beginner-level understanding of Django web framework.&lt;/li&gt;
&lt;li&gt;A &lt;a href="https://code.visualstudio.com/download" rel="noopener noreferrer"&gt;VS Code editor&lt;/a&gt; installed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Building your Project
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Getting Started
&lt;/h3&gt;

&lt;p&gt;We are going to create a sample Django project with which to integrate Google social authentication. Proceed to open your VS Code editor then create a directory called &lt;code&gt;Google-OAuth2&lt;/code&gt;, which will host your entire application code.&lt;/p&gt;

&lt;p&gt;If you already have a project and only need to start integrating Google social authentication, jump to here.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up your Development Environment.
&lt;/h3&gt;

&lt;p&gt;You need to create a Python virtual environment to isolate and install software dependencies you will need for this project. We obviously need to install the Django framework itself. Then we will install a package known as &lt;code&gt;django-allauth&lt;/code&gt;. &lt;a href="https://github.com/pennersr/django-allauth" rel="noopener noreferrer"&gt;django-allauth&lt;/a&gt; is a Django project, available on Github. The project contains all the necessary routes (URLs), logic (views) and rendering mechanisms (HTML templates) that facilitate social login with major third parties such as Google, Twitter and Facebook. You can check out the project for a deeper understanding.&lt;/p&gt;

&lt;p&gt;Open a new VS code terminal and ensure you are inside your project directory. Create a Python virtual environment using the &lt;code&gt;venv&lt;/code&gt; python module like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
 &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;venv&lt;/span&gt; &lt;span class="n"&gt;venv&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;info&lt;/strong&gt;: The first &lt;code&gt;venv&lt;/code&gt; command line argument is the Python virtual environment module while the second &lt;code&gt;venv&lt;/code&gt; argument is a custom name you give as an identifier of your virtual environment. You could name your virtual environment anything you want—env, myenv, env1, my_env, etc. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After the Python interpreter has successfully created a virtual environment for you, you should see a new directory named as the custom name of your virtual environment inside your &lt;code&gt;Google-OAuth2&lt;/code&gt; folder. Now activate the virtual environment by running the command &lt;code&gt;venv\Scripts\activate&lt;/code&gt;.  You should see your virtual environment name, enclosed by parentheses, preceding your terminal’s command prompt: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 users\user\Desktop\Google-OAuth2:

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Perfect. Now install django and django-allauth inside the environment using pip.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

pip install django django-allauth


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; You can install several packages using one pip command like above. However, it is best practice to install a package at a time. If one fails to install, it is easier to pin-point which package failed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Now we Build
&lt;/h3&gt;

&lt;p&gt;Inside your root directory, create a Django project like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 startproject core .

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Django will create a new directory called &lt;code&gt;core&lt;/code&gt; in the current working directory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;warning&lt;/strong&gt;: Remember to include the period at the end. Otherwise, Django will create a project directory called &lt;code&gt;core&lt;/code&gt; within another directory with the same name.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, create an application called &lt;code&gt;authentication&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
 &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;startapp&lt;/span&gt; &lt;span class="n"&gt;authentication&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above command invocation will create an app directory called &lt;code&gt;authentication&lt;/code&gt; at the same level as your project directory &lt;code&gt;core&lt;/code&gt;.  Proceed to include your new application in the list of &lt;code&gt;INSTALLED_APPS&lt;/code&gt; section of the newly created &lt;code&gt;settings.py&lt;/code&gt; file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# core/settings.py
&lt;/span&gt;&lt;span class="n"&gt;INSTALLED_APPS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="bp"&gt;...&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;authentication&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="bp"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Create a User Data Model
&lt;/h3&gt;

&lt;p&gt;Next, you need to create a user data model that defines how your application will store user information.&lt;/p&gt;

&lt;p&gt;Think about the utility of the application you are creating for a minute. Why are you integrating Google social authentication in the first place? You want to extend the built-in user model by allowing a user to log in using their email instead of a username. Remember that social authentication is usually built on top of the traditional Django authentication system. For the traditional authentication, a user registers with a username, email (optional), and password as primary credentials. Therefore, your user data model should have all these fields.&lt;/p&gt;

&lt;p&gt;Django ships with a built-in user model, a Python class object, called &lt;code&gt;User&lt;/code&gt;. The model has the three primary fields above in addition to other fields. You can extend the &lt;code&gt;User&lt;/code&gt; model by creating your own custom fields and methods to suit your unique project needs. &lt;/p&gt;

&lt;p&gt;For instance, the default username field in the built-in model requires a username. You may override this condition if you want your users to use email addresses as their usernames instead, as we will do in this project.&lt;/p&gt;

&lt;p&gt;To include extra custom fields, your &lt;code&gt;User&lt;/code&gt; class object should &lt;a href="https://docs.python.org/3/tutorial/classes.html" rel="noopener noreferrer"&gt;inherit&lt;/a&gt; from another Django class object called &lt;a href="https://docs.djangoproject.com/en/5.1/topics/auth/customizing/" rel="noopener noreferrer"&gt;AbstractUser&lt;/a&gt;. This superclass allows you to include custom fields and customize the default fields.&lt;/p&gt;

&lt;p&gt;With this information in mind, edit your &lt;code&gt;authentication/models.py&lt;/code&gt; file like this:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# authentication/models.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AbstractUser&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CustomUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AbstractUser&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;EmailField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;USERNAME_FIELD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;email&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
    &lt;span class="n"&gt;REQUIRED_FIELDS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;username&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Our custom user model extends the built-in &lt;code&gt;AbstractUser&lt;/code&gt; model. Even though username, email and password fields exist in the built-in model, you explicitly redefine them here to customize the allowable lengths for the username, email, and password strings you want to accept from the user during registration.&lt;/p&gt;

&lt;p&gt;Furthermore, you override the default username field to use an email instead of a username.&lt;/p&gt;

&lt;p&gt;Next, to use your custom user model, you need to register your model in &lt;code&gt;settings.py&lt;/code&gt; file. Head over to &lt;code&gt;core/setttings.py&lt;/code&gt; and add this line:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# Custom user model
&lt;/span&gt;&lt;span class="n"&gt;AUTH_USER_MODEL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;authentication.CustomUser&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: This directive overrides the default &lt;code&gt;User&lt;/code&gt; by stating the path to the custom model, which is in the authentication app.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Next, you need to initialize and apply a database schema to an SQLite database that ships with Django. Run the following two commands in your VS Code terminal:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="n"&gt;Python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;makemigrations&lt;/span&gt;
&lt;span class="n"&gt;Python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;migrate&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Proceed to register your custom user model with the admin so you can access and edit your database entries as admin:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# authentication/admin.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib.auth&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;get_user_model&lt;/span&gt;

&lt;span class="n"&gt;CustomUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_user_model&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CustomUser&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Finally, using a VS Code terminal, create a superuser account that can access the admin dashboard:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

python manage.py createsuperuser


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run a development server:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

python manage.py runserver


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A local development server will start on port 8000. Visit &lt;code&gt;http://127.0.0.1:8000/admin&lt;/code&gt; to log into the admin panel. You should see your model under &lt;strong&gt;Models&lt;/strong&gt;. You can create a few user entries for testing purposes.&lt;/p&gt;

&lt;p&gt;&lt;a id="google"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Integrate Google OAuth2
&lt;/h2&gt;

&lt;p&gt;Time to bring in the big guns. As you have already installed the &lt;code&gt;django-allauth&lt;/code&gt; dependency, let’s proceed to configure our backend to support authentication with Google.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;django-allauth&lt;/code&gt; is a django project similar to the one you're building here, with several apps. You therefore need to register the required &lt;code&gt;django-alluth&lt;/code&gt; apps in your &lt;code&gt;INSTALLED_APPS&lt;/code&gt; section.&lt;/p&gt;

&lt;p&gt;Add the following django-alluth apps to the section. You could label the apps as "allauth apps" to distinguish them from other installed apps:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="n"&gt;INSTALLED_APPS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="err"&gt;…&lt;/span&gt;
    &lt;span class="c1"&gt;#allauth apps
&lt;/span&gt;    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;allauth&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;allauth.account&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;allauth.socialaccount&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;alluth.socialaccount.providers.google&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="err"&gt;…&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, you need to update the middleware settings under the &lt;code&gt;MIDDLEWARE&lt;/code&gt; section. Insert this middleware at the bottom of the middleware list:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="n"&gt;MIDDLEWARE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="bp"&gt;...&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;allauth.account.middleware.AccountMiddleware&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, you need to configure &lt;code&gt;AUTHENTICATION_BACKENDS&lt;/code&gt;. When you issue this directive in the settings.py, you are providing a way for your project to handle the authentication credentials passed into sign up and login endpoints.&lt;/p&gt;

&lt;p&gt;Now, by default, Django uses &lt;code&gt;ModelBackend&lt;/code&gt; to parse authentication credentials, i.e., it verifies your sign up or login schema against the built-in or custom user model.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; When you initialize a Django project, the &lt;code&gt;AUTHENTICATION_BACKENDS&lt;/code&gt; directive is not explicitly stated. (You will not see this directive when you open your settings.py file). Nonetheless, it's default value is &lt;code&gt;ModelBackend&lt;/code&gt;. If you do not need to use any other authentication backend, it is needless to explicitly state this directive. Django will use the &lt;code&gt;ModelBackend&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;django-allauth&lt;/code&gt; relies on its own authentication backend that extends the default. Therefore, you need to provide an additional authentication backend to cater to &lt;code&gt;allauth&lt;/code&gt; authentication. Hence, you must explicitly state the &lt;code&gt;AUTHENTICATION_BACKENDS&lt;/code&gt; directive. Add the following setting:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="n"&gt;AUTHENTICATION_BACKENDS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="c1"&gt;# Needed to login by username in Django admin, regardless of `allauth`
&lt;/span&gt;    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.contrib.auth.backends.ModelBackend&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

    &lt;span class="c1"&gt;# `allauth` specific authentication methods, such as login by email
&lt;/span&gt;    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;allauth.account.auth_backends.AuthenticationBackend&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, add social provider settings:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="n"&gt;SOCIALACCOUNT_PROVIDERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;google&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;SCOPE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;profile&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;email&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;AUTH_PARAMS&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;access_type&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;online&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;OAUTH_PKCE_ENABLED&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Notice that the above are Google provider specific settings. &lt;code&gt;allauth&lt;/code&gt; provides similar settings for tons of other third-party authentication providers. See a full list &lt;a href="https://docs.allauth.org/en/latest/socialaccount/providers/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And finally for settings.py, add these extra &lt;code&gt;allauth&lt;/code&gt; email account configurations:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# Django allauth config
&lt;/span&gt;&lt;span class="n"&gt;SITE_ID&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;ACCOUNT_EMAIL_VERIFICATION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;mandatory&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;ACCOUNT_EMAIL_REQUIRED&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;ACCOUNT_AUTHENTICATION_METHOD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;email&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Include allauth URLs
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;django-allauth&lt;/code&gt; ships with its URLs. You can use or override the URLs in your project. For this project, you will be using some of these default URLs. More specifically, you will utilize the URL that allows for login with Google, You can visit the &lt;a href="https://github.com/pennersr/django-allauth/blob/main/allauth/account/urls.py" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; for more information.&lt;/p&gt;

&lt;p&gt;In your project level &lt;code&gt;urls.py&lt;/code&gt; file add this code:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# core/urls.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;

&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;admin/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;admin.site.urls&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;accounts/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;allauth.urls&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;You are all set to use django allauth's URLs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up an App in Google Developer Console
&lt;/h3&gt;

&lt;p&gt;You need to set up an app in &lt;a href="https://dev.to[Google%20developer%20console]"&gt;Google Console&lt;/a&gt; to enable users of your application authenticate with Google. The process involves two steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Configuring an &lt;strong&gt;OAuth consent screen&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt; Setting up &lt;strong&gt;Credentials&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  OAuth consent screen
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Visit &lt;a href="https://console.cloud.google.com" rel="noopener noreferrer"&gt;Google developer console&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Create a new project with a suitable name from the dropdown menu.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feswtf88mwdefujdpc2kr.png" alt="Google developer console home window"&gt;
&lt;/li&gt;
&lt;li&gt;Select to use the project you have just created from the dropdown menu.&lt;/li&gt;
&lt;li&gt;In the displayed window, select &lt;strong&gt;OAuth consent screen&lt;/strong&gt; and select the &lt;strong&gt;External&lt;/strong&gt; option, then hit &lt;strong&gt;Create&lt;/strong&gt;.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjyai58pghqdckx7ypohu.png" alt="Google developer console OAuth2 consent screen"&gt;
&lt;/li&gt;
&lt;li&gt;Fill in only the &lt;strong&gt;App information&lt;/strong&gt; and &lt;strong&gt;Developer information&lt;/strong&gt; details under &lt;strong&gt;Oauth2 consent screen&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Save and continue&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Save and continue&lt;/strong&gt; button for both &lt;strong&gt;Scopes&lt;/strong&gt; and &lt;strong&gt;Test users&lt;/strong&gt; forms without filling in anything.&lt;/li&gt;
&lt;li&gt;Confirm your Oauth2 details in the &lt;strong&gt;Summary&lt;/strong&gt; section then click on &lt;strong&gt;Back to dashboard&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Set up Credentials
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Locate &lt;strong&gt;Credentials&lt;/strong&gt; under &lt;strong&gt;APIs and Services&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create Credentials&lt;/strong&gt; and select &lt;strong&gt;OAuth Client ID&lt;/strong&gt; from the dropdown options.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsaklgk6f6j73oiybahjt.png" alt="The credentials screen in Google developer console"&gt;
&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;Web application&lt;/strong&gt; for the Application type field. You could use the default name under the &lt;strong&gt;Name&lt;/strong&gt; field or provide a suitable name.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Select &lt;strong&gt;ADD URI&lt;/strong&gt; under &lt;strong&gt;Authorized JavaScript Origins&lt;/strong&gt; to add a &lt;a href="https://www.techtarget.com/whatis/definition/URI-Uniform-Resource-Identifier" rel="noopener noreferrer"&gt;Uniform Resource Identifier&lt;/a&gt; (URI).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Info:&lt;/strong&gt; Adding a URI ensures only permitted domains can send requests to your OAuth2 app from a browser. Since you will be testing your Django app in development, add this local host URI:&lt;br&gt;
&lt;code&gt;http://127.0.0.1:8000&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Select &lt;strong&gt;ADD URI&lt;/strong&gt; under &lt;strong&gt;Authorized Redirect URIs&lt;/strong&gt; and add the following URI:&lt;br&gt;
&lt;code&gt;http://127.0.0.1:8000/google/login/callback/&lt;/code&gt;&lt;br&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Info:&lt;/strong&gt;This is the callback URL where Google will redirect to after a user grants or denies your application permission to authenticate with their Google account.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hit the &lt;strong&gt;Create&lt;/strong&gt; button and wait for a few seconds for your app to be ready. Your new app will generate a &lt;strong&gt;Client ID&lt;/strong&gt; and &lt;strong&gt;Client secret&lt;/strong&gt;. In the resulting window, download the JSON for these details or copy and paste the Client ID and Client secret somewhere. You will need these details in a few.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Set up a Social Account Provider
&lt;/h3&gt;

&lt;p&gt;Next, you need to set up a Social Account with the client ID and secret you generated earlier in your admin console. Login in to the admin via &lt;code&gt;http://127.0.0.1/8000/admin&lt;/code&gt; and locate the &lt;code&gt;SOCIAL ACCOUNTS&lt;/code&gt; section. Under this section, click on &lt;code&gt;SOCIAL APPLICATIONS&lt;/code&gt; then select &lt;code&gt;ADD SOCIAL APPLICATION&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Under Provider, if you click on the dropdown icon, you will notice that &lt;code&gt;Google&lt;/code&gt; is already available as a provider. This is because we already configured Google as a provider in our &lt;code&gt;settings.py file&lt;/code&gt;, when we added this line:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;

&lt;span class="c1"&gt;# core/settings.py
&lt;/span&gt;&lt;span class="n"&gt;INSTALLED_APPS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="bp"&gt;...&lt;/span&gt;
&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;alluth.socialaccount.providers.google&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Just to test it out, you could try commenting out this line and refreshing your current admin view. Google will disappear under providers.&lt;/p&gt;

&lt;p&gt;Back to adding a social application, uncomment the above code if you commented it out. Select Google under provider and provide a suitable name for the &lt;strong&gt;Name&lt;/strong&gt; field. For this purpose, call your social application &lt;code&gt;Google&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, under &lt;strong&gt;Client id&lt;/strong&gt;, paste the Client ID from Google console. Similarly, paste your Google console's secret key under &lt;strong&gt;Secret key&lt;/strong&gt;. Finally, click on &lt;strong&gt;Save&lt;/strong&gt; to save your social application credentials. &lt;/p&gt;

&lt;p&gt;Everything is now set.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing and Troubleshooting
&lt;/h3&gt;

&lt;p&gt;Now visit &lt;code&gt;http://127.0.0.1:8000/accounts/google/login&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Try logging in via the Google third party.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F37jbppy8fyuc3cxupc24.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F37jbppy8fyuc3cxupc24.png" alt="Django allauth's Google login window"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;django-allauth&lt;/code&gt; will hand over the authentication process to Google. Google will use the app you configured in the console to carry out the authentication for the Google account you select. Google will then return a response to your &lt;code&gt;allauth&lt;/code&gt; app via the callback URL you provided in Google console's &lt;strong&gt;Authorized Redirect URIs&lt;/strong&gt; section. Allauth will then handle the rest of the authentication flow from here.&lt;/p&gt;

&lt;p&gt;If you had issued a &lt;code&gt;LOGIN_REDIRECT_URL&lt;/code&gt; directive in your &lt;code&gt;settings.py&lt;/code&gt; file, &lt;code&gt;allauth&lt;/code&gt; will redirect the user to the URL provided for login redirect. For instance, you could create a home page and pass its URL as the &lt;code&gt;LOGIN_REDIRECT_URL&lt;/code&gt;. Your app will redirect all successfully logged in users to your home page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This tutorial walks the reader through the process of integrating social authentication into a Django web application using Google. The process involves defining a suitable user data model, configuring &lt;code&gt;allauth&lt;/code&gt; in your Django app, and setting up an app in &lt;a href="https://console.cloud.google.com" rel="noopener noreferrer"&gt;Google console&lt;/a&gt;, pointing it to your Django app. Finally you need to test the authentication flow. You can extend this application by integrating other third party authentication flows such Facebook, X, or Github using the &lt;a href="https://docs.allauth.org/en/latest/" rel="noopener noreferrer"&gt;django-allauth&lt;/a&gt; package. Hope this helps. Happy coding!&lt;/p&gt;

</description>
      <category>django</category>
      <category>webdev</category>
      <category>oauth</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Deploy your FastAPI Backend with PostgreSQL Database to Render</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Wed, 19 Jun 2024 13:54:16 +0000</pubDate>
      <link>https://dev.to/odhiambo/how-to-deploy-your-fastapi-backend-with-postgresql-database-to-render-4ca2</link>
      <guid>https://dev.to/odhiambo/how-to-deploy-your-fastapi-backend-with-postgresql-database-to-render-4ca2</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://fastapi.tiangolo.com/"&gt;FastAPI&lt;/a&gt; is a popular Python backend web development framework. Many Python developers use FastAPI to built Application Programming Interfaces (APIs) and connect other backend infrastructure such as databases. FastAPI is suitable for API design for several reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;APIs built using FastAPI are fast in terms of performance.&lt;/li&gt;
&lt;li&gt;FastAPI framework is simple to learn.&lt;/li&gt;
&lt;li&gt;FastAPI comes with a built-in API documentation feature that saves the developer the time needed to manually document APIs.&lt;/li&gt;
&lt;li&gt;FastAPI is generally optimized for API design.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This tutorial walks Python developers through the process of hosting APIs built using FastAPI to the &lt;a href="https://dashboard.render.com/"&gt;Render cloud hosting service&lt;/a&gt;, and connecting a PostgreSQL database. The article assumes you have your FastAPI backend ready for deployment. However, I will walk you through the deployment checks involved to get your code ready, so you can apply the checks you might have missed. Let's go!&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;A FastAPI backend&lt;/li&gt;
&lt;li&gt;A registered &lt;a href="https://dashboard.render.com/register"&gt;Render account&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Pre-Deployment
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Deployment Checks
&lt;/h3&gt;

&lt;p&gt;Assuming you have written the logic for all your APIs and have tested that everything is working as expected, there are five steps extra steps we are going to take to ascertain that our backend is ready for production:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Create a requirements.txt file
&lt;/h4&gt;

&lt;p&gt;You obviously installed software packages to build your APIs inside your Python virtual environment. Among the packages is the FastAPI framework itself and all its dependencies. We will need to install the same packages to run our code in production. We need to pass to the Render build process a list of dependencies for our project.&lt;/p&gt;

&lt;p&gt;In your local environment, create a file named &lt;strong&gt;requirements.txt&lt;/strong&gt;. You could name it anything, but naming it as above is a standard convention. Next, run &lt;code&gt;pip freeze &amp;gt; requirements.txt&lt;/code&gt; in your terminal. The command gathers the names of all the software dependencies and their version numbers from your virtual environment, and outputs the information in the requirements.txt file&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Create a .env file
&lt;/h4&gt;

&lt;p&gt;Next you need to hide any sensitive information in your codebase before you push your code to Github. In most cases, the sensitive information in most Python projects is your project's secret keys, database URL connection string, and database name, username and password. You could include database port and host if you deem them sensitive enough to be hidden.&lt;/p&gt;

&lt;p&gt;Proceed to create a hidden file called &lt;code&gt;.env&lt;/code&gt;. Do not forget the period at the beginning. Edit the file by providing key-value pairs of the secret information like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DATABASE_PORT=5432
DATABASE_NAME=fastapi
DATABASE_USERNAME=postgres
DATABASE_PASSWORD=testpass123
SECRET_KEY=123456HDUDCDHCHDUCDNCNDNCR
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember to replace the environment variables values with your actual values.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Create a .gitignore file
&lt;/h4&gt;

&lt;p&gt;This steps precedes pushing your code to Github and it involves instructing git to ignore directories and files you do not want checked into Github. By default, when you initialize a git repository as we will do in a moment, git tracks all files in the entire directory. We do not want version control to track the .env file. In addition, we do not want to check our virtual environment folder or the &lt;strong&gt;pycache&lt;/strong&gt; directory into Github. This is because we can easily recreate our virtual environment. Furthermore, we do not want to pollute our Github repository with code we do not need, such as &lt;strong&gt;pycache&lt;/strong&gt; files and virtual environment packages.&lt;/p&gt;

&lt;p&gt;Untrack unnecessary files by editing the .gitignore file like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;__pycache__&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
&lt;span class="n"&gt;venv&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="c1"&gt;# Use your virtual environment's name
&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  4. Create a build.sh file
&lt;/h4&gt;

&lt;p&gt;This step involves creating a bash file called &lt;code&gt;build.sh&lt;/code&gt; that will store our pre-deploy commands. We will run this file shortly when we host our code on Render.&lt;/p&gt;

&lt;p&gt;Let's talk about pre-deploy commands. These are commands that should run before you start your production server. Most often, these are commands that set up your production database. They may involve commands that set up your database schema and tables and run database migrations. Additionally, you can run pre-deploy commands to upload your static files to a Content Delivery Network (CDN).&lt;/p&gt;

&lt;p&gt;In most cases, with FastAPI and other Python frameworks such as Django, pre-deploy commands involve running database migrations to create schema and tables. Django, in particular, auto-generates migration files every time you make changes to your database. Therefore, with Django, you almost always have to write only these two database migration commands in your build.sh file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;makemigrations&lt;/span&gt;
&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;migrate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, FastAPI does not create any migration files when you make changes to your database. For a serious project, you usually have&lt;br&gt;
to integrate a database migration tool like &lt;a href="https://testdriven.io/blog/fastapi-sqlmodel/"&gt;Alembic&lt;/a&gt;, which tracks changes to your database schema. If you integrated Alembic into your FastAPI backend, first write this database migration command in build.sh to set up your schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;alembic upgrade head
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However if you did not integrate any database migration tool, you typically wouldn't have to run pre-deploy commands on Render. &lt;br&gt;
Assuming you do not need to make database migrations for your FastAPI, our build.sh file will contain only one command; the command to start our production server. Input the following command in your build.sh file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;uvicorn main:app &lt;span class="nt"&gt;--host&lt;/span&gt; 0.0.0.0 &lt;span class="nt"&gt;--port&lt;/span&gt; &lt;span class="nv"&gt;$PORT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;FastAPI ships with the &lt;a href="https://fastapi.tiangolo.com/deployment/manually/"&gt;uvicorn&lt;/a&gt; server by default. The above command presumes that the entry point to your backend, the main.py file, is located at the root directory of your project. If the file were located in some sub-directory, you would have to provide the relative path to the file, i.e relative path to the root directory&lt;/p&gt;

&lt;p&gt;Also, take note of the host IP. The IP is set to the 0.0.0.0 address, which is a special network interface that accepts connections from all IPs. The port option specifies an environment variable &lt;strong&gt;PORT&lt;/strong&gt;. Using an environment variable instead of hard-coding a specific port number is considered best practice. The hosting service build process will assign us a random port during the build. You could hard-code a port, say 5000 or 10000 and hope the port is not occupied by another process during the build. &lt;/p&gt;

&lt;h4&gt;
  
  
  5. Initialize a Github repository and push your code to Github
&lt;/h4&gt;

&lt;p&gt;Go to your Github account, create a new public repository and copy the repository's URL. Back in your command line, enter these prompts, in order,&lt;br&gt;
at the root of your project folder&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;init&lt;/span&gt; &lt;span class="c1"&gt;# Initializes a local git repository
&lt;/span&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nb"&gt;all&lt;/span&gt;
&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;commit&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;FastAPI backend&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;# Commit message could be anything you want
&lt;/span&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;branch&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="c1"&gt;# Sets up a branch called 'main' on Github
&lt;/span&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;remote&lt;/span&gt; &lt;span class="n"&gt;add&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Your&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt; &lt;span class="n"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# Sets up a remote repository on Github
&lt;/span&gt;&lt;span class="n"&gt;git&lt;/span&gt; &lt;span class="n"&gt;push&lt;/span&gt; &lt;span class="n"&gt;origin&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt; &lt;span class="c1"&gt;# Pushes all your code to the main branch of your Github repository.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All your code is now hosted on Github&lt;/p&gt;

&lt;h2&gt;
  
  
  Deployment
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Set up a PostgreSQL Database on Render
&lt;/h3&gt;

&lt;p&gt;Follow these steps to spin up a free PostgreSQL database on Render:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Login into your Render account. You will be redirected to your dashboard.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;new&lt;/strong&gt; and select &lt;strong&gt;PostgreSQL&lt;/strong&gt; from the dropdown menu.&lt;/li&gt;
&lt;li&gt;Fill in the Database, Name and User fields with the database credentials you want to use.&lt;/li&gt;
&lt;li&gt;Scroll down to &lt;strong&gt;Instance Type&lt;/strong&gt; and select free instance. You can select a paid instance for an enterprise-level project.&lt;/li&gt;
&lt;li&gt;Hit &lt;strong&gt;Create Database&lt;/strong&gt; at the bottom of the page and wait for Render to create a new PostgreSQL database instance for you. Render will redirect you to a page with information about the newly created database.&lt;/li&gt;
&lt;li&gt;Copy the generated &lt;strong&gt;Database Password&lt;/strong&gt; and &lt;strong&gt;Database Internal URL&lt;/strong&gt; somewhere.
Note that we are using &lt;a href="https://docs.render.com/databases"&gt;Internal URL&lt;/a&gt; and not external URL because both our  service and database are hosted within the same server. Connecting using the internal URL speeds up the connection process and reduces latency.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Create a Web Service
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Click on new again, and select &lt;strong&gt;Web Service&lt;/strong&gt; from the dropdown.&lt;/li&gt;
&lt;li&gt;Choose &lt;strong&gt;Build and deploy from a Git repository&lt;/strong&gt; and press &lt;strong&gt;next&lt;/strong&gt;. Render will connect to your Github and avail all your public repositories. Connect your FastAPI repository.&lt;/li&gt;
&lt;li&gt;Fill in details about your web service in the resulting page:

&lt;ol&gt;
&lt;li&gt;Input a suitable unique name for your service under &lt;strong&gt;Name&lt;/strong&gt; field&lt;/li&gt;
&lt;li&gt;Scroll down to &lt;strong&gt;Runtime&lt;/strong&gt; field and select &lt;strong&gt;Python3&lt;/strong&gt; from the dropdown&lt;/li&gt;
&lt;li&gt;Input &lt;code&gt;pip install -r requirements.txt&lt;/code&gt; command under  &lt;strong&gt;Build Command&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Run your build.sh file under &lt;strong&gt;Start Command&lt;/strong&gt; field like this:&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight shell"&gt;&lt;code&gt;./build.sh
&lt;/code&gt;&lt;/pre&gt;



&lt;p&gt;You could just input the command required to start our production server directly into the Start Command field since we do not have any other pre-deploy command. However, in a future event where you would need to run database migration commands, it is best practice to create a build.sh file to run all your commands at once.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select the instance type of your choice. If it is a practice project, select the &lt;strong&gt;free instance&lt;/strong&gt; type.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Add environment variables&lt;br&gt;
You can proceed in either of the following ways in Render:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can add your environment variables one-by-one from your &lt;code&gt;.env&lt;/code&gt; file by clicking on the &lt;strong&gt;Add Environment Variable&lt;/strong&gt; button.
&lt;/li&gt;
&lt;li&gt;You can also click on &lt;strong&gt;Add from .env&lt;/strong&gt; which will provide an input field. Proceed to copy the contents of your .env file from your local machine and paste them in the input field provided, then click on &lt;strong&gt;Add variables&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Remember that we added the details of our new PostgreSQL instance, which are different from the database you were using locally. Return to the dashboard and you should see the PostgreSQL instance you just created, marked as &lt;strong&gt;available&lt;/strong&gt;. Click on it and note the database name, database user, database password and database &lt;strong&gt;internal&lt;/strong&gt; URL details. Then proceed in either of the two ways discussed above to set your environment variables on Render.&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Create Web Service&lt;/strong&gt; to initialize the build process. The process will take a few minutes after which your new web service URL will be generated. You can visit your service on the browser by clicking on its URL. If the build process fails for some reason, Render will inform you. You should review the build logs to fix any errors.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Hosting your FastAPI backend with a PostgreSQL database on Render is a fairly simple process. First you will need to prepare your project for production by creating a requirements.txt file, a .env file, a .gitignore file and a build.sh file. Then you will need to push all your code to Github. Finally, you need to proceed to your Render account and create a PostgreSQL database instance, then create your web service by passing in a build command, a start command and your environment variables. Hope this helps. Happy hosting!&lt;/p&gt;

</description>
      <category>fastapi</category>
      <category>python</category>
      <category>render</category>
      <category>hosting</category>
    </item>
    <item>
      <title>Do not Reinvent the Wheel: Utilize Django’s Built-in Auth App to Create a Robust Authentication System</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Thu, 16 May 2024 22:45:56 +0000</pubDate>
      <link>https://dev.to/odhiambo/do-not-reinvent-the-wheel-utilize-djangos-built-in-auth-app-to-create-a-robust-authentication-system-4840</link>
      <guid>https://dev.to/odhiambo/do-not-reinvent-the-wheel-utilize-djangos-built-in-auth-app-to-create-a-robust-authentication-system-4840</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Many web applications implement a user authentication system for purposes of data security. A robust user authentication system protects user data against unauthorized access or loss.  Many server-side programming frameworks like Django provide a ready-made user authentication system. Optionally, a developer can configure such a system from scratch based on project needs. The later is a risky endeavor, as such a system is less secure than a built-in one. Moreover, opting to create from scratch may be an overkill in many scenarios. As best practice, one should use the built-in authentication system which has already been tested and optimized. This article discusses Django’s built-in user authentication system and how you, as a Django developer, can configure it in your project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An understanding of how to build with Django.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Django's Auth App
&lt;/h2&gt;

&lt;p&gt;If you peruse &lt;a href="https://docs.djangoproject.com/en/3.1/topics/auth/default/#module-django.contrib.auth.views"&gt;Django’s official docs&lt;/a&gt; or the source code available on &lt;a href="https://github.com/django/django"&gt;Github&lt;/a&gt;, you should locate the &lt;strong&gt;auth&lt;/strong&gt; app documentation. The auth app ships with Django, and is installed in your project when you run the &lt;code&gt;django-admin start project&lt;/code&gt; command.  Let us observe this in real-time by creating a Django project.&lt;/p&gt;

&lt;p&gt;First create your project directory and install Django using your preferred Python virtual environment package. Do not forget to activate the virtual environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;code &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;code
pipenv &lt;span class="nb"&gt;install &lt;/span&gt;django
pip shell
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next start a Django project to generate manage.py file and other config files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;django-admin startproject user-auth &lt;span class="nb"&gt;.&lt;/span&gt;
python manage.py migrate
Python manage.py runserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can visit &lt;a href="http://127.0.0.1:8000"&gt;http://127.0.0.1:8000&lt;/a&gt; to confirm that you have successfully created a Django project.&lt;/p&gt;

&lt;p&gt;Now open our settings.py file to confirm that an auth app exists. In your settings.py under &lt;code&gt;INSTALLED-APPS&lt;/code&gt;, you should see this line&lt;br&gt;
&lt;code&gt;'django.contrib.auth'&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# user-auth/settings.py
&lt;/span&gt;&lt;span class="n"&gt;INSTALLED_APPS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.contrib.admin&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.contrib.auth&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# built-in authentication
&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.contrib.contenttypes&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.contrib.sessions&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.contrib.messages&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.contrib.staticfiles&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The built-in authentication system comes with its own URL routes and views. Before you configure the system in your project though, you have to include its URL routes in your project-level urls.py file. This is necessary to enable the use of the provided routes locally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# user-auth/urls.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;

&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="c1"&gt;# Django admin
&lt;/span&gt;    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;admin/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="c1"&gt;# User management
&lt;/span&gt;    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;accounts/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.contrib.auth.urls&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="c1"&gt;# This
&lt;/span&gt;
                                    &lt;span class="bp"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are prefixing all our user authentication routes with &lt;strong&gt;accounts/&lt;/strong&gt; as a convection. You could use any prefix though.&lt;/p&gt;

&lt;p&gt;Let's inspect the URL routes that ship with the auth app. You can inspect the&lt;a href="https://docs.djangoproject.com/en/3.1/topics/auth/default/#module-django.contrib.auth.views"&gt; docs&lt;/a&gt; or &lt;a href="https://github.com/django/django"&gt;Django source code&lt;/a&gt; for the routes. Auth app provides the following routes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;login&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;login&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;logout&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;logout&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;password_change&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;password_change&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;password_change&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;password_change_done&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;password_reset&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;password_reset&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;password_reset&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;password_reset_done&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;reset&lt;/span&gt;&lt;span class="o"&gt;/&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;uidb64&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;/&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;/&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;password_reset_confirm&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;accounts&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;reset&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;done&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;password_reset_complete&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Implement Login and Logout
&lt;/h2&gt;

&lt;p&gt;Now it's time to configure a simple login and logout. In the routes above, we can see that similar URLs exist.&lt;/p&gt;

&lt;p&gt;An important thing to note: The built-in auth app provides the URLs and the views necessary to handle login, logout and other user authentication-related functionality. However, it is up to the Django developer to create templates that render various authentication pages.&lt;/p&gt;

&lt;p&gt;We are going to create three pages for rendering purposes: a home page, a login and a logout page&lt;/p&gt;

&lt;h3&gt;
  
  
  A Simple Homepage
&lt;/h3&gt;

&lt;p&gt;We are going to create a dedicated pages app to render our homepage&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py startapp pages
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we configure the URL route for the home page&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/urls.py
&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;

&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="c1"&gt;# Django admin
&lt;/span&gt;    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;admin/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="c1"&gt;# User management
&lt;/span&gt;    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;accounts/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.contrib.auth.urls&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="c1"&gt;# Local apps
&lt;/span&gt;    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;pages.urls&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt; &lt;span class="c1"&gt;# This
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then create a urls.py file inside the pages app and update it with the home page route&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# pages/urls.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;.views&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HomePageView&lt;/span&gt;

&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;HomePageView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_view&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;home&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we need to provide a view logic which the home page URL should resolve to, and a template to&lt;br&gt;
render the page(This article assumes you are sufficiently familiar with the flow of building a web page using Django).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# pages/views
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.views.generic&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;TemplateView&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HomePageView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TemplateView&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Renders the home page&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;template_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;home.html&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we create a home page template. We need to update our settings.py file to create a route to our templates directory. Also, we need to create a templates directory inside our project-level directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/settings.py
&lt;/span&gt;&lt;span class="err"&gt;…&lt;/span&gt;
&lt;span class="n"&gt;TEMPLATES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;BACKEND&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.template.backends.django.DjangoTemplates&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;DIRS&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BASE_DIR&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;joinpath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;templates&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))],&lt;/span&gt; &lt;span class="c1"&gt;# update template DIRS like this
&lt;/span&gt;        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;APP_DIRS&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;OPTIONS&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;context_processors&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.template.context_processors.debug&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.template.context_processors.request&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.contrib.auth.context_processors.auth&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.contrib.messages.context_processors.messages&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="err"&gt;…&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then create a templates directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;templates
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally create the home page. We want the page to have a login link if a user is logged out, and&lt;br&gt;
a logout link if a user is logged in. We will use conditional rendering.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--templates/home.html--&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;home&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Home page&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
      {% if user.is_authenticated %}
      &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&lt;/span&gt;Hello, {{ user.username }}&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;span&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"{% url 'logout' %}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Log Out&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
      {% else %}
      &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"{% url 'login' %}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Log In&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
      {% endif %}
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the login and logout links provided in the page. They use similar namespaces provided in the auth/urls.py file. Now visit &lt;a href="http://127.0.0.1:8000"&gt;http://127.0.0.1:8000&lt;/a&gt; to confirm if the home page renders correctly.&lt;/p&gt;

&lt;p&gt;However, If you click on the login link, you will encounter a TemplateDoesNotExist Error since we do not yet have a login template.&lt;/p&gt;

&lt;p&gt;If you inspect &lt;a href="https://github.com/django/django/blob/b9cf764be62e77b4777b3a75ec256f6209a57671/django/contrib/auth/views.py"&gt;auth/views.py&lt;/a&gt; file, the login template provided is located at registration/login.html. It means we need to create a login template under a "registration" directory. You could create it under any directory. However, that would mean overriding the built-in LoginView provided by the auth app, which we do not want. Therefore, a suitable option is to create a similar directory structure within our templates directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;templates/registration
&lt;span class="nb"&gt;touch &lt;/span&gt;templates/registration/login.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a simple login
&lt;/h3&gt;

&lt;p&gt;Our login page looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--templates/registration/login.html--&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Login&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;Log In&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"post"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
                {% csrf_token %}
                {{ form.as_p }}
                &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Log In&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try accessing the page from your browser at &lt;a href="http://127.0.0.1:8000/accounts/login"&gt;http://127.0.0.1:8000/accounts/login&lt;/a&gt; /. If you click on the Log In button, you will run into a 404 error. That means the page you are requesting does not exist on the server. If you inspect the URL in your address bar, you will see that the login provided by auth redirects to /accounts/profile/ page by default. We have not configured such a page, hence the 404 error.&lt;/p&gt;

&lt;p&gt;A simple solution is to change the redirect to point to a page we have already configured, in this case our home page. To implement a redirect to the home page, we need to set the LOGIN_REDIRECT_URL directive explicitly in our settings.py file to point to our home page&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/settings.py
# Add this at the end
&lt;/span&gt;&lt;span class="n"&gt;LOGIN_REDIRECT_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;home&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Implement Logout
&lt;/h3&gt;

&lt;p&gt;Auth does not provide a logout template. Only URL and view. We could create a custom logout page which the logout URL should resolve to, but that would be unnecessary. The only thing we need to do is to update the LOGOUT_REDIRECT_URL which should redirect a user to one of our existing pages when they log out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/settings.py
&lt;/span&gt;&lt;span class="n"&gt;LOGIN_REDIRECT_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;home&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;span class="n"&gt;LOGOUT_REDIRECT_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;home&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;# This
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both our login and logout are now redirecting to our home page&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure Sign Up
&lt;/h3&gt;

&lt;p&gt;Django does not provide a built-in sign up functionality. The Developer should configure it manually. Manually setting up user registration is beyond the scope of this tutorial.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Django is a mature and robust web development framework that comes with many pre-built features. This is intentional, as Django purposes to speed up development. Among the features that ship with Django is the auth app, which contains a pre-built user authentication functionality. The auth app avails login, logout and other user-authentication related functionality for easy configuration. You can, and probably should, leverage the built-in login and logout functionality to quickly set up a user authentication system, so that you can focus on other core features of your web app. Happy coding!&lt;/p&gt;

</description>
      <category>django</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>authentication</category>
    </item>
    <item>
      <title>Containerize your Django Web Application with Docker</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Thu, 16 May 2024 22:43:16 +0000</pubDate>
      <link>https://dev.to/odhiambo/containerize-your-django-web-application-with-docker-2c2e</link>
      <guid>https://dev.to/odhiambo/containerize-your-django-web-application-with-docker-2c2e</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Containerization is becoming commonplace in modern software development. The practice involves the development of software applications in isolated environments that mimic a desired operating system. Containerization has enabled developers, using different operating systems, to create applications and collaborate in a consistent environment. Containerization essentially standardizes the development environment for a team of software developers collaborating on a project. &lt;/p&gt;

&lt;p&gt;Several containerization technologies exist in the market today, with the most popular one being &lt;a href="https://www.docker.com/" rel="noopener noreferrer"&gt;Docker&lt;/a&gt;. This article walks Django developers through containerization using Docker. The article discusses how to set up a containerized development workspace, and the best practices involved. We will create a simple Django application and containerize it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;• A Working knowledge of web development using Django.&lt;br&gt;&lt;br&gt;
• A Windows 10+ computer with &lt;a href="https://learn.microsoft.com/en-us/windows/wsl/install" rel="noopener noreferrer"&gt;Windows Subsystem for Linux (WSL)&lt;/a&gt; installed.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up Docker on Windows
&lt;/h2&gt;

&lt;p&gt;You will need to install &lt;a href="https://docs.docker.com/desktop/install/windows-install/" rel="noopener noreferrer"&gt;Docker on Windows&lt;/a&gt; and create a &lt;a href="https://hub.docker.com/signup" rel="noopener noreferrer"&gt;Docker hub &lt;/a&gt; account. Follow the installation guidelines provided to set up Docker Desktop on your Windows. Ensure your system meets the specified &lt;a href="https://learn.microsoft.com/en-us/windows/wsl/install" rel="noopener noreferrer"&gt;requirements&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The Docker desktop app acts as a front-end where you can view your images and containers (More on this in soon). Your WSL acts as a backend, where you interact with the Docker Engine, which essentially builds your images and runs your containers. You interact with the Docker Engine by running Docker commands right from your WSL Command Line Interface (CLI).&lt;/p&gt;

&lt;p&gt;As a best practice, it is advisable to run Docker as a non-root user using WSL. To set up a non-root user account in your WSL distro with Docker, follow &lt;a href="https://docs.docker.com/engine/install/linux-postinstall/" rel="noopener noreferrer"&gt;this&lt;/a&gt; guideline. If you already have a non-root user and choose not to set up the user with Docker, you will have to preface all your Docker commands with the &lt;strong&gt;sudo&lt;/strong&gt; command.&lt;br&gt;
If you have followed the installation process successfully, run &lt;code&gt;docker –version&lt;/code&gt;. Your terminal should display the Docker version you just installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker &lt;span class="nt"&gt;--version&lt;/span&gt;
docker version 26.0.0, build 2ae903e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Docker ships with a pre-built "Hello Word" image. You can check if Docker is installed and running properly by confirming that this image exists. To do so, run this command on WSL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker run hello-world
Unable to find image &lt;span class="s1"&gt;'hello-world:latest'&lt;/span&gt; locally
latest: Pulling from library/hello-world
1b930d010525: Pull &lt;span class="nb"&gt;complete
&lt;/span&gt;Digest:sha256:b8ba256769a0ac28dd126d584e0a2011cd2877f3f76e093a7ae560f2a5301c00
Status: Downloaded newer image &lt;span class="k"&gt;for &lt;/span&gt;hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps:

1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the &lt;span class="s2"&gt;"hello-world"&lt;/span&gt; image from the Docker Hub. &lt;span class="o"&gt;(&lt;/span&gt;amd64&lt;span class="o"&gt;)&lt;/span&gt;
3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with: 
&lt;span class="nv"&gt;$ &lt;/span&gt;docker run &lt;span class="nt"&gt;-it&lt;/span&gt; ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit: https://docs.docker.com/get-started/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run &lt;code&gt;docker-info&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker info
Client: 
  Debug Mode: &lt;span class="nb"&gt;false
&lt;/span&gt;Server: 
  Containers: 1
  Running: 0
  Paused: 0
  Stopped: 1
  Images: 1
  ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are ready to build and containerize our Django web application.&lt;/p&gt;

&lt;h2&gt;
  
  
  But Before we Build…
&lt;/h2&gt;

&lt;p&gt;Let’s understand core Docker terminologies. When building with Docker, you should familiarize yourself with these three fundamental terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A Docker image&lt;/li&gt;
&lt;li&gt;A Docker container&lt;/li&gt;
&lt;li&gt;A Docker host&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A Docker image is, basically, a list of instruction that the Docker engine follows to build your project. A Docker image can also be construed as a snapshot in time of your software project. You rebuild your image every time you update software packages in your project, or when you change some key code, as in settings.py. You write an image inside the &lt;code&gt;Dockerfile&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;A Docker container is a running instance of a Docker Image. You build your project (an image) inside Docker, then you run it, in which case the image is now known as a container. Docker Host is the underlying Operating System that runs Docker containers. One host could run several containers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now we Build...
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Create a Basic Django application
&lt;/h3&gt;

&lt;p&gt;We are going to build a basic blog application where users can blog on various topics of interest. This article assumes you are already familiar with the flow of building any Django project, hence it will not go into details about building with Django.&lt;/p&gt;

&lt;p&gt;Let us think for a moment about the structure and functionality of our project. We want our blog web application to enable a user input a topic and a brief blog about the topic. Each topic could have one or several blog entries. Thus, we will model a topic with a blog entry for each topic.&lt;/p&gt;

&lt;p&gt;For this project, we only want to display a list of all available topics though, so we will write a view logic that queries the database to retrieve a list of all topics available. Finally, we will write a template that renders the list of topics on the user interface. We could develop a full blog website. However, for Docker demonstration purposes, we will use this minimalist application.&lt;/p&gt;

&lt;p&gt;Let’s get started…&lt;br&gt;
From your CLI:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a project directory named code, or any name you prefer, and change into this directory.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir code &amp;amp;&amp;amp; cd code&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Install the pipenv python virtual environment package if you do not have it already.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install pipenv&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Install Django using pipenv.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pipenv install django&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Activate a virtual environment using the shell command.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pipenv shell&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;You should see the name of your directory preceding your WSL distro prompt like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;code&lt;span class="o"&gt;)&lt;/span&gt; nick@DESKTOP-TOB16R4:~/ 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This indicates that your environment is active&lt;/p&gt;

&lt;h3&gt;
  
  
  Pipfile and Pipfile.lock
&lt;/h3&gt;

&lt;p&gt;Note that when you installed Django, pipenv created two files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pipfile&lt;/li&gt;
&lt;li&gt;Pipfile.lock&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Pipfile contains a list of dependencies for your project. It serves the same purpose as a requirements.txt file. A Pipfile.lock locks down all project dependencies you installed and the order in which you installed them.&lt;/p&gt;

&lt;p&gt;The Pipfile.lock file enforces a deterministic build; you will achieve the same result regardless of the number of times you, or anybody in your team, installs the locked software packages. Without locking down the dependencies and their order, team members may have a slightly differentiated build installations, which could result in conflicts. Remember we want a consistent development environment when working with Docker.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Containerized Blog App
&lt;/h3&gt;

&lt;p&gt;While your virtual environment is still active, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;django-admin startproject config &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Django will create a project level directory called &lt;strong&gt;config&lt;/strong&gt; in the current working directory. Do not forget the period after the command, or Django will create an extra directory for our project. We want the project-level directory to be in the current working directory. You could name your project anything else other than config.&lt;/p&gt;

&lt;p&gt;Next, we need to migrate our database. The action will apply the default user model and other admin settings to the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we need to confirm that our project is working as expected:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py runserver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you visit &lt;strong&gt;&lt;a href="http://127.0.0.1:8000" rel="noopener noreferrer"&gt;http://127.0.0.1:8000&lt;/a&gt;&lt;/strong&gt; on your browser, you should see the default Django successfully installed page.&lt;br&gt;
If everything is OK, return to your terminal and exit your virtual environment by typing the command &lt;code&gt;exit&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Build your Image
&lt;/h3&gt;

&lt;p&gt;Our project is working as expected in our local setting. Now we need to host it on Docker. It should show a similar page. To containerize it, we will need to build an image of the project. As mentioned earlier, this is a list of instructions that Docker will run to set up our project. Create a file called Dockerfile in your root directory:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dockerfile&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Pull base image
FROM python:3.10
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /code
# Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv &amp;amp;&amp;amp; pipenv install --system
# Copy project
COPY . /code/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Dockerfile is executed top to bottom. Thus, the first command must be the &lt;strong&gt;FROM&lt;/strong&gt; command. This command pulls over our base image from an online repository, which is python 3.10 in this case. You could specify any Python version you want based on your project needs.&lt;/p&gt;

&lt;p&gt;The ENV command set two basic environment variables: PYTHONDONTWRITEBYTECODE prevents Python from writing .pyc files, which we do not need. PYTHONUNBUFFERED formats Docker’s output to the console.&lt;/p&gt;

&lt;p&gt;Next we are setting up a working directory called &lt;strong&gt;code&lt;/strong&gt; using the WORKDIR command. This step is essential when we are executing commands within our container. If we do not specify a working directory, we would have to type in a long path. By setting a default directory as above, Docker automatically executes commands within this directory.&lt;/p&gt;

&lt;p&gt;Next, we install project dependencies using pipenv. Remember that our project contains one dependency, Django, so far. We begin by copying over the contents of our Pipfile and Pipfile.lock into our working directory (code), that we just created. Then we install the dependencies globally within Docker by appending the –system flag at the end of the install command. We use the --system flag to instruct pipenv to install the software packages globally within Docker. This is because pipenv, by default, looks for a virtual environment to install packages, and Docker now acts as our virtual environment.&lt;/p&gt;

&lt;p&gt;Next we copy over the rest of our local code into our Docker working directory.&lt;br&gt;&lt;br&gt;
Now let’s build our image with a single command.&lt;br&gt;
Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker build &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Docker will output a lot of information on the console. Check the last part of the output to confirm whether Docker successfully built your image.&lt;/p&gt;

&lt;p&gt;Next, we need to get this image running so we can interact with our web app from the browser. To accomplish that, we would need to create a &lt;br&gt;
&lt;code&gt;docker-compose.yml&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;docker-compose.yml&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;version: &lt;span class="s1"&gt;'3.8'&lt;/span&gt;
services: 
  web:
    build: &lt;span class="nb"&gt;.&lt;/span&gt;
    &lt;span class="nb"&gt;command&lt;/span&gt;: python /code/manage.py runserver 0.0.0.0:8000
    volumes: - .:/code
    ports: - 8000:8000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s explain the contents of this file:&lt;br&gt;&lt;br&gt;
The first line specifies the Docker compose version, which is 3.8 in this case.&lt;/p&gt;

&lt;p&gt;The services command specifies the containers to run, which is a just web service container in this case. As mentioned earlier, you can specify several containers to run.&lt;/p&gt;

&lt;p&gt;The build command instructs Docker to look for a Dockerfile within the current working directory. Next, we start our server at port 8000, which is Django’s default port.&lt;/p&gt;

&lt;p&gt;The volumes mount syncs Docker’s file system with our local file system, meaning we do not have to edit the files locally once we edit them inside Docker.&lt;/p&gt;

&lt;p&gt;Finally, we expose port 8000 within Docker.&lt;/p&gt;

&lt;p&gt;Time to run our container. Type this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker-compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should get the following output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Creating network &lt;span class="s2"&gt;"code_default"&lt;/span&gt; with the default driver
Building web
Step 1/7 : FROM python:3.8 ...
Creating code_web_1 ... &lt;span class="k"&gt;done
&lt;/span&gt;Attaching to code_web_1
web_1 | Watching &lt;span class="k"&gt;for &lt;/span&gt;file changes with StatReloader
web_1 | Performing system checks...
web_1 |
web_1 | System check identified no issues &lt;span class="o"&gt;(&lt;/span&gt;0 silenced&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
web_1 | May 03, 2024 - 19:28:08
web_1 | Django version 3.1, using settings &lt;span class="s1"&gt;'config.settings'&lt;/span&gt;
web_1 | Starting development server at http://0.0.0.0:8000/
web_1 | Quit the server with CONTROL-C.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you visit your browser at &lt;a href="http://127.0.0.1:8000" rel="noopener noreferrer"&gt;http://127.0.0.1:8000&lt;/a&gt;, you should see the default Django successfully installed page as before. This means that you have successfully containerized your Django project using Docker.&lt;br&gt;
Since Docker stores a running container in RAM, and a container takes a lot of space (A simple container could be over 1GB), it is advisable to stop the container when not in use. To do so, run:&lt;br&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker-compose down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Building within Docker
&lt;/h3&gt;

&lt;p&gt;We can start our docker container in &lt;strong&gt;detached mode&lt;/strong&gt;. Running a container in detached mode allows you to use a single terminal without having to stop your server or use multiple terminals. To use a container in detached mode, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker-compose up –d 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Building within Docker is no different from building locally. The only exception is we now have to preface any command we run with &lt;code&gt;docker-compose exec web&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We are going to create an app called &lt;strong&gt;blog&lt;/strong&gt; then create the necessary model, view and template for our blog web app.&lt;br&gt;&lt;br&gt;
Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;docker-compose &lt;span class="nb"&gt;exec &lt;/span&gt;web python manage.py startapp blog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# blog/models.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Topic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;topic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;topic&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Entry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;topic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ForeignKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;on_delete&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CASCADE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TextField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;[:&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then migrate your database to apply these new models&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker-compose &lt;span class="nb"&gt;exec &lt;/span&gt;web python manage.py makemigrations blog
docker-compose &lt;span class="nb"&gt;exec &lt;/span&gt;web python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, you should register your models with the admin site at blog/admin.py so you can interact with them via them admin panel at &lt;a href="http://127.0.0.1:8000/admin" rel="noopener noreferrer"&gt;http://127.0.0.1:8000/admin&lt;/a&gt;. You can try adding a few topics and entries via the admin.&lt;/p&gt;

&lt;p&gt;Next configure URL routes for the blog app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/urls.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;

&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;admin/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;blog.urls&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# blog/urls.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;.views&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;TopicListView&lt;/span&gt;
&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nc"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;''&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TopicListView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;as_view&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;topics&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Write a view to list all topics available in the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# blog/views.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Topic&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.views.generic&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ListView&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TopicListView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ListView&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Lists all topics&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Topic&lt;/span&gt;
    &lt;span class="n"&gt;context_object_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;topic_list&lt;/span&gt;
    &lt;span class="n"&gt;template_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;topics.html&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Render the topics list to the user interface&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--templates/topic.html--&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Topics&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
    {% for topic in topic_list %}
        &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;{{ topic }}&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you have updated your app files like above, try visiting the local host URL to check if it lists all the topics you have added. If you are encountering any errors, Docker provides an error log. Run:&lt;br&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;docker logs&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  A Summary of Steps
&lt;/h2&gt;

&lt;p&gt;In summary, follow these steps to containerize your Django application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a virtual environment locally and install Django&lt;/li&gt;
&lt;li&gt;Create a new project&lt;/li&gt;
&lt;li&gt;Exit the virtual environment&lt;/li&gt;
&lt;li&gt;Write a Dockerfile and then build the initial image&lt;/li&gt;
&lt;li&gt;Write a docker-compose.yml file and run the container with docker-compose up&lt;/li&gt;
&lt;li&gt;Rebuild your image every time you install a software package&lt;/li&gt;
&lt;li&gt;Restart Docker every time you make changes to your settings.py file&lt;/li&gt;
&lt;li&gt;Run docker logs if you encounter any errors&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Modern web development involves working in teams. Therefore, the development necessitates building using containers such as Docker. It is important for beginners in backend development familiarize with Docker as early as possible. This article covers the fundamentals of getting started with Docker. It discusses fundamental Docker concepts and commands. Whereas there is more to containerization and the Docker technology that requires further learning, this article is an excellent starting point for beginners. You can start here and work your way up to becoming an expert. Docker itself has a steep learning curve, and it takes more than what we have discussed here to gain proficiency. Nevertheless, you can start here and build small, containerized Django applications to familiarize yourself with the basics. Happy containerization!&lt;/p&gt;

</description>
      <category>django</category>
      <category>docker</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Django ORM: The Magic between the Application and the Database</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Sun, 17 Mar 2024 20:03:59 +0000</pubDate>
      <link>https://dev.to/odhiambo/the-django-orm-the-magic-between-the-application-and-the-database-4k8g</link>
      <guid>https://dev.to/odhiambo/the-django-orm-the-magic-between-the-application-and-the-database-4k8g</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Applications that constitute a database allow users to interact with the application’s data through &lt;strong&gt;Object Relational Mappers (ORMs)&lt;/strong&gt;.  ORMs are the intermediaries between an application and a database. They provide a set of functionality that enable &lt;a href="https://www.codecademy.com/article/what-is-crud"&gt;CRUD&lt;/a&gt; database operations. Many server-side programming frameworks like Python’s Django provide ORMs that allow developers to write database queries in the pure server-side language. The ORM then performs a database operation in a language understood by the database type in use. This article discusses the Django ORM, including the functionalities that the ORM provides to facilitate database interactions.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Brief Overview
&lt;/h2&gt;

&lt;p&gt;When building a backend application using Django, you would typically install Django in a virtual environment. Django installs a SQLite database by default, which comes with the Django ORM to allow database operations. You may have interacted with the Django ORM functionality without noticing. For instance, every time you have used &lt;code&gt;.get()&lt;/code&gt; or &lt;code&gt;.filter()&lt;/code&gt; methods somewhere in your views.py file, you were using Django ORM methods to query the database.&lt;/p&gt;

&lt;p&gt;Django ORM methods can broadly be classified into two types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Those that interact with a queryset&lt;/li&gt;
&lt;li&gt;Those that interact with a model object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In simple terms, a queryset is single or several database records. A database record is basically a data row in a database. Therefore, queryset methods act on database records. A model object is an instance, or a definition if you may, of a database record. A model object provides information about a database record. That is, what &lt;a href="https://www.inetsoft.com/info/database-fields-and-types/"&gt;fields&lt;/a&gt; does the database record have? For instance, a database record could have ID, name, URL, image or Foreign Key fields. Django ORM has methods that can manipulate these database record fields.&lt;/p&gt;

&lt;h2&gt;
  
  
  Django ORM Methods
&lt;/h2&gt;

&lt;p&gt;This section discusses the most commonly used Django ORM methods. The methods are grouped based on related functionality. That is, the methods that perform a similar functions are discussed together. Note that based on the broad classification discussed above, methods within the same category could interact with either a queryset or a model object.&lt;/p&gt;

&lt;h3&gt;
  
  
  Filtering Methods
&lt;/h3&gt;

&lt;p&gt;Filtering methods return querysets depending on the lookup parameters passed to the methods.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;.filter(**kwargs)&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;.filter()&lt;/code&gt; returns a queryset that matches the lookup parameters passed to the method. For instance, assume you are a querying a database field, &lt;strong&gt;name&lt;/strong&gt;, for all database records containing the name &lt;strong&gt;bob&lt;/strong&gt;. In this case you would need to filter your database by &lt;strong&gt;bob&lt;/strong&gt; as a &lt;strong&gt;keyword argument&lt;/strong&gt;. Django ORM provides a magic method, &lt;strong&gt;__icontains&lt;/strong&gt;, that attaches to a database field, which is useful for string and substring matching. You would implement this operation like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;YourModel&lt;/span&gt;
&lt;span class="n"&gt;bob_records&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;YourModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name__icontains&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bob&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another common example where you would use &lt;code&gt;.filter()&lt;/code&gt; is when you have implemented user accounts in your Django application. You may want to restrict each user to their data. In this case, if a user requests some data, you want to show them only the data that belongs to them. You would typically use &lt;code&gt;.filter()&lt;/code&gt; method to avail data by the user making the request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;your_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;YourModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Assuming in your data model you have a field called &lt;code&gt;owner&lt;/code&gt; that associates data and users&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;.excludes(**kwargs):&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Works the same way as &lt;code&gt;.filter()&lt;/code&gt; but returns database records that do not match the lookup parameters.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;.get(**kwargs):&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;This method is among the most commonly used. Like .&lt;code&gt;filter()&lt;/code&gt;, the method is used to retrieve the data that matches the lookup parameters passed to it as keyword arguments. However, &lt;code&gt;.get()&lt;/code&gt; returns only a single database record  while &lt;code&gt;.filter()&lt;/code&gt; returns one or more records. &lt;code&gt;.get()&lt;/code&gt; raises a &lt;a href="https://docs.djangoproject.com/en/5.0/ref/exceptions/"&gt;&lt;code&gt;MultipleObjectsReturned&lt;/code&gt;&lt;/a&gt; error or &lt;a href="https://docs.djangoproject.com/en/5.0/ref/exceptions/"&gt;&lt;code&gt;DoesNotExist&lt;/code&gt;&lt;/a&gt; error exception if multiple objects are found or no object is found, respectively. Below is an example of how you would use &lt;code&gt;.get()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;YourModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This queryset returns a database record with an id 1&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;.first()&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Returns the first object matched by the queryset or &lt;code&gt;None&lt;/code&gt; if the queryset is empty.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;.last()&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;Returns the last object matched by the queryset or &lt;code&gt;None&lt;/code&gt; if the queryset is empty.&lt;br&gt;
&lt;code&gt;.first()&lt;/code&gt; and &lt;code&gt;.last()&lt;/code&gt; are usually used with &lt;code&gt;.filter()&lt;/code&gt; incase one needs only the first or the last item returned by .filter():&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;bob_records&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;YourModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name__icontains&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bob&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;first&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Aggregation Methods
&lt;/h3&gt;

&lt;p&gt;Aggregation methods perform arithmetic calculations on querysets. They include:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;aggregate(*args, **kwargs)&lt;/code&gt;:
&lt;/h4&gt;

&lt;p&gt;Performs aggregate calculations (e.g., &lt;code&gt;Count&lt;/code&gt;, &lt;code&gt;Sum&lt;/code&gt;, &lt;code&gt;Avg&lt;/code&gt;, etc.) on the queryset&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;.count()&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;returns the number of objects in a queryset&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;.exists()&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Evaluates to true if a queryset contains any results, else false. This method could be combined with &lt;code&gt;.filter()&lt;/code&gt; in a real-world use case. Assume you want to add a record to the database, but only if no similar record already exists. Say you want to add a name bob to a database field name:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;If&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;YourModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bob&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
  &lt;span class="n"&gt;model_instance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;YourModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bob&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Ordering Methods
&lt;/h3&gt;

&lt;p&gt;Ordering methods sort a queryset based on declared model fields.&lt;br&gt;
Assume you have this model&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Names&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;date_added&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DateTimeField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auto_now_add&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Names.objects.order_by('date_added')&lt;/code&gt;: returns a queryset based on the date they were added to the database. The first added entry is returned first. Assuming you want to return a queryset based on the most recently added record, you would reverse the order like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;order_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-date_added&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt; &lt;span class="c1"&gt;# The '-' sign indicates 'reverse'
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, Django ORM provides a .reverse() method for reversing the order of querysets:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Names&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;order_by&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;date_added&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, you could sort the entries of the above model like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;Ordering&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;date_added&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, you provide a Meta class inside your model class and define the desired ordering using the attribute ordering&lt;/p&gt;

&lt;h3&gt;
  
  
  Update and Delete
&lt;/h3&gt;

&lt;p&gt;Methods for updating or deleting objects in the database.&lt;br&gt;
 &lt;code&gt;.update(**kwargs)&lt;/code&gt;: Updates all objects(fields) in the queryset with the given values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;YourModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;bob&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above query updates the model field "name" with value "bob"&lt;br&gt;
You can update more than one field with .update()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;YourModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="n"&gt;bob&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;gender&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="n"&gt;male&lt;/span&gt;&lt;span class="err"&gt;”&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;.delete()&lt;/code&gt;: Deletes all objects in the queryset.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;YourModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above query deletes a database record with id 1. Note that while &lt;code&gt;.update()&lt;/code&gt; can act on a part of a database record(a field), &lt;code&gt;.delete()&lt;/code&gt; acts on the entire record(all fields);&lt;code&gt;.delete()&lt;/code&gt; erases the entire record at once from the database.&lt;/p&gt;

&lt;h3&gt;
  
  
  Other Methods
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;.values(*fields, **expressions)&lt;/code&gt;: Returns a queryset that returns dictionaries instead of model instances, with values according to the specified fields and expressions.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.values_list(*fields, flat=False)&lt;/code&gt;: Similar to &lt;code&gt;values()&lt;/code&gt; but returns tuples instead of dictionaries.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.values()&lt;/code&gt; and &lt;code&gt;.value_list()&lt;/code&gt; are helpful when you want to return specific database fields while excluding others.&lt;br&gt;
Assume you have this model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib.auth.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;User&lt;/span&gt;

&lt;span class="c1"&gt;# Create your models here.
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PasswordEntry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Defines password&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;s data structure&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;website_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;URLField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;created_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DateTimeField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auto_now_add&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;updated_at&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DateTimeField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auto_now&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;owner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ForeignKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;on_delete&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CASCADE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;ordering&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;-created_at&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;verbose_name_plural&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;password entries&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Returns a string rep of password entry&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;website_url&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;-&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a password entries model that allows a user to store passwords to their various user accounts. If you are building a similar Django application, you may want to implement a functionality that allows users to search for specific passwords. In this application, you may want to display the password entries by website URL or username instead of the actual password, for security reasons&lt;/p&gt;

&lt;p&gt;Your search functionality may look something like this. Notice how &lt;code&gt;.values_list()&lt;/code&gt; comes in handy when you want to choose the fields to display:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;search_entry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Searches for a password entry by site name&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;GET&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;q&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;entries&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PasswordEntry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;website_url__icontains&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;values_list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;id&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;website_url&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;entries&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PasswordEntry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;owner&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;values_list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;id&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;website_url&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;entries&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;manager/search_entries.html&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;.values_list()&lt;/code&gt; and &lt;code&gt;.values()&lt;/code&gt; act on model instances instead of querysets. Attempting to apply the methods on querysets will raise an attribute error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Object relational Mappers (ORMs) are a useful technology. They allow backend developers to query databases without having to write raw statements in a standard database query language like Structured Query Language (SQL). ORMs, therefore, reduce the chances of database injection attacks. The attacks can occur if users access the database directly using languages like SQL. Moreover, developers do not have to necessarily understand a query language; ORMs conduct most of the heavy lifting.&lt;/p&gt;

&lt;p&gt;Django provides its own ORM, the Django ORM. The Django ORM avails different types of methods that enable developers to perform CRUD operations on the database. The methods perform operations such as filtering, aggregation, ordering, updating and deleting. Several errors are bound to rise if the methods are used inappropriately. This article mentions several errors such as AttributeError, MultipleObjectsReturnedError and DoesNotExistError. Nevertheless, the Django ORM is a powerful tool for executing database transactions. Try it out, and happy querying!&lt;/p&gt;

</description>
      <category>django</category>
      <category>orm</category>
      <category>database</category>
      <category>python</category>
    </item>
    <item>
      <title>Understanding Http Redirections in Django</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Thu, 22 Feb 2024 06:12:43 +0000</pubDate>
      <link>https://dev.to/odhiambo/understanding-http-redirections-in-django-4b27</link>
      <guid>https://dev.to/odhiambo/understanding-http-redirections-in-django-4b27</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;As a web developer, you may need to perform Http redirections in your backend logic. You may implement Http redirections under several circumstances:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When redesigning a website URLs&lt;/li&gt;
&lt;li&gt;When rerouting traffic to HTTPS&lt;/li&gt;
&lt;li&gt;When enforcing authentication and authorization to protected resources&lt;/li&gt;
&lt;li&gt;When using canonical URLs&lt;/li&gt;
&lt;li&gt;When implementing a shortened URL functionality
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article briefly discusses the above circumstances. Moreover, the article explains the Http redirection types you would typically perform. Finally, it elaborates on how you may implement temporary Http redirections in Django. The article provides a useful starting point for engineers getting started with Http redirections in Django. Additionally, the article could be an informative piece for general learning.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Read this Article
&lt;/h2&gt;

&lt;p&gt;The article has two sections, one that provides an overview of Http redirections in general, and the other that gets into Http redirections in Django.  You can start from the beginning or jump to implementing Http redirections in Django.&lt;/p&gt;

&lt;p&gt;&lt;a id="beginning"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Redirect URLs?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs8ic35vmgfn4zvrd7r9c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs8ic35vmgfn4zvrd7r9c.png" alt="Http redirection process" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  When Redesigning your Website URLs
&lt;/h3&gt;

&lt;p&gt;As a web developer, you may perform major backend updates from time to time. The updates may involve changing the structure of your URLs or updating the URLs to new routes. In such a case, your website's users may encounter a &lt;a href="https://en.wikipedia.org/wiki/HTTP_404#:~:text=In%20computer%20network%20communications%2C%20the,not%20find%20what%20was%20requested."&gt;Page Not Found Error&lt;/a&gt; if they visit your old URLs. To prevent the error, you would let users make requests using the URLs they are used to, and then handle Http redirections to the new URLs.&lt;/p&gt;

&lt;h3&gt;
  
  
  When Implementing a URL shortening functionality.
&lt;/h3&gt;

&lt;p&gt;Sometimes you may want to convert long URL strings to shorter strings that are memorable. Implementing a URL shortening functionality may be necessary in this case, to enhance user experience. When users request resources from your server using the shortened URLs, they should be redirected to the actual URLs. In your backend logic, you would need to map the shortened URLs to their corresponding longer versions.&lt;/p&gt;

&lt;h3&gt;
  
  
  When Re-routing Web Traffic to HTTPS
&lt;/h3&gt;

&lt;p&gt;Many website applications reroute non-HTTPS traffic to HTTPS for security reasons. &lt;a href="https://en.wikipedia.org/wiki/HTTPS"&gt;HTTPS&lt;/a&gt; is a secure protocol with which a web server and client computers communicate. System engineers typically configure web servers such as NGINX and Apache to redirect all non-HTTPS traffic.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg036wn5pa921k3sz8b0d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg036wn5pa921k3sz8b0d.png" alt="Http to Https" width="300" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  When Enforcing Authentication and Authorization
&lt;/h3&gt;

&lt;p&gt;Websites restrict access to protected content by requiring prior user authentication and authorization. In such cases, if an unauthenticated or unauthorized user tries to access a protected resource, the website would redirect them to login or registration page. A backend engineer implements the Http redirection.&lt;/p&gt;

&lt;h3&gt;
  
  
  When using Canonical URLs
&lt;/h3&gt;

&lt;p&gt;A &lt;a href="https://www.semrush.com/blog/canonical-url-guide/"&gt;canonical URL&lt;/a&gt; is the preferred URL(s) in a list of URLs that can access the same web resource. In some cases, web apps may have different URLs that map to the same resource. In such cases, web developers may implement canonical(read preferred URLs), for search engine optimization purposes. The idea is to map the least preferred URLs to the canonical URLs. In addition to optimizing search engines, canonicalization ensures predictable and consistent browsing experience for a website's users. Canonicalization can be implemented both on the front-end and back-end.&lt;/p&gt;

&lt;p&gt;&lt;a id="django"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Redirection
&lt;/h2&gt;

&lt;p&gt;In a Django Project, and based on the above Http redirection circumstances, a Django engineer would implement both temporary and permanent Http redirections. A temporary URL redirection means that, while a web resource can be internally accessed via a new URL, the client should continue using the old/original URL to request the resource. &lt;/p&gt;

&lt;p&gt;Suppose you are changing your URL formats but want the user to continue using previous URLs, which they are used to. You would implement a temporary redirection. When users visit your web resources using the previous URLs, your app would redirect them to the new URLs&lt;/p&gt;

&lt;p&gt;You may also implement a permanent redirection. You use this redirection type where the structure of a URL has changed fundamentally. Permanent redirections instruct clients (browsers and search engine crawlers) to cache the new URL and use it to make future requests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbe5307bbbi0qd08sorp5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbe5307bbbi0qd08sorp5.png" alt="Types of Http redirections" width="800" height="396"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;HTTP status codes in the range &lt;a href="https://kinsta.com/knowledgebase/307-redirect/"&gt;3XX&lt;/a&gt; represent the different types of Http redirections. The server returns status codes 302 and 307 for temporary redirections. In contrast, codes 301 and 308 signify permanent redirections. When running a Django web server in a development environment, you can inspect the server response header of a URL endpoint to check the Http redirection type.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Implement a Temporary Redirection in Django
&lt;/h2&gt;

&lt;p&gt;Here is how you would implement a temporary redirection in your Django project. You would typically implement the redirection in your views.py file. Suppose you need to redirect users to the home page after they have logged into their accounts. In this case, you would implement the Http redirection in your login view logic, after a user has been authenticated.&lt;br&gt;
Assume you have this login view function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# views.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib.auth&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;login&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.http&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HttpResponseRedirect&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;login_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Logs in users into their accounts&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;email&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;password&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;none&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;HttpResponseRedirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;myapp:index&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, take note of the following lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.http&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HttpResponseRedirect&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;HttpResponseRedirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;myapp:index&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You would need to import the &lt;code&gt;HttpResponseRedirect&lt;/code&gt; class object and the &lt;code&gt;reverse&lt;/code&gt; function from the correct libraries as above. After you have defined the logic for your view function and need to perform a redirection, return HttpResponseRedirect class, passing the reverse function to it. Additionally, you need to pass the URL you want the view to redirect to as a parameter of the reverse function&lt;/p&gt;

&lt;h3&gt;
  
  
  The HttpResponseRedirect Class
&lt;/h3&gt;

&lt;p&gt;This is a Python class object used to perform Http redirections to specified URLs. The class takes a single parameter, the URL to redirect to. Below is the class's definition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HttpResponseRedirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;redirect_to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Location&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;iri_to_uri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;redirect_to&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;302&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;redirect_to&lt;/code&gt; parameter represents the URL the client should be redirected to. This URL is set in the &lt;code&gt;Location header&lt;/code&gt; of the server response.&lt;/p&gt;

&lt;h3&gt;
  
  
  The reverse() Function
&lt;/h3&gt;

&lt;p&gt;The reverse function dynamically generates a URL string based on a view's namespace or its URL pattern. The namespace is defined in a Django app's urls.py configuration file. Below is the reverse function's signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;urlconf&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;current_app&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;viewname&lt;/code&gt; parameter is the main argument. This parameter is required. &lt;code&gt;viewname&lt;/code&gt; is the namespace of a view function or the associated URL pattern specified in your app's url.py file. Returning to our login example above, in a Django app called 'app', you might have configured your index page URL configuration like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# urls.py
&lt;/span&gt;&lt;span class="n"&gt;url_patterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/index/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;index&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                                &lt;span class="bp"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;index&lt;/code&gt; is the namespace for the index view function, or namespace for your index URL pattern. A namespace can be understood as an identifier for a particular view or URL. You can reference a URL or a view anywhere in your Django app using its namespace.&lt;/p&gt;

&lt;p&gt;Other parameters passed to reverse are optional. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;urlconf&lt;/code&gt;: allows you to specify a URL pattern for the redirect URL.  When building a Django project, you define several patterns in different apps for your URLs. By default, the root URL pattern for the current app is selected. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;args&lt;/code&gt;: allows you to pass positional arguments to reverse as a list or tuple.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;kwargs&lt;/code&gt;: a dictionary containing keyword arguments you may need to pass into reverse. You cannot pass args and kwargs at the same time&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;current_app&lt;/code&gt;: Specifies the name of the app that the namespace belongs to.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The reverse function constructs and returns a URL string when the resolver finds a URL pattern matching the namespace. If no match is found, the resolver raises a &lt;a href="https://docs.djangoproject.com/en/dev/ref/exceptions/#django.urls.NoReverseMatch"&gt;NoReverseMatch&lt;/a&gt; exception.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Cases
&lt;/h3&gt;

&lt;p&gt;In some cases, you may pass a URL string as an argument directly to the HttpResponseRedirect class instance. In this case, you are hardcoding the redirection. In case you change the structure of your redirect URL, you would need to explicitly update the URL string passed to HttpResponseRedirect.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# views.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.http&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HttpResponseRedirect&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Assuming redirect_url is the URL you want to redirect to
&lt;/span&gt;    &lt;span class="c1"&gt;# Redirect URL could be a relative or absolute path of a URL
&lt;/span&gt;    &lt;span class="c1"&gt;# within your Django project
&lt;/span&gt;    &lt;span class="n"&gt;redirect_url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;/some/redirect/url&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;HttpResponseRedirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;redirect_url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, it is considered best practice to use HttpResponseRedirect with reverse. The technique offers more flexibility and makes it easier to maintain and read code.&lt;/p&gt;

&lt;p&gt;Take this use case for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# views.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.http&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HttpResponseRedirect&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Some logic to determine view name or URL pattern dynamically
&lt;/span&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;some_condition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;view_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;app_name:view_name_1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;view_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;app_name:view_name_2&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

    &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;HttpResponseRedirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above case, if you change the structure of a URL, you only need to update the value of &lt;code&gt;view_name&lt;/code&gt; variables. Moreover, you can dynamically redirect to different URLs based on custom conditions. The approach offers more freedom.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Complex Case
&lt;/h3&gt;

&lt;p&gt;As discussed above, the reverse function takes other optional parameters. Sometimes you may need to redirect to a URL that takes additional parameters in the URL string. In this situation, you need to pass the additional parameters either as positional or keyword arguments to reverse.&lt;/p&gt;

&lt;p&gt;Assume you are writing a view that updates the information about an object stored in a database. The view retrieves the object by ID and should redirect to a URL that returns the updated object. In such a case, you may pass the object's ID as a keyword argument to reverse. Look at the example below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# views.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.http&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;HttpResponseRedirect&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;reverse&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;update_object&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;
    Updates an object by id
    args:
        request: request object
        pk: object id
    &lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
                &lt;span class="bp"&gt;...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;HttpResponseRedirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;myapp:some_view_name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
        &lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;pk&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;pk&lt;/span&gt;&lt;span class="p"&gt;}))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;A Django engineer may occasionally need to perform Http redirections for various reasons. Understanding the various redirection types and implementation strategies is key to executing successful redirections. Python provides tools that facilitate Http redirections. The HttpResponseRedirect class object takes in a URL string and redirects the current view to the redirect URL, which is the URL string argument passed to the class instance. When the class object is used with the reverse function, Http redirection becomes more flexible. The reverse function takes in a namespace, representing a view or the view's URL pattern. Then a URL resolver engine matches the namespace to a configured URL pattern and executes the redirection. That's all. Have fun implementing Http redirections in your Django project!&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Handling JSON Data Serialization when Working with Django REST Framework APIs</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Fri, 02 Feb 2024 15:50:10 +0000</pubDate>
      <link>https://dev.to/odhiambo/handling-json-data-serialization-when-working-with-django-rest-framework-apis-59g7</link>
      <guid>https://dev.to/odhiambo/handling-json-data-serialization-when-working-with-django-rest-framework-apis-59g7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Website development, and computer programming in general, involves handling and manipulating data. Many web apps constitute database serves that hold various types of data. Client computers frequently need to retrieve or update the data, which necessitates data transmission back and forth, between the front-end and back-end.&lt;/p&gt;

&lt;p&gt;Applications transmit data in several formats between origin and destination entities, with encoding and decoding of the data taking place at source and destination respectively. This article discusses the encoding and decoding of data in Python backend applications using JSON format, with a specific focus on Django Rest Framework APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A basic understanding of the concept of serialization&lt;/li&gt;
&lt;li&gt;A basic understanding of the Django REST Framework&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  A Simplistic Overview
&lt;/h2&gt;

&lt;p&gt;As mentioned, software systems transmit data in various formats. Common formats include &lt;strong&gt;JavaScript Object Notation(JSON)&lt;/strong&gt;, XML, and CSV. However, web applications widely implement the JSON transmission due to JSON's compact structure and readability. Moreover, since JSON's syntax closely resembles a JavaScript's object syntax, and JavaScript is the primary language for modern web browsers, JSON enjoys robust browser and server-side support.&lt;/p&gt;

&lt;h3&gt;
  
  
  Serialization
&lt;/h3&gt;

&lt;p&gt;In simple terms, serialization basically means "encoding" of data. Serialization is the practice of converting a data object that constructed in one of a programming language's native data types into a format suitable for transmission, such as JSON. Python data objects, for instance, could be of any native Python data types—string, integer, list or dictionary. Serializing such an object involves converting its format from, say, a Python string to a JSON string.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deserialization
&lt;/h3&gt;

&lt;p&gt;Deserialization could be understood as "decoding" of data at the destination. The concept is the opposite of serialization. In this case, a JSON or XML data structure is reconverted into a native type. For instance, a Python string that was processed into a JSON string at the source is reverted into a Python string. The data could then be stored in a database or used in some other Python program.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fze21agsbi7tmem5k6drc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fze21agsbi7tmem5k6drc.jpg" alt="Image description" width="600" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Serialize Data?
&lt;/h2&gt;

&lt;p&gt;Serialization of data intends to achieve various efficiency metrics in applications. The metrics include performance, responsiveness, speed, security etc. In the context of web applications, serializing data facilitates:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Seamless portability of data between front-end and back-end systems written in different programming language&lt;/li&gt;
&lt;li&gt;Reduced network bandwidth during the transmission process since data is compacted&lt;/li&gt;
&lt;li&gt;Reduced network latency during transmission.&lt;/li&gt;
&lt;li&gt;Easy conversion of data from a complex type to a string of bytes for network transmission&lt;/li&gt;
&lt;li&gt;Validation and implementation of security checks for the data being transmitted&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How is Data Serialization and Deserialization achieved in Python?
&lt;/h2&gt;

&lt;p&gt;Python has built-in modules that facilitate serialization and deserialization in common data formats. The most popular Python data modules include &lt;code&gt;json&lt;/code&gt; and &lt;code&gt;csv&lt;/code&gt;. For purposes of this article, we will not discuss working with these modules for basic implementation of serialization in Python. For more information regarding JSON and CSV formats in Python, &lt;a href="https://www.geeksforgeeks.org/convert-json-to-csv-in-python/"&gt;check out this tutorial&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  JSON Serialization in Django REST Framework APIs
&lt;/h2&gt;

&lt;p&gt;Understanding data serialization comes in handy when working with &lt;strong&gt;Application Programming Interfaces (APIs)&lt;/strong&gt;.  One of the primary functions of APIs is to avail data to front-end applications. When building APIs that deliver data to the front-end, backend engineers define data serialization mechanisms in the backend logic.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;Django REST Framework (DRF)&lt;/strong&gt;, it is common practice for backend engineers to create a &lt;strong&gt;serializers.py&lt;/strong&gt; file that defines an application's data serialization logic. The serialization logic, in this case, constitutes one or several Python class objects. The class objects are defined therein, imported into &lt;strong&gt;views.py&lt;/strong&gt; file and applied to API view classes or functions.&lt;/p&gt;

&lt;p&gt;To recap a typical Django web application file structure, assume you are designing APIs of a restaurant web app. Your app’s file structure, including the serializers.py, might look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  -&amp;gt;Project’s root directory
    -&amp;gt;manage.py
    -&amp;gt;restaurant_app directory
      -&amp;gt; urls.py
      -&amp;gt; views.py
      -&amp;gt; models.py
      -&amp;gt; serializers.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your project’s root directory might host several applications with different APIs defined. Each application should have its serializers.py file.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Typical Serializer Class
&lt;/h3&gt;

&lt;p&gt;To define a typical serializer Python class object for your API, you need to provide the following inside your serializer class definition:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A nested Meta class&lt;/li&gt;
&lt;li&gt;Your model&lt;/li&gt;
&lt;li&gt;The model fields to be serialized&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Assume in your restaurant’s model.py file you have defined the following fields:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;Django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;

&lt;span class="c1"&gt;# Create your models here
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Available_Foods&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Food types in my restaurant&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="nb"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;IntegerField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;primary_key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;food_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;null&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The model above has two fields that need to be serialized: id and food_name. Your serializer.py might look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# serializer.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;rest_framework&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;serializers&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Available_Foods&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Available_Foods_Serializer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;serializers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ModelSerializer&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Serializes my model fields&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Available_Foods&lt;/span&gt;
        &lt;span class="n"&gt;fields&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;id&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this typical serializer class, you need to import the &lt;strong&gt;serializers&lt;/strong&gt; module, which DRF provides. The module contains a Python class object known as &lt;strong&gt;ModelSerializer&lt;/strong&gt; which will act as a super class to all the classes you define in the serializers.py file.&lt;/p&gt;

&lt;p&gt;Inside your custom serializer class, you need to provide a nested class, Meta, that specifies the model whose fields need to be serialized. Additionally, you need explicitly state the model fields to include in the serialization. In some cases you may not need to serialize all fields, and hence, DRF serialization offers this flexibility. If you need to serialize all fields without explicitly stating them in the &lt;strong&gt;fields&lt;/strong&gt; variable array, you can replace the fields array value with this Python magic method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;fields&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="n"&gt;__all__&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After you have defined your serializer class and the fields you need serialized, you can go ahead and apply the serializer class(es) to your API views like this:&lt;br&gt;
1.Import your custom serializer class(es) into your views.py file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;.serializers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Available_Foods_Serializer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Define your view class(es), if not already defined. &lt;br&gt;
3.Enter your serializer class at the beginning of your API definition, after the line that defines the queryset which the API will act upon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ListFoods&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;generics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ListAPIView&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="sh"&gt;"""&lt;/span&gt;&lt;span class="s"&gt;Lists all available food items&lt;/span&gt;&lt;span class="sh"&gt;"""&lt;/span&gt;
    &lt;span class="n"&gt;queryset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Available_Foods&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;serializer_class&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Available_Foods_Serializer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Sending Serialized Data to a DRF API Endpoint
&lt;/h3&gt;

&lt;p&gt;Using our restaurant project, assume you want to create a food item that your restaurant offers, identified by ID and name, and store it in the database. In a development environment, you will need to enter the API endpoint, which should already be defined in your application's &lt;strong&gt;urls.py&lt;/strong&gt; file, in a browser, with some JSON formatted data. Look at the example below:&lt;/p&gt;

&lt;p&gt;Assume you have this endpoint for populating your database with available foods:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;/root_url/restaurant_app/create-food-item/&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;DRF’s browsable API provides an effective platform for working with DRF APIs in your browser. Your &lt;strong&gt;POST request&lt;/strong&gt; data to the above endpoint might look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"food_name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"pizza"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the format of the request data. The format is JSON—key-value pairs with all strings in double quotations. The data is said to be JSON serialized, ready for transmission. If you send a POST request with this data to the above endpoint, the data will be deserialized into a Python object and stored in the database.&lt;/p&gt;

&lt;p&gt;The server may send a similar response as below if your create operation was a success. Note that the response contains the object you just created in JSON format.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;POST /restaurant_app/create-food-item/
HTTP 201 CREATED OK
Allow: POST
Content-Type: application/json

{
    "id": 1,
    "food_name": "pizza"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you inspect the server's response header’s &lt;strong&gt;Content-Type&lt;/strong&gt; field value,  it displays &lt;strong&gt;application/json&lt;/strong&gt;. The Content-Type field returns the data format of the response body, which, in this case, is JSON.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Working with the JSON data format when DRF APIs could be challenging for first timers. While this article provides useful information for those getting started with data serialization in DRF APIs, it is certainly not exhaustive.&lt;/p&gt;

&lt;p&gt;This article fails to mention the mistakes that developers can commit while defining models and serialization classes in Django. The mistakes mess up serialized outputs and throw errors when sending requests to endpoints. Additionally, a backend engineer may need to develop more complex serialization class objects, not captured in this article, based on the front-end consumer application’s needs.&lt;/p&gt;

&lt;p&gt;Nevertheless, this article offers a decent entry point to those who are getting started with API serialization in Django. Happy coding!&lt;/p&gt;

</description>
      <category>django</category>
      <category>restapi</category>
      <category>python</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Deploy your Django Web App and connect a MySQL Database in cPanel</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Tue, 23 Jan 2024 14:52:01 +0000</pubDate>
      <link>https://dev.to/odhiambo/how-to-deploy-your-django-web-app-and-connect-a-mysql-database-in-cpanel-43l3</link>
      <guid>https://dev.to/odhiambo/how-to-deploy-your-django-web-app-and-connect-a-mysql-database-in-cpanel-43l3</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;With many businesses going online, there is an increasing demand for web hosting services. Hosting services provide a platform where web developers can store web application files in a production environment.&lt;/p&gt;

&lt;p&gt;Several hosting services exist in the market. The services cater to a range of client needs, which includes the volume of traffic a web service is expected to receive, security needs and performance.&lt;/p&gt;

&lt;p&gt;Hosting services such as Digital Ocean are suitable when a web application is expected to receive huge traffic. However, such hosting sites are usually cost-prohibitive. In contrast, hosting services such as cPanel can efficiently host sites that handle medium to low traffic at lower costs. Moreover, cPanel is a convenient to-go service for practice projects. This article guides on how to host a Python Django project in cPanel.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An existing Django project&lt;/li&gt;
&lt;li&gt;A cPanel account with a domain name&lt;/li&gt;
&lt;li&gt;A zipped file of your website (Should be a .zip extension)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Meeting the Prerequisites
&lt;/h2&gt;

&lt;p&gt;If you do not have an existing Django project, &lt;a href="https://realpython.com/get-started-with-django-1/" rel="noopener noreferrer"&gt;use one here&lt;/a&gt;. For a cPanel account and domain set up, &lt;a href="https://www.namecheap.com/domains/?gad_source=1&amp;amp;gclid=Cj0KCQiAwP6sBhDAARIsAPfK_wapKXtCufJBjauiek3-34gk2x5dg65MY2EyvW70m2gGsDJCPXHeh2IaAiNoEALw_wcB" rel="noopener noreferrer"&gt;check this out&lt;/a&gt;. Now that you are ready to go with a Django project and a cPanel account, create a zipped file of your website like this:&lt;/p&gt;

&lt;h3&gt;
  
  
  Linux
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the directory that hosts your project's folder&lt;/li&gt;
&lt;li&gt;Type the command below. Remember to replace &lt;code&gt;archive.zip&lt;/code&gt; and 
&lt;code&gt;your_project_folder&lt;/code&gt; with actual names.
&lt;code&gt;bash
zip -r archive.zip your_project_folder
&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Windows &lt;a id="upload"&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to your project's folder location.&lt;/li&gt;
&lt;li&gt;Right click on the folder and select &lt;strong&gt;Add to archive&lt;/strong&gt;. A .zip file will be created.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Upload your Files
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Log in to cPanel using the username and password you set with the hosting provider&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Navigate to the &lt;strong&gt;File Manager&lt;/strong&gt; section on the cPanel dashboard. Click and select &lt;strong&gt;Public_html&lt;/strong&gt; under File Manager.&lt;br&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhakci4mnekfjt4vqdvg8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhakci4mnekfjt4vqdvg8.png" alt="File manager section"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This is only applicable if you are uploading the web app for the main domain and not the add-on domain or subdomain as they are located in a different folder within the file manager.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Click on &lt;strong&gt;Upload&lt;/strong&gt; then &lt;strong&gt;Select File&lt;/strong&gt;. You will be prompted to select a file from you PC.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjhx05mogiywepkistept.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjhx05mogiywepkistept.png" alt="Uploads option"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select the zipped file from your PC and hit upload. Alternatively you could drag and drop the .zip file to cPanel. Once the file finishes uploading, click on &lt;strong&gt;go back&lt;/strong&gt; to return to the &lt;strong&gt;Public_html&lt;/strong&gt; directory.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Locate your zipped file and right click on it, then select extract to extract subfolders and files within it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fly345jdqpmj5rl6ulqdn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fly345jdqpmj5rl6ulqdn.png" alt="Extract file option"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now your files are ready &lt;a id="db"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Set up a MySQL Database
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You need to perform this step before you set up your Django app. To set up a MySQL database:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Locate the &lt;strong&gt;Databases&lt;/strong&gt; section on the cPanel dashboard.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Select &lt;strong&gt;MySQL Databases&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhu5s3gscj9kcgm13x155.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhu5s3gscj9kcgm13x155.png" alt="MySQL Database section"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a database with a suitable name in the provided &lt;strong&gt;Create New Database&lt;/strong&gt; field.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Input user credentials, a username and password, in the &lt;strong&gt;Create New User&lt;/strong&gt; field to create a new user. Copy the password somewhere as you will need it to configure the database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the User &lt;strong&gt;Privileges&lt;/strong&gt; section, check &lt;strong&gt;All Privileges&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Confirm that the database has been created by clicking on the &lt;strong&gt;phpMyAdmin&lt;/strong&gt; option under Databases section.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Configure a MySQL database in your settings.py file.&lt;br&gt;
In development, you most likely have been using an SQLite database. Since we have switched to a MySQL database, you need to configure the new database settings.&lt;br&gt;&lt;br&gt;
Locate the &lt;strong&gt;DATABASES&lt;/strong&gt; section in your project's settings.py file and replace the existing database settings with the code below. Remember to replace the NAME (database name), USER, and PASSWORD fields with actual values:&lt;/p&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#settings.py
&lt;/span&gt;
&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;default&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ENGINE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.db.backends.mysql&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;NAME&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;mysql_db_name&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;HOST&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;localhost&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;PORT&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3306&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;USER&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;my_sql_user&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;PASSWORD&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;password&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Save and close the file.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Your database is now configured.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set up Your Django App &lt;a id="app"&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Create Your App
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Locate the &lt;strong&gt;Software&lt;/strong&gt; options in the cPanel Dashboard&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Click on &lt;strong&gt;Setup Python App&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcgzzu5fpu5dsebd953yr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcgzzu5fpu5dsebd953yr.png" alt="cPanel software section"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select &lt;strong&gt;CREATE APPLICATION&lt;/strong&gt; to begin setting up your Python app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select a Python version. The latest is recommended.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Input the root directory of your project under the &lt;strong&gt;Application root&lt;/strong&gt; field.  For instance, if your application root folder is called &lt;strong&gt;Django-project&lt;/strong&gt; and you uploaded it to &lt;strong&gt;Public_html&lt;/strong&gt;, your application root becomes &lt;strong&gt;Public_html/Django-project&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select a domain name for your application. The main domain is selected by default.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Leave the &lt;strong&gt;Application startup file&lt;/strong&gt; and &lt;strong&gt;Application Entry point&lt;/strong&gt; fields empty. The build process will fill them appropriately.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hit the &lt;strong&gt;CREATE&lt;/strong&gt; button in the top right bar to create your application. &lt;br&gt;
Notice that the build process has filled the two empty fields. We will need the &lt;strong&gt;passenger_wsgi.py&lt;/strong&gt; file in a moment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Edit the &lt;strong&gt;passenger_wsgi.py&lt;/strong&gt; file.&lt;br&gt;&lt;br&gt;
When you created your web app, the build process generated a file named &lt;strong&gt;passenger_wsgi.py&lt;/strong&gt; and stored it in the root directory of your project. You need to edit this file so that the production server can serve your application. Right click on the file and select the edit option. Once the file is opened, delete all contents in the file except the two import statements at the top. Next, add the following line below the imports: &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# passenger_wsgi.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;wsgi_directory.wsgi&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;application&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The line imports your application object from the wsgi.py file located in the same directory as your &lt;strong&gt;settings.py&lt;/strong&gt;. Remember to replace &lt;strong&gt;wsgi_directory&lt;/strong&gt; with the actual name of the directory. Next, save and close the file.&lt;/p&gt;

&lt;p&gt;Your have created a basic application&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; For subsequent steps, you might run into errors when installing dependencies and/or when running migrations. Such errors could, most likely, be a result of issues in your source code. You can inspect the &lt;strong&gt;stderr.log&lt;/strong&gt; file for error details. This file was created together with your app, and is located in your project's root directory. Read the error log and fix any problems in your source code before you proceed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set up a Virtual Environment
&lt;/h3&gt;

&lt;p&gt;Activate a Python virtual environment. This is necessary to store and separate your project's dependencies.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Note 1:&lt;/strong&gt; A virtual environment link was generated when you created the application. It can be accessed at the top of the page.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8wykv17agywwgz0pj7qi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8wykv17agywwgz0pj7qi.png" alt="Your app's virtual environment link"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Note 2:&lt;/strong&gt; You need to copy the link and paste it in a bash terminal provided by the hosting service. The terminal should be under &lt;strong&gt;Advanced&lt;/strong&gt; section on the cPanel dashboard.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Note 3:&lt;/strong&gt; Depending on your hosting plan, the terminal may not be available to you.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you can access the terminal:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Paste the virtual environment path into the terminal and press Enter. The process activates a virtual environment and changes into your project's root directory.&lt;/li&gt;
&lt;li&gt;Install project dependencies:
  &lt;code&gt;python
  pip install -r requirements.txt
&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create the database schema and tables
  &lt;code&gt;python
  python manage.py makemigrations
  python manage.py migrate
&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you do not have access to a terminal:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select the Setup Python App option. You should see your app under &lt;strong&gt;WEB APPLICATIONS&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Click on the edit icon under &lt;strong&gt;Actions&lt;/strong&gt; and scroll down to &lt;strong&gt;Configuration files&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F227lzqs4dnyxn3yj3s4d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F227lzqs4dnyxn3yj3s4d.png" alt="Configurations section"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enter the name of your dependencies file, in our case the &lt;strong&gt;requirements.txt file&lt;/strong&gt;, in the field provided, and click the &lt;strong&gt;Add&lt;/strong&gt; button.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click on &lt;strong&gt;Run Pip Install&lt;/strong&gt; button, which will prompt you to select the requirements.txt file you just added.&lt;br&gt;&lt;br&gt;
Wait for a few moments while the build process installs dependencies in your virtual environment. You should get a successfully installed pop-up message as soon as the process completes&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Enter the following commands, one at a time, in the &lt;strong&gt;Execute python scripts&lt;/strong&gt; field and press the &lt;strong&gt;Run Script&lt;/strong&gt; button. This step is crucial to create database schema and tables, based on your models.py file, in the database you set up earlier.&lt;/p&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;makemigrations&lt;/span&gt;
&lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;migrate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note: If you uploaded your files to &lt;strong&gt;Public_html&lt;/strong&gt; and entered the commands exactly as above, it should work. However, if you uploaded your files to a different directory, you would need to specify the full path to the manage.py file. Additionally, if you have more than one project in the Public_html directory, you may need to specify the path to a specific manage.py file. This tutorial assumes only one manage.py file is available in Public_html directory.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set Your &lt;strong&gt;Environment Variables&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
This step is crucial if you used any environment variables within your app. For instance, you might have used an environment variable as a place holder for the &lt;strong&gt;SECRET_KEY&lt;/strong&gt; variable in your settings.py file.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwj9z14oz2b3o08nqu2ve.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwj9z14oz2b3o08nqu2ve.png" alt="cPanel Environmental Variables section"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This tutorial provides a comprehensive guide to correctly hosting your project in cPanel. Hosting your Django web app with cPanel essentially involves three major steps:&lt;br&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uploading your project .zip extension file&lt;/li&gt;
&lt;li&gt;Configuring a database&lt;/li&gt;
&lt;li&gt;
Setting up your app
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Deploying your Python project to cPanel could be challenging for beginners. However, in this author's opinion, cPanel has a gentler learning curve compared to other hosting sites. The above guidelines have expounded on the steps above in detail, including providing information on potential pitfalls. Happy hosting!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>django</category>
      <category>cpanel</category>
      <category>mysql</category>
    </item>
    <item>
      <title>Need to Create a Simple Content Aggregator Website using Django? It's a Minimalist Task</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Thu, 04 Jan 2024 03:07:03 +0000</pubDate>
      <link>https://dev.to/odhiambo/need-to-create-a-simple-content-aggregator-website-using-django-its-a-minimalist-task-l2d</link>
      <guid>https://dev.to/odhiambo/need-to-create-a-simple-content-aggregator-website-using-django-its-a-minimalist-task-l2d</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The web contains a lot of content housed in thousands of websites. Different websites contain related content that one website could efficiently host. The nature of distribution of related content on the web forces users to visit different websites for the same information, which could be hectic and time consuming.&lt;/p&gt;

&lt;p&gt;Several content gathering websites exist to enhance user experience. The sites gather information about various topics in one place so that users do not have to scour the web unnecessarily. This article discusses the engineering behind these content aggregator sites. Developers interested in designing content aggregators may find this article helpful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An understanding of Python basics and the Django framework&lt;/li&gt;
&lt;li&gt;Basic knowledge of RSS feeds&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  RSS feeds
&lt;/h2&gt;

&lt;p&gt;Content aggregator sites collect content from &lt;a href="https://rss.com/blog/how-do-rss-feeds-work/"&gt;&lt;strong&gt;Really Simple Syndication (RSS)&lt;/strong&gt;&lt;/a&gt; feeds. RSS feeds are XML files that many websites, which host frequently changing content, create to track content records. An RSS feed typically contains important information about a piece of web content. For instance for a news content, the feed could contain the following information: &lt;a id="meta"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Name of the publishing site&lt;/li&gt;
&lt;li&gt;Title of content&lt;/li&gt;
&lt;li&gt;Publication date&lt;/li&gt;
&lt;li&gt;URL of the content&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Python's feedparser library
&lt;/h2&gt;

&lt;p&gt;A Python library known as feedparser is a useful tool that a content aggregator site developer can use to parse and extract content from RSS feeds. However, before using the library, the developer should decide the kind of data to retrieve from RSS feeds. If one is building a news aggregator website, one would perhaps need to retrieve the metadata listed in the previous section for each news item at the minimum. &lt;/p&gt;

&lt;p&gt;To use the feedparser library to parse RSS feeds:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a Python virtual environment and install Django&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Install the feedparser library using pip&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;pip&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;feedparser&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a Django project and app&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a database table in models.py file with the necessary columns. For instance, for a news website, a database table could be something like this:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt; &lt;span class="c1"&gt;# app/models.py
&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.db&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;

 &lt;span class="c1"&gt;# Create your models here
&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;News&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
     &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="n"&gt;pub_date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DateTimeField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
     &lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;URLField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
     &lt;span class="n"&gt;site_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

     &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__str__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run Django migrations to include your table in the database&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt; &lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;makemigrations&lt;/span&gt;
 &lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;migrate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To test the working of the feedparser, one can manually retrieve content from an RSS feed and display it in the terminal. To achieve this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start a terminal session inside your virtual environment&lt;/li&gt;
&lt;li&gt;Import the feedparser library&lt;/li&gt;
&lt;li&gt;Use the parse function to extract data from an RSS feed&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In code,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;venv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;shell&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;feedparser&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;feedparser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;some_rss_feed_url&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;site_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;site_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make the project more robust and efficient, one can automate the data retrieval process. Additionally, it is paramount to implement a storage mechanism. For this purpose, it may be necessary to create a custom Django command. &lt;/p&gt;

&lt;h2&gt;
  
  
  A Custom Django Command
&lt;/h2&gt;

&lt;p&gt;The file &lt;strong&gt;manage.py&lt;/strong&gt; runs all commands within a Django project. It is possible to create a custom command in Django to utilize the manage.py file. To achieve this, the command should be created inside the project app directory in a subdirectory called &lt;strong&gt;management/commands&lt;/strong&gt;. When the developer runs the manage.py file, it checks for custom commands in the directory and executes them. Furthermore, it is worth noting that all custom commands inherit from the &lt;strong&gt;BaseCommand&lt;/strong&gt; object. Therefore, our command file should import BaseCommand.&lt;/p&gt;

&lt;p&gt;Inside the app/management/commands directory:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a file with a suitable name that will contain the command code to execute&lt;/li&gt;
&lt;li&gt; Import the necessary dependencies:

&lt;ul&gt;
&lt;li&gt;BaseCommand object&lt;/li&gt;
&lt;li&gt;Feedparser library&lt;/li&gt;
&lt;li&gt;Your model&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Create a command class that inherits from BaseCommand&lt;/li&gt;
&lt;li&gt;Create a handle method that does two things:

&lt;ol&gt;
&lt;li&gt;Retrieves data from a feed&lt;/li&gt;
&lt;li&gt;Stores the data in a database as illustrated below
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# app/management/commands/news.py
&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.core.management.base&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BaseCommand&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;feedparser&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;app.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;News&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Command&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseCommand&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# parses RSS feeds
&lt;/span&gt;    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;**&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;feed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;feedparser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;some_website_rss_feed_url&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;feed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;

        &lt;span class="c1"&gt;# Stores parsed data in db
&lt;/span&gt;        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;feed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;News&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
                &lt;span class="n"&gt;news_item&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;News&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;pub_date&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;published&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
                    &lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;site_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
              &lt;span class="p"&gt;)&lt;/span&gt;
              &lt;span class="n"&gt;news_item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this set-up in place, when you execute the above command, the app will retrieve data from the specified RSS feed URL and store it in the database. One can access the model from the admin panel to inspect the data. To hook up URLs from several feeds into the project, &lt;a href="https://realpython.com/build-a-content-aggregator-python/#demo-what-youll-build"&gt;follow this tutorial&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The tutorial above discusses the main building blocks necessary to create a simple content aggregator website using Django. The main tools a developer needs are RSS feed URLs, clear understanding of the kind of data to retrieve from the RSS feeds, the feeparser library and a custom Django command. With these tools, the developer can conceptualize and construct a suitable data model, parse RSS feeds, and implement a command that handles how the data is stored in the project's database.&lt;/p&gt;

</description>
      <category>django</category>
      <category>webdev</category>
      <category>technical</category>
      <category>python</category>
    </item>
    <item>
      <title>Servers, Application Servers and Web Servers. What are the distinctions?</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Sat, 18 Nov 2023 06:02:57 +0000</pubDate>
      <link>https://dev.to/odhiambo/servers-application-servers-and-web-servers-what-are-the-distinctions-5fm5</link>
      <guid>https://dev.to/odhiambo/servers-application-servers-and-web-servers-what-are-the-distinctions-5fm5</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Understanding the differences between a server, a web server, and an application server can be challenging. In many cases, tech enthusiasts use the terms interchangeably. This article, targeted at newbies to the concept of servers, purposes to clarify the differences.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Server
&lt;/h2&gt;

&lt;p&gt;A server is broad term that computer professionals use to refer to a device that avails digital resources to other computers (clients) over a network. A server device could be a physical computer or a &lt;a href="https://www.vmware.com/topics/glossary/content/virtual-machine.html"&gt;virtual machine&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Several types of severs exist. Web servers avails web pages to client computers. Application servers execute set functionalities for the client. Additionally, mail servers facilitate sending and receiving emails between clients. Furthermore, system engineers routinely configure servers to facilitate file storage and transfer, in which case the server becomes a File Transfer Protocol server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Web Servers
&lt;/h2&gt;

&lt;p&gt;A web server is a software device that serves web pages. This server type understands the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview"&gt;Hyper Text Transfer Protocol&lt;/a&gt; communication; the server can communicate with client computers requesting web pages. A web server delivers static resources to clients.  Static resources are digital content that are served as is, without any modification. Examples of popular web servers in the market include Apache and Nginx.&lt;/p&gt;

&lt;h2&gt;
  
  
  Application Servers
&lt;/h2&gt;

&lt;p&gt;An application server is a software device that executes business logic. Business logic, in this case, refers to a website's core functionality. Consider an ecommerce website where customers can browse product listings. An application server runs the logic that involves the retrieval of product types from a database and display on the frontend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Web Versus Application Servers
&lt;/h2&gt;

&lt;p&gt;Web and application servers have one fundamental difference; web servers process static requests while application servers handle dynamic requests. As mentioned, servers deliver static resources to the client unchanged. Comparatively, when a client requests a dynamic resource—could be a list of the available products in an ecommerce website—an application server performs modifications on the existing resources to deliver the right content to the client.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To recap, a server is a physical computer or software component that hosts several software capable of serving web pages, mails, files and running business logic. A web server is a software device that servers web pages. An application server runs business logic. Furthermore, the basic difference between a web and application server is in the nature of resources they serve.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>servers</category>
    </item>
    <item>
      <title>How to Initiate the Development Environment when building a Django App</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Mon, 13 Nov 2023 10:15:56 +0000</pubDate>
      <link>https://dev.to/odhiambo/how-to-initiate-the-development-environment-when-building-a-django-app-153l</link>
      <guid>https://dev.to/odhiambo/how-to-initiate-the-development-environment-when-building-a-django-app-153l</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Django is a versatile web development framework. Many top companies, including Instagram, FireFox and Spotify use the framework to develop their websites. Django is useful when developing complex, large-scale web apps. However, many developers use it to create simple web apps. This tutorial guides new Django developers on how to set up a Django development environment to create their web apps. The tutorial will guide on how to set up a Django app in both Windows and Linux environments.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;: Ensure you have a Python version 3.4+ installed on your system.&lt;/p&gt;

&lt;p&gt;Setting up a Django application involves two major steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Setting up a &lt;a href="https://www.geeksforgeeks.org/python-virtual-environment/"&gt;Python virtual environment&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Starting a Django project&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Jump to Windows set-up&lt;br&gt;
Jump to Linux set-up&lt;/p&gt;

&lt;p&gt;&lt;a id="windows"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Windows set-up
&lt;/h2&gt;

&lt;p&gt;This set-up assumes you are running Windows 10&lt;/p&gt;

&lt;p&gt;Follow these steps for Windows set up:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a project folder in your windows file system.&lt;/p&gt;

&lt;p&gt;For example, create the folder named &lt;strong&gt;django_app&lt;/strong&gt; in your &lt;strong&gt;desktop directory&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Start a Windows terminal&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Press &lt;code&gt;Win + R&lt;/code&gt; to open a dialog box&lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;cmd&lt;/code&gt; then press &lt;code&gt;enter&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Navigate to the project folder&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd Desktop\django_app&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a virtual environment&lt;br&gt;&lt;br&gt;
Run &lt;code&gt;python -m venv my_env&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt;: &lt;strong&gt;my_env&lt;/strong&gt; is a custom name you give to your virtual environment&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Activate the virtual environment&lt;br&gt;&lt;br&gt;
Run &lt;code&gt;.\my_env\Scripts\activate&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Install Django using pip&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install django&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start a Django project&lt;br&gt;&lt;br&gt;
Run &lt;code&gt;django-admin startproject django_app .&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt;: &lt;strong&gt;django_app&lt;/strong&gt; is a custom name you give to your project. &lt;br&gt;&lt;br&gt;
Do not forget the dot at the end of the command or you may run into some configurations problems when deploying your app.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Change into the Django project directory&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd django_app&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create your Django application&lt;br&gt;&lt;br&gt;
Run &lt;code&gt;python manage.py startapp my_first_app&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt;: &lt;strong&gt;my_first_app&lt;/strong&gt; is a custom name of the specific Django application you are creating&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Add your application in the project's &lt;strong&gt;settings.py&lt;/strong&gt; file&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Locate a file named settings.py within your project folder.
&lt;strong&gt;Hint&lt;/strong&gt;: You can locate it manually within your project folder. It should be in a subdirectory with a similar name to your project.&lt;/li&gt;
&lt;li&gt;Open the settings.py file using a code editor&lt;/li&gt;
&lt;li&gt;Locate a section called &lt;strong&gt;INSTALLED_APPS&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Add the name of your Django application at the end&lt;/li&gt;
&lt;li&gt;Save and close the settings.py file&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Create your app's database schema&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Return to your terminal&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;python manage.py migrate&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: It is necessary to run this operation to create a database that the app can work with&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start a development server&lt;br&gt;&lt;br&gt;
In your terminal, with the virtual environment still active, run &lt;code&gt;python manage.py runserver&lt;/code&gt;&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt;: The operation will start a local development server on port 8000&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;View your project in a browser&lt;br&gt;&lt;br&gt;
Open a browser and enter the URL &lt;strong&gt;&lt;a href="http://127.0.0.1:8000/"&gt;&lt;/a&gt;&lt;a href="http://127.0.0.1:8000/"&gt;http://127.0.0.1:8000/&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a id="linux"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Linux set-up
&lt;/h2&gt;

&lt;p&gt;This set up assumes you are running an ubuntu version 20.04+&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open an ubuntu shell on your system&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a project directory and switch into the directory&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;mkdir django-project &amp;amp;&amp;amp; cd django-project&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a virtual environment&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python3 -m venv my_env&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Activate the virtual environment&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;source my_env/bin/activate&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Install Django using pip&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install django&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;To install a specific Django version&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install django==3.2.21&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Start a Django project&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;django-admin startproject django_project&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Switch into the Django project directory&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cd django_project&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Start a Django application&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python3 manage.py startapp django_app&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Add the new application to your settings.py file&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Locate a file, settings.py within your shell&lt;/li&gt;
&lt;li&gt;Open the file using a code editor&lt;/li&gt;
&lt;li&gt;Locate a section named &lt;strong&gt;INSTALLED_APPS&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;'django_app'&lt;/code&gt;at the end of installed apps list&lt;/li&gt;
&lt;li&gt;Save and close the settings.py file&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Create a database&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python3 manage.py migrate&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;Start a development server on port 8000&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;python3 manage.py runserver&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;View your project in a browser&lt;br&gt;&lt;br&gt;
Run &lt;strong&gt;&lt;a href="http://127.0.0.1:8000/"&gt;&lt;/a&gt;&lt;a href="http://127.0.0.1:8000/"&gt;http://127.0.0.1:8000/&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By following either steps depending on your operating system, you will have created a basic Django web application. Note that this is only a basic application, without any features. You need to build on the basic application by customizing it further to suit your project's needs or client's specifications&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>django</category>
    </item>
    <item>
      <title>What Happens When you Type google.com in your Browser and Press Enter?</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Fri, 10 Nov 2023 15:42:03 +0000</pubDate>
      <link>https://dev.to/odhiambo/what-happens-when-you-type-googlecom-in-your-browser-and-press-enter-2j8a</link>
      <guid>https://dev.to/odhiambo/what-happens-when-you-type-googlecom-in-your-browser-and-press-enter-2j8a</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Ever wondered what goes on “under the hood” when you type ‘google.com’, or any other phrase, into your browser’s address bar and press enter? Many people not familiar with web development are curious about the inner workings of a website. By reading this blog, readers from a non-technical background will gain a deeper understanding of the technologies that power their popular websites.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.tutorialspoint.com/what-is-web-architecture"&gt;Website architecture&lt;/a&gt; (or web architecture) refers to the organization and structure of a website. Readers need to perceive the basic structure of a website to facilitate understanding. The web architecture consists of several components:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.simplilearn.com/what-is-client-server-architecture-article#:~:text=The%20client%2Dserver%20architecture%20refers,model%20or%20client%20server%20network."&gt;Client-Server Model&lt;/a&gt;: Is the fundamental structure of a website. A typical website assumes a client-server model. In this model, clients (web browsers) request resources from servers. Servers receive the client’s &lt;a href="https://en.wikipedia.org/wiki/HTTP"&gt;Hyper Text Transfer Protocol&lt;/a&gt; (HTTP) requests, process them and send specified resources back to the client, or perform an action.&lt;/p&gt;

&lt;p&gt;Front-end: Is the user facing end of the website, for example a Chrome browser’s window.&lt;/p&gt;

&lt;p&gt;Backend: Is a set of logic (code) that runs the website away from the front-end. The backend code processes requests coming from the front-end.&lt;/p&gt;

&lt;p&gt;Database: Is a component that stores a website application’s data, such as user credentials.&lt;/p&gt;

&lt;p&gt;Understanding the basic parts of a website creates a vivid visualization. A good understanding will help you follow along as this blog discusses the processes that run in the individual components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Typing  ‘google.com’ in your browser
&lt;/h2&gt;

&lt;p&gt;When you type ‘google.com’ in your browser, you are essentially requesting a resource from one of several thousand Google servers. Clients can request several &lt;a href="https://en.wikipedia.org/wiki/Web_resource"&gt;types of resources&lt;/a&gt; from Google servers. The resource ‘google.com’ is a &lt;a href="https://en.wikipedia.org/wiki/Static_web_page"&gt;static content&lt;/a&gt;, which is Google’s search engine main page.&lt;/p&gt;

&lt;p&gt;The phrase ‘google.com’ is a domain name. A domain name is a human-readable and memorable address that a browser uses to identify and locate a server on the &lt;a href="https://en.wikipedia.org/wiki/World_Wide_Web"&gt;World Wide Web&lt;/a&gt;. Technically, a browser identifies a server by an &lt;a href="https://dev.toInternet%20Protocol"&gt;Internet Protocol&lt;/a&gt; (IP) address and not a domain name. Therefore, when you type a domain name and press enter, the browser performs a &lt;a href="https://www.cloudflare.com/learning/dns/what-is-dns/"&gt;Domain Name System&lt;/a&gt; (DNS) lookup. This process converts the wordy domain name into an IP address. Once the browser locates a server with the IP address specified in the address bar, the browser establishes a connection.&lt;/p&gt;

&lt;p&gt;Manufacturers routinely configure client computers and servers with firewalls. Firewalls are software or hardware components that scrutinize website traffic to determine malicious content. Firewalls can allow or restrict outgoing requests and incoming server responses. Assuming both your web browser and the Google server contain non-restrictive firewall configurations for your type of request, they will establish a connection once the browser locates the server.&lt;/p&gt;

&lt;p&gt;Computers need a communication method to facilitate the exchange of information. The Internet Assigned Numbers Authority (IANA) establishes and maintains a suite of &lt;a href="https://www.techtarget.com/searchnetworking/definition/protocol"&gt;computer communication protocols&lt;/a&gt;. The IANA protocols facilitate communication between computers of different architectures and operating systems. Your web browser communicates with a Google server using a communication protocol known as &lt;a href="https://www.techtarget.com/searchnetworking/definition/TCP-IP"&gt;Transmission Control Protocol/Internet Protocol&lt;/a&gt; (TCP/IP). This communication standard governs the way data is transmitted between the two computers over the internet.&lt;/p&gt;

&lt;p&gt;HTTP requests are human-readable, which poses a data security risk. Computer communications need to be discreet to prevent malicious actors from intercepting and deciphering communication in-between. For security purposes, browser and server manufacturers configure their software components with a security feature that allows the encryption of the data transmitted back and forth. This feature, known as the &lt;a href="https://www.cloudflare.com/learning/ssl/what-is-ssl/"&gt;Secure Socket Layer&lt;/a&gt; (SSL), encrypts your HTTP request in transit and only decrypts it at the destination server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://seo.ai/blog/how-many-people-use-google#:~:text=Google%20processes%20approximately%2099%2C000%20search,and%20four%20searches%20each%20day."&gt;Billions of people use the Google Search Engine daily&lt;/a&gt;, which can overload the servers. Companies that process such huge web traffic use software or hardware devices known as &lt;a href="https://www.nginx.com/resources/glossary/load-balancing/"&gt;load balancers&lt;/a&gt; to distribute traffic between the different servers. This distribution solution aims to ease up workload on individual servers. Google engineers configure load balancers with &lt;a href="https://www.nginx.com/resources/glossary/load-balancing/"&gt;distribution algorithms&lt;/a&gt; to route traffic from web browsers to various servers. Therefore, a load balancer will route your ‘google.com’ request to a server that can efficiently handle the request, based on factors such as server load and geographical location.&lt;/p&gt;

&lt;p&gt;Once your request hits a server, the server will start processing it. A typical server contains within it a &lt;a href="https://www.solarwinds.com/resources/it-glossary/web-server"&gt;web server&lt;/a&gt;, an &lt;a href="https://www.nginx.com/resources/glossary/application-server-vs-web-server/"&gt;application server&lt;/a&gt; and a database. A web server processes static content. The server usually sends a static resource back to the client in an unchanged state. Static content includes HTML, CSS, JavaScript, and images files.&lt;/p&gt;

&lt;p&gt;Servers also host &lt;a href="https://en.wikipedia.org/wiki/Dynamic_web_page"&gt;dynamic content&lt;/a&gt;. A web server forwards dynamic content requests to an application server for further processing. An application server contains the server-side logic—a set of instructions to manage dynamic requests and route requests within the web framework).&lt;/p&gt;

&lt;p&gt;Additionally, servers host databases that store an application’s data. A typical web application could store users’ login credentials in the database. Servers can host several &lt;a href="https://www.dataversity.net/types-of-databases-and-their-uses/"&gt;categories of databases&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;To recap, when you request ‘google.com’ from your web browser, the web browser will conduct a DNS lookup using the domain name to connect to a Google server where the requested resource lives. An SSL feature in your browser will encrypt the request data.  A TCP/IP protocol will then transmit the request data through the internet to a Google server. A load balancer will route your request to a server. A web server will then process the request since you are requesting a static content. The server will return an encrypted response to your web browser. Your browser will then decrypt, render and display the response. Finally, your browser and server will close the connection.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
