<?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: Dev Mehta</title>
    <description>The latest articles on DEV Community by Dev Mehta (@devmehta).</description>
    <link>https://dev.to/devmehta</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%2F503766%2F95cb8675-550f-4b31-bb85-6ceffbcbc226.png</url>
      <title>DEV Community: Dev Mehta</title>
      <link>https://dev.to/devmehta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devmehta"/>
    <language>en</language>
    <item>
      <title>Can you write FizzBuzz?</title>
      <dc:creator>Dev Mehta</dc:creator>
      <pubDate>Fri, 24 Nov 2023 12:59:05 +0000</pubDate>
      <link>https://dev.to/devmehta/can-you-write-fizzbuzz-21f2</link>
      <guid>https://dev.to/devmehta/can-you-write-fizzbuzz-21f2</guid>
      <description>&lt;p&gt;I was incredulous, when I read &lt;a href="https://blog.codinghorror.com/why-cant-programmers-program/" rel="noopener noreferrer"&gt;this post&lt;/a&gt; by &lt;a href="https://blog.codinghorror.com/why-cant-programmers-program/" rel="noopener noreferrer"&gt;Jeff Atwood&lt;/a&gt;. The post is about how Jeff was incredulous, when he read an observation from &lt;a href="http://weblog.raganwald.com/2007/01/dont-overthink-fizzbuzz.html?ref=blog.codinghorror.com" rel="noopener noreferrer"&gt;Reginald Braithwarte&lt;/a&gt;. Okay let's not start calling the post recursively. The post was about the frustration expressed by interviewers who encounter candidates, including those with advanced degrees, unable to perform basic programming tasks.&lt;/p&gt;

&lt;p&gt;It's a fairly old post(old by around 16 years). I stumbled upon it today while surfing over the internet and was incredulous by this.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;After a fair bit of trial and error I've discovered that people who struggle to code don't just struggle on big problems, or even smallish problems (i.e. write a implementation of a linked list). &lt;em&gt;They struggle with tiny problems&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So I set out to develop questions that can identify this kind of developer and came up with a class of questions I call "FizzBuzz Questions" named after a game children often play (or are made to play) in schools in the UK. An example of a Fizz-Buzz question is the following:&lt;/p&gt;

&lt;p&gt;Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".&lt;/p&gt;

&lt;p&gt;Most good programmers should be able to write out on paper a program which does this in a under a couple of minutes. Want to know something scary?  &lt;strong&gt;The majority of comp sci graduates can't. I've also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So after googling the meaning of word incredulous(Yeah, I am definitely adding it to my vocabulary), I thought, no, this can't be possible. I mean who in their right mind would take more than 10-15 minutes to write such a simple program, especially when they proclaim themselves to be "senior" programmers? Heck even bad programmers should be able to come up with a solution fairly quickly.&lt;/p&gt;

&lt;p&gt;Apparently only &lt;a href="https://www.globalnerdy.com/2012/11/15/fizzbuzz-still-works/" rel="noopener noreferrer"&gt;40% candidates passed the FizzBuzz test&lt;/a&gt; when all the candidates had some kind of college - or university level programming education, many had other certifications or degrees. Each candidate could point to a sizeable, completed project on which they worked.&lt;/p&gt;

&lt;p&gt;Now, don't start solving the fizzbuzz problem if you are confident that you can, and could come up with a solution in your mind. It was presented as the lowest level of comprehension required to illustrate basic adequacy. There's no glory in being able to come up with a solution.&lt;/p&gt;

&lt;p&gt;Between reading all these I wondered what might be the reasons people &lt;em&gt;failed&lt;/em&gt; FizzBuzz. I have some guesses from noticing my experiences during my diploma and graduation programs(As of today, I am a sophomore year CS student):&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“Copy and paste, adjust to taste”-style programming:&lt;/strong&gt;  Also known as “Coding by Googling”,  this is when a developer writes applications by searching for code that’s similar to the problem that s/he’s trying to solve, copy-and-pastes it, and then tweaks it as needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Moving away from programming:&lt;/strong&gt; Some of them may have started as developers only to realize they were meant more for the managerial roles, or jobs that require something to do with applications but most of their job involved something outside the realm of writing code.&lt;/p&gt;




&lt;p&gt;This essay is heavily inspired, and to &lt;del&gt;some&lt;/del&gt; most extent copied from, &lt;a href="https://blog.codinghorror.com/" rel="noopener noreferrer"&gt;Jeff Atwood&lt;/a&gt;'s post about &lt;a href="https://blog.codinghorror.com/why-cant-programmers-program/" rel="noopener noreferrer"&gt;why can't programmers program&lt;/a&gt;. Thank you Jeff Atwood for all the amazing codinghorror blogposts. Jeff has been writing since the year I was born, and I do not intend to pass around his work claiming being mine. I have been an avid reader of his posts, and I just wanted to share my experience and thoughts about the said &lt;em&gt;&lt;a href="https://blog.codinghorror.com/why-cant-programmers-program/" rel="noopener noreferrer"&gt;why can't programmers program&lt;/a&gt;&lt;/em&gt; post when I read it.&lt;/p&gt;

&lt;p&gt;References&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.codinghorror.com/why-cant-programmers-program/" rel="noopener noreferrer"&gt;https://blog.codinghorror.com/why-cant-programmers-program/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.globalnerdy.com/2012/11/15/fizzbuzz-still-works/" rel="noopener noreferrer"&gt;https://www.globalnerdy.com/2012/11/15/fizzbuzz-still-works/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.kegel.com/academy/getting-hired.html" rel="noopener noreferrer"&gt;http://www.kegel.com/academy/getting-hired.html&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>How to revert migration in django?</title>
      <dc:creator>Dev Mehta</dc:creator>
      <pubDate>Wed, 29 Mar 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/devmehta/how-to-revert-migration-in-django-3aah</link>
      <guid>https://dev.to/devmehta/how-to-revert-migration-in-django-3aah</guid>
      <description>&lt;p&gt;Imagine you are a developer working on a Django web application for a client. You have been tasked with adding a new feature that requires changes to the database schema. You create a new Django migration using the &lt;code&gt;python manage.py makemigrations&lt;/code&gt; command to make these changes. Then you apply the migration using the &lt;code&gt;python manage.py migrate&lt;/code&gt; command, which updates the database schema.&lt;/p&gt;

&lt;p&gt;After deploying the new feature, you receive feedback from the client that the feature is causing issues and needs to be reverted. You realize that you need to undo the changes made by the migration to restore the previous database schema. Holy hell! I hate when they do this. In this article, I will guide you on reverting migrations so that you can be saved from such horrors.&lt;/p&gt;

&lt;p&gt;In Django, migrations are used to manage changes in your database schema. However, sometimes it's necessary to revert a migration, particularly if you made a mistake or if you want to undo a particular change.&lt;/p&gt;

&lt;p&gt;Here's how you can revert a migration in Django&lt;/p&gt;

&lt;p&gt;First, you need to find the migration that you want to revert. You can do this by running the following command in your terminal&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;This will display a list of all the migrations in your project. Find the migration that you want to revert to and take note of its name.&lt;/p&gt;

&lt;p&gt;Once you have identified the migration to revert, you can use the following command to revert it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py migrate &amp;lt;app_name&amp;gt; &amp;lt;migration_name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;&amp;lt;app_name&amp;gt;&lt;/code&gt; with the name of your app and &lt;code&gt;&amp;lt;migration_name&amp;gt;&lt;/code&gt; with the name of the migration that you want to revert. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python manage.py migrate myapp 0002_previous_migration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will roll back the migration and undo the changes it made to your database schema.&lt;/p&gt;

&lt;p&gt;Well akshually🤓, you don’t even need any of that, you just need the migration number of the migration you want to revert back to&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;That's it! By following these steps, you can easily revert a migration in Django and undo any changes made to your database schema.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>django</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>python</category>
    </item>
    <item>
      <title>Django - CBVs or FBVs?</title>
      <dc:creator>Dev Mehta</dc:creator>
      <pubDate>Tue, 21 Mar 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/devmehta/django-cbvs-or-fbvs-47gd</link>
      <guid>https://dev.to/devmehta/django-cbvs-or-fbvs-47gd</guid>
      <description>&lt;p&gt;Meet Jane. Jane is a Django developer working on various projects. In one of her projects, Jane needed to build complex views which needed to be more extensible and flexible. Jane got introduced to class-based views in Django. At first, like any of us would, Jane was hesitant to rewrite her views to class-based views. But as she delved deeper, she realized that there were many benefits to using class-based views&lt;/p&gt;

&lt;p&gt;To begin with - we use class-based views in Django because we don’t want to end up in the same situation as Jane — nobody would like to rewrite their function-based views to class-based views(you will need to - if your app scales).&lt;/p&gt;

&lt;p&gt;Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Organization of code related to specific HTTP methods (&lt;code&gt;GET&lt;/code&gt;, &lt;code&gt;POST&lt;/code&gt;, etc.) can be addressed by separate methods instead of conditional branching.&lt;/li&gt;
&lt;li&gt;Object oriented techniques such as mixins (multiple inheritance) can be used to factor code into reusable components.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;During the old times of django, there was only a simple method of writing views - Django would provide &lt;code&gt;HttpRequest&lt;/code&gt; to your function and expect an &lt;code&gt;HttpResponse&lt;/code&gt; in return. Then function-based views were introduced to ease view development. The problem with function-based generic views is that while they covered the simple cases well, there was no way to extend or customize them beyond some configuration options, limiting their usefulness in many real-world applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  But…..
&lt;/h2&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="nn"&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="nn"&gt;django.shortcuts&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;.forms&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;MyForm&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;create_post&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="k"&gt;if&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;method&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyForm&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;POST&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;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_valid&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
            &lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save&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;HttpResponseRedirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/success/"&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&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="s"&gt;"post.html"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"errors"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;errors&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;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;MyForm&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&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="s"&gt;"post.html"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;"form"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;form&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

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

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;But this view works really work for me, why will I ever use a class-based view if it just does the same work, and if its hard to grasp as a beginner.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You may think like this, but what if you could do all that with 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="nn"&gt;django.views.generic.edit&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;CreateView&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Post&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CreatePost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CreateView&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;Post&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="s"&gt;'title'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'content'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This is just a simple example of how class-based views are more concise and readable, but that’s not it. There are more generic views that you can use like&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CreateView,&lt;/li&gt;
&lt;li&gt;UpdateView,&lt;/li&gt;
&lt;li&gt;DeleteView,&lt;/li&gt;
&lt;li&gt;ListView,&lt;/li&gt;
&lt;li&gt;DetailView&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Additionally, by organizing your code into methods that correspond to different parts of the request-response cycle, you can make it easier for other developers to understand what's going on. And by using mixins and generic views, you can avoid writing repetitive code that can be hard to read and maintain.&lt;/p&gt;

&lt;p&gt;You can also subclass a class and refine methods like &lt;code&gt;get_context_data&lt;/code&gt; for specific cases, and leave the rest as-is. You can't do that with functions.&lt;/p&gt;

&lt;p&gt;For instance, you might need to create a new view that does everything a previous one does, but you need to include extra variables in the context. Subclass the original view and override the &lt;code&gt;get_context_data&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Also, separating the steps needed to render the template into separate methods promotes clearer code - the less done in a method, the easier it is to understand. With regular view functions, it's all dumped into one processing unit.&lt;/p&gt;

&lt;p&gt;You might even not need to write a view for simpler cases, you can use them directly 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;path('author/&amp;lt;int:pk&amp;gt;/', DetailView.as_view(model=Author)),

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

&lt;/div&gt;



&lt;p&gt;You can &lt;a href="https://docs.djangoproject.com/en/4.1/topics/class-based-views/intro/"&gt;start learning&lt;/a&gt; about Django class-based views from here. Once you have read the documentation or at least used CBVs a few times in your app, I would recommend using &lt;a href="https://ccbv.co.uk/"&gt;https://ccbv.co.uk/&lt;/a&gt; - It is the ultimate resource for Class Based Views, giving you an easy way to view CBV code&lt;/p&gt;

&lt;h2&gt;
  
  
  Famous final words….
&lt;/h2&gt;

&lt;p&gt;In the world of Django development, while function-based views might seem like the obvious choice, once you put on those class-based view glasses, you'll realize there's a whole new world of possibilities waiting for you.&lt;/p&gt;

&lt;p&gt;With the power of mixins and the elegance of generic views, Django class-based views can help you build complex, extensible, and scalable views with ease. So don't be like Jane who's hesitant to make the switch, and start using class-based views today!&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>django</category>
      <category>webdev</category>
      <category>programming</category>
      <category>python</category>
    </item>
    <item>
      <title>Best Developer Blogs to Follow in 2023</title>
      <dc:creator>Dev Mehta</dc:creator>
      <pubDate>Mon, 13 Mar 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/devmehta/best-developer-blogs-to-follow-in-2023-3l83</link>
      <guid>https://dev.to/devmehta/best-developer-blogs-to-follow-in-2023-3l83</guid>
      <description>&lt;p&gt;If you want to stay up-to-date as a developer, you need to immerse yourself in the coding culture. The programming field is so vast, and there is so much information to absorb that you’ll never get on top if you don’t keep involved enough in all the news and updates.&lt;/p&gt;

&lt;p&gt;This article mainly focuses on bringing out the critical blog resources across the internet that deliver the top articles for programmers. With no further delay, we’d like to walk you through our curated list of the best developer blogs.&lt;/p&gt;

&lt;p&gt;Please keep in mind that, this is not a top 10 list but a list of 10 blogs that I follow every day, to keep myself up with the constantly changing world of web development, and programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Daily WTF - &lt;a href="https://thedailywtf.com/"&gt;thedailywtf.com&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Well, technically this is different from your how-to guide for developing good software, but rather a collection of stories of disastrous development, from project management going spectacularly bad to inexplicable coding choices.&lt;/p&gt;

&lt;p&gt;Some articles on the website are written by contributors who share their own experiences and frustrations with coding, while others are fictional stories designed to poke fun at common industry practices and stereotypes.&lt;/p&gt;

&lt;p&gt;While the website may not be appropriate for all audiences, it does provide a lighthearted look at the challenges and absurdities of software development to me personally.&lt;/p&gt;

&lt;p&gt;The storytelling will keep you hooked and you will learn “what not to do” in a humorous &amp;amp; engaging manner.&lt;/p&gt;

&lt;h2&gt;
  
  
  Substack Newsletters
&lt;/h2&gt;

&lt;p&gt;Again this one is not specifically a blog but its one of my personal favourite platforms to read content.&lt;/p&gt;

&lt;p&gt;Substack — allows writers to send digital newsletters directly to their readers and monetize their work by putting it behind a paywall. One of the benefits of such a mechanism, for us readers is that due to this paywall, most of the writers produce quality work - why? because only good content sells.&lt;/p&gt;

&lt;p&gt;Here are my favourite newsletters on substack that you could give a try(Your mileage may vary):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ByteByteGo - &lt;a href="https://blog.bytebytego.com/"&gt;https://blog.bytebytego.com/&lt;/a&gt; - My go-to system design learning resource&lt;/li&gt;
&lt;li&gt;The Pragmatic Enigneer - &lt;a href="https://newsletter.pragmaticengineer.com/"&gt;https://newsletter.pragmaticengineer.com/&lt;/a&gt; - Talks about big tech and high-growth startups, from inside&lt;/li&gt;
&lt;li&gt;Not Boring by Packy McCormick - &lt;a href="https://www.notboring.co/"&gt;https://www.notboring.co/&lt;/a&gt; - Talks about tech, web3, optimism and strategy but not in a boring way&lt;/li&gt;
&lt;li&gt;Software Design, Tidy First - &lt;a href="https://tidyfirst.substack.com/"&gt;https://tidyfirst.substack.com/&lt;/a&gt; - Book-scoped topics as articles.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  HackerNoon - &lt;a href="https://hackernoon.com/"&gt;hackernoon.com&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Hacker Noon has a very wide range of activities. It is a portal that tackles issues related to technology, software development, cryptocurrencies, and blockchain technology. You can find mostly technical articles that provide really interesting insights.&lt;/p&gt;

&lt;p&gt;This platform is quite popular among technical specialists and tech fans, as it is an independent resource dedicated to sharing any kind of technology news stories and expert opinions. I have been publishing on &lt;a href="https://hackernoon.com/u/simplifiedweb"&gt;hackernoon&lt;/a&gt; recently, and it's good for both content creators and readers - 55% of their articles are rejected if they don’t match their quality assurance.&lt;/p&gt;

&lt;h2&gt;
  
  
  David Walsh's Blog - &lt;a href="https://davidwalsh.name/"&gt;davidwalsh.name&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;It is of the most well-known personal blogs run by a single developer. He is a senior web developer and front-end engineer, providing Internet users with the coolest programming tutorials.&lt;/p&gt;

&lt;p&gt;If you’re in search of tutorials to improve your coding and web development skills, this blog is the best place to start with. The great thing about using this blog is that you will see real examples of the programming world. You can also find free trial versions of the software in his blog.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://blog.codinghorror.com/"&gt;CodingHorror&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;This blog is heartily enjoyed by a lot of readers. This blog is included in this list since it’s a frank, humorous blog that discusses the challenges and ironies of software creation, its ecosystem, and how it interacts (or doesn’t) with the business world.&lt;/p&gt;

&lt;p&gt;Since 2004, it has been around us. The blog is written by Jeff Atwood, the creator of Stack Overflow. Jeff has a brilliant way of looking at coding, and he can give you a stimulating view of the tech world when you need some inspiration.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://TestDriven.io"&gt;TestDriven.io&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Blog backed up by Michael Hermon - The Co-Founder/Author of &lt;a href="http://realpython.com"&gt;realpython.com&lt;/a&gt;. As the name suggests his tutorials on test-driven development, django and python are unmatched. He is popular among the Django community for being one of the early adopters of Django, and a source of inspiration for bloggers like me.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/"&gt;Mozilla Developer Network Blogs&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;The Mozilla Developer Network Blog is a programming blog that covers topics such as web development, browser technology, and JavaScript. The blog is written by a team of experts who provide valuable insights into the latest trends and updates in web development. The blog also features tutorials and resources to help programmers improve their skills.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://mcfunley.com/"&gt;Dan McKinley’s Blog&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;A lot of great material on how engineering companies should be run. He has a lot of ideas that sound like common sense, e.g., &lt;a href="http://mcfunley.com/choose-boring-technology"&gt;choose boring technology&lt;/a&gt;, and &lt;a href="https://blog.skyliner.io/ship-small-diffs-741308bec0d1?gi=9b622ffaf39f"&gt;ship small diffs&lt;/a&gt; until you realize that it's actually uncommon to find opinions that are so sensible.&lt;/p&gt;

&lt;p&gt;Mostly distilled wisdom (as opposed to, say, detailed explanations of code).&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="http://blogs.whitman.edu/countingfromzero/"&gt;Janet Davis’ Blog&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Another “not a blog”. Damn it. Yes its not a blog but an interesting story read on how a professor moved from Grinnel to Whitman and &lt;a href="http://blogs.whitman.edu/countingfromzero/2015/07/22/how-i-got-here/"&gt;started a CS program from scratch&lt;/a&gt;. The archives are great reading if you're interested in how organizations form or CS education&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;a href="https://rakyll.org/"&gt;Jaana Dogan’s Blog&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://rakyll.org/"&gt;rakyll.org&lt;/a&gt; has posts on Go, some of which are quite in-depth, e.g., &lt;a href="https://rakyll.org/generics-proposal/"&gt;this set of notes on the Go generics proposal&lt;/a&gt; and &lt;a href="https://medium.com/@rakyll"&gt;Jaana's medium blog&lt;/a&gt; has some posts on Go as well as posts on various topics in distributed systems.&lt;/p&gt;

&lt;p&gt;Also, &lt;a href="https://twitter.com/rakyll"&gt;Jaana's Twitter&lt;/a&gt; has what I think of as "intellectually honest critiques of the industry", which I think is unusual for critiques of the industry on Twitter. It's more typical to see people scoring points at the expense of nuance or even being vaguely in the vicinity of correctness, which is why I think it's worth calling out these honest critiques.&lt;/p&gt;




&lt;p&gt;That’s it for today. I hope you enjoyed reading about my personal list of top developer blogs to follow in 2023. Remember, there are many other great blogs out there, so don't be afraid to explore and find what works best for you. Keep learning and stay up-to-date with the latest developments in the tech industry!&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>resources</category>
      <category>blogs</category>
      <category>programming</category>
    </item>
    <item>
      <title>Are your webapps scalable?</title>
      <dc:creator>Dev Mehta</dc:creator>
      <pubDate>Wed, 08 Mar 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/devmehta/are-your-webapps-scalable-5be0</link>
      <guid>https://dev.to/devmehta/are-your-webapps-scalable-5be0</guid>
      <description>&lt;p&gt;...Django is a widely used Python web framework that allows developers to build scalable web applications quickly and efficiently. One of the most important factors to consider when developing a website is scalability - the ability of a website to handle increasing traffic and data without sacrificing performance.&lt;/p&gt;

&lt;p&gt;The scalability of a website is essential for businesses that are growing or expecting increased traffic from marketing campaigns or seasonal spikes in demand.&lt;/p&gt;

&lt;p&gt;In this blog post, we will explore how scalable websites built-in Django are, what factors can affect their scalability, and how to build scalable websites using Django.&lt;/p&gt;

&lt;h3&gt;
  
  
  What does “scaling” mean in the context of django web apps?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Can progressively scale from a low amount of users to a high amount of users.&lt;/li&gt;
&lt;li&gt;Have modular components that are decoupled and can be replaced if they are determined to be bottlenecks of the application.&lt;/li&gt;
&lt;li&gt;The infrastructure that you use is scalable - more on this later.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  A little bit of background
&lt;/h3&gt;

&lt;p&gt;Even though the conventionally accepted norm is that - “Python is slow”. This argument is &lt;a href="https://hackernoon.com/python-is-slow-and-other-myths-of-a-dying-era"&gt;slowly dying&lt;/a&gt;. Django is/was used by Disqus, Pinterest, Instagram and many other apps. They surely made changes to the framework and tweaked it according to their use cases, but this gives us enough confidence that, if we keep things simple &amp;amp; logical we should be able to do it too.&lt;/p&gt;

&lt;p&gt;Also, I would like to add that when I am talking about scaling django apps, I am not talking about “reduced running time with code optimization”. For that, you can read &lt;a href="https://dev.to/devmehta/unlock-the-full-potential-of-your-django-app-with-these-performance-optimization-techniques-1ig6-temp-slug-1703814"&gt;this post&lt;/a&gt;. I want to focus on scaling the infrastructure that the code is running on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Django's Scalability
&lt;/h3&gt;

&lt;p&gt;Django is designed to be scalable, and it has built-in features that allow developers to create websites that can handle a large amount of traffic and data. One of the most important features of Django is its ORM (Object-Relational Mapping) system. It provides a powerful and flexible way to interact with databases, which is essential for the scalability of a website.&lt;/p&gt;

&lt;p&gt;Django also supports caching and load balancing, which helps to reduce the load on the server and ensures that the website performs well under heavy traffic.&lt;/p&gt;

&lt;p&gt;Another factor that makes Django scalable is its modularity. Django allows developers to break down their applications into smaller, reusable components, which makes it easier to add new features and scale the website as needed.&lt;/p&gt;

&lt;p&gt;Django's built-in support for third-party packages also makes it easy to integrate additional functionality into the website without having to write everything from scratch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scalable Architecture
&lt;/h2&gt;

&lt;p&gt;You must have heard these phrases “vertical scaling” &amp;amp; “horizontal scaling”. Scaling vertically simply means adding more resources(RAM, CPU) in a monolithic manner &amp;amp; hope that the application would be able to handle more requests.&lt;/p&gt;

&lt;p&gt;This may work well initially, but at last, you would want to scale horizontally - meaning spawning more machines that serve your application - or better divide your app into micro-services so that each node on the network only serves a specific part of your application.&lt;/p&gt;

&lt;p&gt;Splitting application to individual services helps you scale them individually as your load increases. You can then, work on optimizations at a service level.(Note: Be aware of the extra communication costs among your services).&lt;/p&gt;

&lt;p&gt;You can look at &lt;a href="https://blog.disqus.com/scaling-django-to-8-billion-page-views"&gt;how disqus scaled django to 8 billion page views&lt;/a&gt; handling 45k requests per second.&lt;/p&gt;

&lt;h3&gt;
  
  
  A note on load balancing…
&lt;/h3&gt;

&lt;p&gt;The features required to scale our application horizontally are generally features that allow our application to be stateless. Stateless applications mean that they do not keep any state. They don’t store any data, images, sessions or files within them. They use third-party services for all states, this includes things such as Databases, Memory Cache, Cloud Storage etc.&lt;/p&gt;

&lt;p&gt;Django is great when it comes to this, and it definitely gives us the tools needed for us to run our application completely stateless:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can replace the default SQLite database (which stores data locally) to instead store data on a database that is running on another server instance.&lt;/li&gt;
&lt;li&gt;It supports custom File Storage Backends that allow you to store files on Amazon S3, DigitalOcean Spaces, Azure Blob Storage or any other cloud storage.&lt;/li&gt;
&lt;li&gt;You can customize Sessions to be stored in a database or Key-Value Store (such as Redis) instead of on the file system.&lt;/li&gt;
&lt;li&gt;You can use Cache Backends such as Memcache or Redis to store any cache on another server instead of the file system.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each one of these features allows us to store our application state &lt;strong&gt;somewhere else&lt;/strong&gt;. The key point is not which technology we choose or where we store our state, as long as it’s not stored locally with our application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Django tools that allow for scaling
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/jschneier/django-storages"&gt;Django Storages&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Memcache and Redis Cache Backends (Part of Django)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;PostgreSQL, MySQL Database Backends (Part of Django)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Redis and Database Session Backends (Part of Django)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of these tools mean that you can be ready to set up your application for statelessness and scale out of the box. If you require support for other services that are not supported by these tools, it is important to note that Django is pluggable and allows the programmer to replace most pieces with their own “Backend” class.&lt;/p&gt;

&lt;p&gt;This means that you can often replace the existing built-in features with a custom backend for the service you want to add support for. I recently did this to &lt;a href="https://docs.djangoproject.com/en/4.1/howto/custom-file-storage/"&gt;store files&lt;/a&gt; on Digitalocean and it was very simple and straightforward.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connections and Connection Pooling
&lt;/h2&gt;

&lt;p&gt;If you have not used Django extensively, it is easy to miss out on the &lt;code&gt;CONN_MAX_AGE&lt;/code&gt; param in &lt;code&gt;settings.py&lt;/code&gt; that defines the maximum lifetime of your connection. This is Django’s way of connection pooling.&lt;/p&gt;

&lt;p&gt;By default, Django, closes the connection at the end of each request. Persistent connections avoid overloading the database for each request and the cost ( it takes around ~20ms to make a DB connection ) of creating a connection is also reduced. So developers should consider setting  &lt;code&gt;CONN_MAX_AGE&lt;/code&gt; to &lt;code&gt;None&lt;/code&gt; (unlimited persistent connections) or a suitable value depending on your request volume at the applications’ end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do most of your bottlenecks end up at database level?
&lt;/h2&gt;

&lt;p&gt;If you are not able to scale your backend, then most likely the database is your bottleneck. There are a few quick things that can help you reduce the time to process your DB query and shorten the request/response cycle to improve performance (always think about reducing the time for the request/response cycle – the holy grail).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Indexes – Adding appropriate indexes can speed up your DB (SELECT) queries and reduce the time to respond to users. It is best to look at slow (above ~30 ms) queries and queries that are done often to look at avenues for improvements. But don’t overdo it. More indexes would impact the performance of INSERT and UPDATE and also increase the load on disks to store the indexes.&lt;/li&gt;
&lt;li&gt;Choosing the right database – I’m used to working with Postgres and have seen great results. The community also likes it for its performance, proven capabilities at scale, data integrity and people who keep working on making it better every day.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Write your own code — if needed
&lt;/h2&gt;

&lt;p&gt;Luckily, or unluckily for the world, all types of applications haven’t been built yet. New unique applications with new demands are always popping up and pushing the industry forward. This means that even though we have huge applications such as Instagram, Disqus or Bitbucket using Django in scale, it does not necessarily mean that your unique application with its own needs will not run into some unique problem.&lt;/p&gt;

&lt;p&gt;Django has its ORM which expects you to use a SQL Database such as MySQL or PostgreSQL. If you want to use a Document Database such as MongoDB, Django might not be the right tool for you. This means that the pluggability of Django might be less than something like Flask or Express which simply let your plugin any tools you want.&lt;/p&gt;

&lt;p&gt;With that said, this does not mean that Django is not pluggable at all. There has been a lot of effort invested in making Django pluggable and allow developers to extend it in their own ways.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/pulse/instagram-scales-python-2-billion-daily-users-shrey-batra"&gt;Instagram is a great example&lt;/a&gt;, they used a very custom implementation of Django which allowed them to scale to 100’s millions of users. Django has extensive configuration possibilities with its &lt;code&gt;settings.py&lt;/code&gt; file that allows you as the developer to configure most parts of the framework.&lt;/p&gt;

&lt;p&gt;A few examples of what settings you can set to plug in your own custom solutions into Django:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Authentication Backend&lt;/li&gt;
&lt;li&gt;Session Storage Backend&lt;/li&gt;
&lt;li&gt;Cache Backend&lt;/li&gt;
&lt;li&gt;Database Backend&lt;/li&gt;
&lt;li&gt;Static Files Backend&lt;/li&gt;
&lt;li&gt;Media Files Backend&lt;/li&gt;
&lt;li&gt;Logging Handlers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On top of this, you can also create your own middlewares that allow you to hook into the HTTP Request/Response cycle.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing unused code
&lt;/h2&gt;

&lt;p&gt;Another way to reduce the request/response time is by removing the extra middleware that your app is not benefiting from. Every request that is made to the Django backend passes through this middleware and adds extra time (20-30ms) to the request/response cycle.&lt;/p&gt;

&lt;p&gt;If your app makes API calls to the Django backend then you don’t need to use some of the middlewares that are enabled in Django by default.&lt;/p&gt;

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

&lt;p&gt;Well, that’s it, folks! Hope you learnt a few things today. When developing products for high loads, the key thing is to stay curious, persistent and innovative. Every small change creates an impact as the change influences millions of requests.&lt;/p&gt;

&lt;p&gt;Keep monitoring, keep measuring and keep fixing, that’s the mantra.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>scalability</category>
      <category>webdev</category>
      <category>django</category>
      <category>python</category>
    </item>
    <item>
      <title>Fatigue.js: The revolutionary new framework...</title>
      <dc:creator>Dev Mehta</dc:creator>
      <pubDate>Mon, 06 Mar 2023 10:39:03 +0000</pubDate>
      <link>https://dev.to/devmehta/fatiguejs-the-revolutionary-new-framework-2oe</link>
      <guid>https://dev.to/devmehta/fatiguejs-the-revolutionary-new-framework-2oe</guid>
      <description>&lt;p&gt;Welcome to yet another blog post about the latest and greatest JavaScript framework that's going to revolutionize the world of web development. That's right, we're here to talk about the hottest, trendiest, and most buzzworthy framework that everyone's talking about but nobody actually understands.&lt;/p&gt;

&lt;p&gt;Introducing...&lt;a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"&gt;Fatigue.js&lt;/a&gt;! This new framework coded by me, promises to make web development faster, easier, and more intuitive than ever before.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HIp3RLIn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1iz73hsioo033t62gku.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HIp3RLIn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1iz73hsioo033t62gku.png" alt="JS mimi" width="250" height="219"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You may ask, how? Well, we don't know that yet, but with its cutting-edge features, sleek design, and unparalleled performance, that all of the JavaScript frameworks before &lt;a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"&gt;Fatigue.js&lt;/a&gt; had, it is sure to take the web development world by storm.&lt;/p&gt;

&lt;p&gt;But wait, what does this framework actually do? How is it different from all the other frameworks that have come before it? What problem is it solving, and is that problem even relevant to your project?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7T6S0cpn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6oqfxesf394qmhbkb6n7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7T6S0cpn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6oqfxesf394qmhbkb6n7.png" alt="JS Dev with dog" width="576" height="576"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, to be honest, I am not really sure, are you? No. Nobody really knows what this framework does or how it works, and that's what makes it unique. It is due  to these reasons that Fatigue.js will be the next big thing as it won't stop everyone from jumping on the bandwagon and declaring it the greatest thing since sliced bread.&lt;/p&gt;

&lt;p&gt;Alright, then let's start with how to get started with fatigue. &lt;/p&gt;

&lt;h2&gt;
  
  
  How to Install Fatigue
&lt;/h2&gt;

&lt;p&gt;Installing fatigue is fairly simple, you just need to run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install fatigue &amp;amp;&amp;amp; npm install create-js-fatigue-cli
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add fatigue
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, you can fatigue-cli from your terminal to create fatigue apps. To create your first fatigue app run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fatigue new myproj . 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fatigue, will now install around 420 dependencies to your device, and within about 1 hours and 9 minutes, your project will be production-ready, optimized, blatantly fast, secure and containerized boilerplate ...umm what other keywords did we have for it?,anyhoo, you get the point. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TRuuhh7G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/in02qe338iq5ka4ylq0l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TRuuhh7G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/in02qe338iq5ka4ylq0l.png" alt="JS Dev workflow" width="880" height="563"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the workflow to build a fatigue app
&lt;/h2&gt;

&lt;p&gt;Good news, fatigue projects revolve around 3 simple steps, which even a beginner can learn. &lt;/p&gt;

&lt;p&gt;Step-1: Download the framework, and spend the next few hours tweaking &lt;code&gt;App.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Step-2: Realize that you have no idea what you're doing and start frantically googling for tutorials and documentation(...we provide the best documentation - trust us).&lt;/p&gt;

&lt;p&gt;Step-3: Give up in frustration &amp;amp; go back to vanilla JS and jQuery.&lt;/p&gt;

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

&lt;p&gt;In conclusion, Fatigue.js may be the latest and greatest thing in the world of web development, but that doesn't necessarily mean it's right for you. &lt;/p&gt;

&lt;p&gt;Before jumping on any new trend, take the time to evaluate whether it actually solves a problem you're facing(or rather how is the framework different than the ones already existing), and whether it's worth the time and effort to learn. &lt;/p&gt;

&lt;p&gt;And if you do decide to try out &lt;a href="https://www.youtube.com/watch?v=dQw4w9WgXcQ"&gt;Fatigue.js&lt;/a&gt;, just remember that you're not alone in feeling overwhelmed and confused. Good luck, and happy coding! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JJbpXpAu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u314gudghw0u2vxqjz74.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JJbpXpAu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u314gudghw0u2vxqjz74.png" alt="Billy" width="320" height="320"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This shitpost was inspired from a &lt;a href="https://dev.to/victorocna/the-downfall-of-web-development-1278"&gt;recent post&lt;/a&gt;, I read. While this, is meant to be parody in all sense, you can check out the linked post for more serious advice. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hope you enjoy reading the article as much as I enjoyed writing it.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Why do you need django signals right now?</title>
      <dc:creator>Dev Mehta</dc:creator>
      <pubDate>Thu, 02 Mar 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/devmehta/why-do-you-need-django-signals-right-now-19n3</link>
      <guid>https://dev.to/devmehta/why-do-you-need-django-signals-right-now-19n3</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fvprc4bzrjky42awn3tr9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvprc4bzrjky42awn3tr9.jpg" alt="Cover Image" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Meet Jane, a passionate software developer who loves building robust and efficient web applications using Django. Jane is currently working on a project where she needs to execute a certain piece of code based on certain events that occur in her app.&lt;/p&gt;

&lt;p&gt;She has heard about Django signals, but she is not quite sure what they are and how they work.&lt;/p&gt;

&lt;p&gt;Django signals are a powerful tool for executing code based on certain events in your Django application.&lt;/p&gt;

&lt;p&gt;Signals are decoupled and allow senders to notify a set of receivers that some action has taken place.&lt;/p&gt;

&lt;p&gt;This can be useful for automating tasks, such as sending emails, updating records, or logging data, whenever a certain event occurs in your app. In this tutorial, we will go through the process of using Django signals to execute code based on certain events in your app.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Django Signals
&lt;/h2&gt;

&lt;p&gt;Django signals are a way to allow certain senders to notify a set of receivers that some action has taken place. Signals are sent by senders, using the &lt;code&gt;Signal.send()&lt;/code&gt; method, and are received by receivers, using the &lt;code&gt;@receiver&lt;/code&gt; decorator.&lt;/p&gt;

&lt;p&gt;There are several built-in signals in Django, such as &lt;code&gt;pre_save&lt;/code&gt;, &lt;code&gt;post_save&lt;/code&gt;, &lt;code&gt;pre_delete&lt;/code&gt;, and &lt;code&gt;post_delete&lt;/code&gt;. These signals are sent whenever a model instance is saved or deleted. You can also create custom signals for your app.&lt;/p&gt;

&lt;p&gt;Django signals can be used for a wide range of events, including user authentication, object creation, object deletion, and many more. By using signals, developers can create more modular and extensible code that is easier to maintain and test.&lt;/p&gt;

&lt;p&gt;In the next section, we'll take a closer look at how Jane can use Django signals in her project to execute code based on certain events.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fpeq43s14jzxnua1m9b3y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fpeq43s14jzxnua1m9b3y.png" alt="image" width="600" height="770"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Signal Receiver
&lt;/h2&gt;

&lt;p&gt;Now that you understand the basics of Django signals, it's time to explore how to use them in your own projects. Let's continue following the journey of our character, Jane, as she discovers the power of signals in her Django app.&lt;/p&gt;

&lt;p&gt;Jane has been working on a web application that allows users to subscribe to different newsletters based on their interests. Whenever a user subscribes or unsubscribes from a newsletter, Jane needs to update the user's profile to reflect their choices.&lt;/p&gt;

&lt;p&gt;She could update the user's profile directly whenever a subscription or unsubscription occurs, but that would create extra code and make her application less modular. Instead, Jane decides to use signals to keep her code clean and organized.&lt;/p&gt;

&lt;p&gt;The first step for Jane is to define the signals that her app will use. She creates two signals, one for subscribing and one for unsubscribing, and gives them descriptive names: &lt;code&gt;user_subscribed&lt;/code&gt;and &lt;code&gt;user_unsubscribed&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;She also includes any relevant data that needs to be passed along with the signal, such as the user's email.&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;# File: yourapp/signals.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.dispatch&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Signal&lt;/span&gt;

&lt;span class="n"&gt;user_subscribed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;providing_args&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;user_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;user_unsubscribed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Signal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;providing_args&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;user_email&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;Once Jane has defined her signals, she needs to connect them to the appropriate functions that will execute whenever the signal is triggered. She creates two functions, &lt;code&gt;subscribe_newsletter&lt;/code&gt; and &lt;code&gt;unsubscribe_newsletter&lt;/code&gt;,that will update the user's profile whenever a subscription or unsubscription occurs.&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;# File: yourapp/signals.py
&lt;/span&gt;&lt;span class="bp"&gt;...&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.dispatch&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;receiver&lt;/span&gt;

&lt;span class="bp"&gt;...&lt;/span&gt;
&lt;span class="nd"&gt;@receiver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_subscribed&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;subscribe_newsletter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sender&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;user_email&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;user_email&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="c1"&gt;# Business logic
&lt;/span&gt;        &lt;span class="c1"&gt;# user = User.objects.get(email=user_email)
&lt;/span&gt;    &lt;span class="c1"&gt;# user.profile.subscriptions.add("my_newsletter")
&lt;/span&gt;        &lt;span class="c1"&gt;# user.profile.save()
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Subscribed &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user_email&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; to our newsletter&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@receiver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_unsubscribed&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;unsubscribe_newsletter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sender&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;user_email&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;user_email&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

    &lt;span class="c1"&gt;# Business logic
&lt;/span&gt;        &lt;span class="c1"&gt;# user = User.objects.get(email=user_email)
&lt;/span&gt;    &lt;span class="c1"&gt;# user.profile.subscriptions.remove("my_newsletter")
&lt;/span&gt;        &lt;span class="c1"&gt;# user.profile.save()
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Unsubscribed &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user_email&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; from our newsletter&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Connecting recievers &amp;amp; signals
&lt;/span&gt;&lt;span class="n"&gt;user_subscribed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subscribe_newsletter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;user_unsubscribed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unsubscribe_newsletter&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 example, the &lt;code&gt;@receiver&lt;/code&gt; decorator is used to register the &lt;code&gt;subscribe_newslettter()&lt;/code&gt; function as a receiver for the &lt;code&gt;user_subscribed&lt;/code&gt; signal. The function simply prints it out to the console.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use it in your views
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fl3jyp3azp14diuv7jm9r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fl3jyp3azp14diuv7jm9r.png" alt="image" width="300" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s it now Jane can use those signals in her &lt;code&gt;views.py&lt;/code&gt; 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;.signals&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;user_subscribed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;user_unsubscribed&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;HttpResponse&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;subscribe&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_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;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;user_email&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;user_email&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&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;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Please provide a user_email parameter in GET request.&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;user_subscribed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sender&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="n"&gt;user_email&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;user_email&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;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Subscribed &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user_email&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; to our newsletter.&lt;/span&gt;&lt;span class="sh"&gt;"&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;unsubscribe&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_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;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;user_email&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;user_email&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&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;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Please provide a user_email parameter in GET request.&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;user_unsubscribed&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sender&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="n"&gt;user_email&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;user_email&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;HttpResponse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Unsubscribed &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user_email&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; from our newsletter.&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;With her signals and functions in place, Jane can now use them in her application whenever a user subscribes or unsubscribes from a newsletter. She simply needs to import the appropriate signal and send it with the relevant data.&lt;/p&gt;

&lt;h2&gt;
  
  
  To make it work…
&lt;/h2&gt;

&lt;p&gt;There is one catch to this situation, the code isn’t functional yet. To make our code work as desired we need to import our signals into our application’s config file - &lt;code&gt;apps.py&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="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.apps&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AppConfig&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AccountsConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AppConfig&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;default_auto_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;django.db.models.BigAutoField&lt;/span&gt;&lt;span class="sh"&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;accounts&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;ready&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="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;accounts.signals&lt;/span&gt;
                &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Signals connected&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 example, the &lt;code&gt;ready()&lt;/code&gt; method imports the &lt;code&gt;signals.py&lt;/code&gt; module, which contains the our signals. It then logs a message to indicate that the signal receiver has been connected.&lt;/p&gt;

&lt;p&gt;Now when you send requests to your view endpoints, you can see your signals working in action.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fxb1yo7fy7vcxgg3q53yd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxb1yo7fy7vcxgg3q53yd.png" alt="image" width="800" height="135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Also see
&lt;/h2&gt;

&lt;p&gt;You can also use in-built model signals like &lt;code&gt;pre-save&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="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="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.db.models.signals&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;pre_save&lt;/span&gt;

&lt;span class="nd"&gt;@receiver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pre_save&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sender&lt;/span&gt;&lt;span class="o"&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;def&lt;/span&gt; &lt;span class="nf"&gt;user_created&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sender&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;user&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;instance&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="c1"&gt;# Other business logic you might want to add...
&lt;/span&gt;    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Creating user, &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="si"&gt;}&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;Django documentation will always be the number 1 resource for further studying: &lt;a href="https://docs.djangoproject.com/en/4.1/topics/signals/" rel="noopener noreferrer"&gt;https://docs.djangoproject.com/en/4.1/topics/signals/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://simpleisbetterthancomplex.com/tutorial/2016/07/28/how-to-create-django-signals.html" rel="noopener noreferrer"&gt;Also, this&lt;/a&gt; is a good blog post by simpleisbetterthancomplex, if you want a detailed overview; rather than an introductory post like this one.&lt;/p&gt;

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

&lt;p&gt;Django signals provide a powerful tool for executing code based on certain events in your application. By using signals, you can keep your code modular and organized, making it easier to maintain and update in the future.&lt;/p&gt;

&lt;p&gt;Whether you're building a simple web app or a complex enterprise application, signals can help you streamline your code and create a more efficient development process.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;




&lt;p&gt;If you've come this far, thanks for reading. If you like my content you can follow me here. I regularly post on my blog, you can &lt;a href="https://simplifiedweb.netlify.app/" rel="noopener noreferrer"&gt;check it out&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Also published &lt;a href="https://simplifiedweb.netlify.app/why-do-you-need-django-signals-right-now/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>signals</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Simplify your workflow with Pre-Commit Hooks</title>
      <dc:creator>Dev Mehta</dc:creator>
      <pubDate>Fri, 24 Feb 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/devmehta/simplify-your-workflow-with-pre-commit-hooks-gif</link>
      <guid>https://dev.to/devmehta/simplify-your-workflow-with-pre-commit-hooks-gif</guid>
      <description>&lt;p&gt;Picture this: You're a busy Django developer working on a large-scale web project with a team of developers. You spend hours coding, testing, and debugging, only to realize that your codebase is littered with small issues like formatting inconsistencies, syntax errors, or import sorting problems.&lt;/p&gt;

&lt;p&gt;These small issues can pile up and slow down your development workflow, especially when working with a large team.&lt;/p&gt;

&lt;p&gt;That's where pre-commit comes in. Pre-commit is a simple yet powerful tool that can help you catch these issues before they make it into your codebase. It is a &lt;code&gt;git&lt;/code&gt; hook script.&lt;/p&gt;

&lt;p&gt;Git hook scripts are useful for identifying simple issues before submission to code review. We run our hooks on every commit to automatically point out issues in code such as missing semicolons, trailing whitespace, and debug statements.&lt;/p&gt;

&lt;p&gt;By pointing these issues out before code review, this allows a code reviewer to focus on the architecture of a change while not wasting time with trivial style nitpicks.&lt;/p&gt;

&lt;p&gt;By automating code formatting, linting, and testing, pre-commit can save you time and headaches, allowing you to focus on what really matters: building great software.&lt;/p&gt;

&lt;p&gt;In this blog post, we'll explore how to use pre-commit in Django projects. We'll cover everything from installation and configuration to creating custom hooks and integrating with CI tools.&lt;/p&gt;

&lt;p&gt;By the end of this post, you'll have a solid understanding of how to use pre-commit to improve your workflow and streamline your development process. So, let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation &amp;amp; Configuration
&lt;/h2&gt;

&lt;p&gt;The first step to using pre-commit in your Django project is to install it using pip, the Python package manager.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install pre-commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have pre-commit installed, you'll need to configure it using a file called .pre-commit-config.yaml.&lt;/p&gt;

&lt;p&gt;This configuration file is where you define the hooks that &lt;code&gt;pre-commit&lt;/code&gt; will run on your code. A hook is a command or script that checks your code for issues like formatting errors, syntax mistakes, or security vulnerabilities. There is a wide range of hooks that you can use in your Django project, and you can also create your own custom hooks.&lt;/p&gt;

&lt;p&gt;In your &lt;code&gt;.pre-commit-config.yaml&lt;/code&gt; file, you'll specify which hooks you want to run and in what order. You can also set options for each hook, such as which files to run it on or what arguments to pass to the command.&lt;/p&gt;

&lt;p&gt;For Django projects, some of the most useful pre-commit hooks include Black for code formatting, Flake8 for code linting, isort for import sorting, and Mypy for static type checking. By running these hooks automatically, you can ensure that your code is clean, consistent, and easy to read.&lt;/p&gt;

&lt;h3&gt;
  
  
  TLDR
&lt;/h3&gt;

&lt;p&gt;To adopt &lt;code&gt;pre-commit&lt;/code&gt; into our system, we simply perform the following actions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install pre-commit: &lt;code&gt;pip install pre-commit&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;pre-commit&lt;/code&gt; to &lt;code&gt;requirements.txt&lt;/code&gt; (or &lt;code&gt;requirements-dev.txt&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Define &lt;code&gt;.pre-commit-config.yaml&lt;/code&gt; with the hooks you want to include.&lt;/li&gt;
&lt;li&gt;Execute &lt;code&gt;pre-commit install&lt;/code&gt; to install git hooks in your &lt;code&gt;.git/&lt;/code&gt; directory.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Creating .pre-commit-config.yaml
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create a file named &lt;code&gt;.pre-commit-config.yaml&lt;/code&gt; in your repository root directory(also your django project’s root directory).&lt;/li&gt;
&lt;li&gt;You can generate a very basic configuration using &lt;code&gt;pre-commit sample-config&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of creating sample-config, you can also start from scratch. In this post, I have included packages like &lt;code&gt;black&lt;/code&gt;, &lt;code&gt;flake8&lt;/code&gt; ,&lt;code&gt;reorder-python-imports&lt;/code&gt; &amp;amp; &lt;code&gt;bandit&lt;/code&gt; for our django projects.&lt;/p&gt;

&lt;p&gt;Here is a sample of pre-commit files that I am currently using for my projects. These files help me ensure that my code is clean, formatted, and free of errors before I commit it to the repository. I highly recommend using pre-commit hooks in your own projects to save time and avoid potential issues down the road.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;repos&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;repo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;https://github.com/psf/black'&lt;/span&gt;
    &lt;span class="na"&gt;rev&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;22.10.0&lt;/span&gt;
    &lt;span class="na"&gt;hooks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;black&lt;/span&gt;
        &lt;span class="na"&gt;args&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;--line-length=88'&lt;/span&gt;
        &lt;span class="na"&gt;language_version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python3&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;repo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;https://github.com/PyCQA/flake8'&lt;/span&gt;
    &lt;span class="na"&gt;rev&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;5.0.4&lt;/span&gt;
    &lt;span class="na"&gt;hooks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;flake8&lt;/span&gt;
        &lt;span class="na"&gt;args&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;--max-line-length=88'&lt;/span&gt;
        &lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;^(.*/migrations/|core/settings.py)&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;repo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;https://github.com/asottile/reorder_python_imports'&lt;/span&gt;
    &lt;span class="na"&gt;rev&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;v3.9.0&lt;/span&gt;
    &lt;span class="na"&gt;hooks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;reorder-python-imports&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;repo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;local&lt;/span&gt;
    &lt;span class="na"&gt;hooks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;django-tests&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;django-tests&lt;/span&gt;
        &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python manage.py test&lt;/span&gt;
        &lt;span class="na"&gt;always_run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
        &lt;span class="na"&gt;pass_filenames&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
        &lt;span class="na"&gt;language&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;system&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;repo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;https://github.com/PyCQA/bandit'&lt;/span&gt;
    &lt;span class="na"&gt;hooks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bandit&lt;/span&gt;
        &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bandit&lt;/span&gt;
        &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Bandit is a tool for finding common security issues in Python code&lt;/span&gt;
        &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;bandit&lt;/span&gt;
        &lt;span class="na"&gt;language&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python&lt;/span&gt;
        &lt;span class="na"&gt;language_version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python3&lt;/span&gt;
        &lt;span class="na"&gt;types&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;python&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Black is a code formatter that ensures my code follows a consistent style.&lt;/p&gt;

&lt;p&gt;Flake8 is a linter that checks my code for syntax errors, unused imports, and other potential issues. reorder-python-imports is a tool that sorts my imports alphabetically, making them easier to read and maintain.&lt;/p&gt;

&lt;p&gt;bandit looks for potential security vulnerabilities in your code. By running these checks automatically, you can ensure that your code is secure and free of common security issues.&lt;/p&gt;

&lt;p&gt;In addition to using these tools, I also include a pre-commit hook that runs my unit tests before each commit. This helps me catch any bugs or regressions before they make it into the codebase.&lt;/p&gt;

&lt;p&gt;Overall, pre-commit hooks are an essential part of my workflow, and I highly recommend incorporating them into your own development process. Note that these are my personal recommendations and not the holy grail of truth for django projects, but I hope they can serve as a helpful starting point for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting it in action
&lt;/h3&gt;

&lt;p&gt;Once, you create your .pre-commit-config.yaml - configuration file, you need to install that pre-commit hook into your .git repository. Its fairly easy and simple.&lt;/p&gt;

&lt;p&gt;To install the pre-commit hook, you just need to run &lt;code&gt;git add .pre-commit-config.yaml&lt;/code&gt; and &lt;code&gt;pre-commit install&lt;/code&gt; in your terminal. This will automatically install the hook into your &lt;code&gt;.git&lt;/code&gt; directory, allowing it to run on every commit. You can also run &lt;code&gt;pre-commit run&lt;/code&gt; to manually run the hooks on your codebase. Once installed, pre-commit will run automatically every time you commit code to your repository, ensuring that your code is clean and ready for review.&lt;/p&gt;

&lt;p&gt;For more details you can look at &lt;a href="https://pre-commit.com/#usage"&gt;pre-commit documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now the next time you make some changes in your repositories, you can run &lt;code&gt;git add .&lt;/code&gt; to stage all changes and &lt;code&gt;git commit&lt;/code&gt; to commit them.&lt;/p&gt;

&lt;p&gt;Pre-commit will automatically run the hooks you specified in your &lt;code&gt;.pre-commit-config.yaml&lt;/code&gt; file on your staged changes before the commit is finalized. This will help catch any errors or issues before they make it into your codebase and save you time and headaches down the road.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZzjKIt5U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0avr7y77wxvi0j8kad1t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZzjKIt5U--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0avr7y77wxvi0j8kad1t.png" alt="Demo Screenshot" width="832" height="664"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The output will be somewhat similar to the above image, if the code passes the rules defined by linters &amp;amp; hooks, it will be mentioned as Passed by that hook, but if the code fails for some reason, the error will be shown where it fails, and you can work on that specific code. If there were formatting issues, most of the times, black will reformat them for you.&lt;/p&gt;

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

&lt;p&gt;By automating code formatting, linting, and testing, pre-commit can help catch errors and issues before they make it into your codebase. With a solid understanding of installation and configuration, and by using custom hooks, you can streamline your development process and ensure that your code is clean, consistent, and easy to read.&lt;/p&gt;

&lt;p&gt;I hope this article served as a good starting point for automating some of your linting, and code quality improvement tasks. There are many amazing hooks other than these available online. You can start at &lt;a href="https://pre-commit.com/"&gt;https://pre-commit.com&lt;/a&gt; to continue learning more about this.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>github</category>
      <category>opensource</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Make things less complicated with django model managers</title>
      <dc:creator>Dev Mehta</dc:creator>
      <pubDate>Sun, 19 Feb 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/devmehta/make-things-less-complicated-with-django-model-managers-2gl3</link>
      <guid>https://dev.to/devmehta/make-things-less-complicated-with-django-model-managers-2gl3</guid>
      <description>&lt;p&gt;In this article, we will learn about django model managers. Recently when I was scrolling through &lt;a href="https://www.reddit.com/r/django/"&gt;r/django&lt;/a&gt; reddit community, I was excited to help other django developers; solve their errors. I went through some posts and read the problems to get started, and I found the first problem, I liked to solve.&lt;/p&gt;

&lt;p&gt;The problem was “I would like to &lt;strong&gt;apply logic to my apps by having methods written in models.py&lt;/strong&gt;. What is the best way to go about this. The documentation is great for the basics but I am &lt;strong&gt;getting frustrated when working to apply business logic outside of views.py&lt;/strong&gt;. Any good resources on this type of flow? &lt;strong&gt;&lt;em&gt;Business Logic in models.py?&lt;/em&gt;&lt;/strong&gt; ”&lt;/p&gt;

&lt;p&gt;You may have faced this frustration too sometimes, because as django is a robust framework for perfectionists with deadlines, it expects us to figure this out yourself(Or, so I thought, because this question was very well covered in documentation, which you like me, might won’t have read.)&lt;/p&gt;

&lt;p&gt;So, I gave them the solution that I generally use, to write repetitive or common django filter queries in models.py file where you define your models, 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="nn"&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;Post&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="n"&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;70&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# ...
&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;get_howto_guides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&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;Post&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="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title__istartswith&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"how to"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;At first, I didn't see anything wrong with this. I was still learning and trying to make things work.&lt;/p&gt;

&lt;p&gt;But soon, people on reddit pointed out that this approach was not optimal and the best place for this is the manager. A manager manages a set of models (so basically an SQL table) and a model is just a row in that table (it shouldn't know about other rows). And boy I was embarrassed.&lt;/p&gt;

&lt;p&gt;I soon realized that as our codebase will grow, our models will become bloated with business logic that was better suited to our model managers.&lt;/p&gt;

&lt;p&gt;It wasn't until I stumbled across the concept of model managers that I realized there was a better way to organize my code(If you use reddit, join &lt;a href="https://reddit.com/r/django"&gt;r/django&lt;/a&gt;. You will get to learn so many new things daily). Model managers, I learned, are a way to encapsulate model-level operations and queries in a clean and modular way.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to do it.
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--huhPtuGG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5drf7uqwt9s4zwely6u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--huhPtuGG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5drf7uqwt9s4zwely6u.png" alt="Talk is cheap show me the code" width="604" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By default, Django adds a &lt;code&gt;Manager&lt;/code&gt; with the name &lt;code&gt;objects&lt;/code&gt; to every Django model class. However, if you want to use &lt;code&gt;objects&lt;/code&gt; as a field name, or if you want to use a name other than &lt;code&gt;objects&lt;/code&gt; for the &lt;code&gt;Manager&lt;/code&gt;, you can rename it on a per-model basis. To rename the &lt;code&gt;Manager&lt;/code&gt; for a given class, define a class attribute of type &lt;code&gt;models.Manager()&lt;/code&gt; on that model. 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="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&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;Post&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="c1"&gt;# ...
&lt;/span&gt;    &lt;span class="n"&gt;how_to&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;Manager&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;Post.how_to&lt;/code&gt; will generate an &lt;code&gt;AttributeError&lt;/code&gt; while, &lt;code&gt;Post.how_to.all()&lt;/code&gt; returns all the objects from that manager.&lt;/p&gt;

&lt;p&gt;Now, I can fit all my business logic about “How to Guide Posts” in my how_to model manager. For example, if I wanted all the posts that starts with How to, or are basically how-to-do-x type of articles, I will write the following model manager separately for those kinds of posts.&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="nn"&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;HowtoPostsManager&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;Manager&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;get_queryset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&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="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;get_queryset&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title__istartswith&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"how to"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="c1"&gt;# istartswith lookup field is used to
&lt;/span&gt;        &lt;span class="c1"&gt;# lookup case-insensitive titles.
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&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="c1"&gt;# ...
&lt;/span&gt;    &lt;span class="n"&gt;objects&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;Manager&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# Default Manager
&lt;/span&gt;    &lt;span class="n"&gt;how_to&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;HowtoPostsManager&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# our custom manager
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;Post.objects.all()&lt;/code&gt;, will return all the posts from the database, while &lt;code&gt;Post.how_to.all(),&lt;/code&gt; will return only posts whose title starts with “How to”.&lt;/p&gt;

&lt;p&gt;This example also pointed out another interesting technique: using multiple managers on the same model. You can attach as many &lt;code&gt;Manager()&lt;/code&gt; instances to a model as you’d like. This is a non-repetitive way to define common “filters” for your models.&lt;/p&gt;

&lt;h2&gt;
  
  
  QuerySets as model managers
&lt;/h2&gt;

&lt;p&gt;You can also define common filters as model managers in your django models. 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="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&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;HowtoQuery&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;QuerySet&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;title_starts_with_howto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&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="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title__istartswith&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"how to"&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;Post&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="c1"&gt;# ...
&lt;/span&gt;    &lt;span class="n"&gt;objects&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;Manager&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# Default Manager
&lt;/span&gt;    &lt;span class="n"&gt;how_to&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;HowtoQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;as_manager&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# our custom manager
&lt;/span&gt;
&lt;span class="c1"&gt;# This will be identicle to the previous code example,
# we looked at
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Not every &lt;code&gt;QuerySet&lt;/code&gt; method makes sense at the &lt;code&gt;Manager&lt;/code&gt; level; for instance django prevents the &lt;code&gt;QuerySet.delete()&lt;/code&gt; method from being copied onto the &lt;code&gt;Manager&lt;/code&gt; class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With model managers, I could write custom methods for my models that handled complex logic, filtering, and aggregation. I could also create new querysets that were specific to my application's needs, which made it easier to reuse code across my views.&lt;/p&gt;

&lt;p&gt;As I started to use model managers more in my applications, I found that my code was becoming cleaner and easier to read. I was also able to remove a lot of code from my models and keep my business logic closer where it belonged.&lt;/p&gt;

&lt;p&gt;In retrospect, it's hard to believe that I didn't know about model managers even after coding in Django since a considerable amount of time. But I'm grateful that I came across this concept when I did, as it completely transformed the way I wrote code and helped me to become a better Django developer.&lt;/p&gt;

&lt;p&gt;So, to anyone who is struggling with complex views and a messy codebase, I highly recommend exploring the power of model managers in Django. You might be surprised by how much they can simplify your code and improve your overall development experience.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Top 10 Must-Have Third-Party Packages for Web Developers</title>
      <dc:creator>Dev Mehta</dc:creator>
      <pubDate>Sun, 29 Jan 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/devmehta/top-10-must-have-third-party-packages-for-web-developers-2838</link>
      <guid>https://dev.to/devmehta/top-10-must-have-third-party-packages-for-web-developers-2838</guid>
      <description>&lt;p&gt;First of all if you are here reading this post, to find "top 10 must have third party packages for javascript", then I am sorry, that I had to clickbait you, as we all have seen, that &lt;a href="https://dev.to/derlin/devto-is-for-webdevs-and-beginners-i-have-data-to-prove-it-54c4"&gt;dev.to prefers these hashtags, more than it prefers quality content&lt;/a&gt;, also I had to add a "top 10" post, to get views.&lt;/p&gt;

&lt;p&gt;Secondly, if you are a django developer and are looking for good third party packages, then you are welcome  here. I had to clickbait you with titles and keywords, as the algorithm doesn't push posts from creators outside of JS Stacks, no matter how much we try to provide back to community :). &lt;/p&gt;

&lt;p&gt;So let's begin, and yeah this is not a "top 10" list, but "10 good packages" list.&lt;/p&gt;

&lt;p&gt;Django is a powerful web framework that makes it easy for developers to build and maintain web applications. However, as any developer knows, there are always areas where a little extra help is needed. That's where third-party packages come in. These packages are created by the community and can help to extend the functionality of your Django application in ways that would otherwise be difficult or impossible.&lt;/p&gt;

&lt;p&gt;In this post, we will be taking a look at the top 10 must-have third-party packages for Django developers. These packages will help you to add new features, improve performance, and make development easier.&lt;/p&gt;

&lt;p&gt;Packages Covered:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Django Debug Toolbar&lt;/li&gt;
&lt;li&gt;Django Rest Framework&lt;/li&gt;
&lt;li&gt;Django Extension&lt;/li&gt;
&lt;li&gt;Django Crispy Forms&lt;/li&gt;
&lt;li&gt;Django Haystack&lt;/li&gt;
&lt;li&gt;Django Allauth&lt;/li&gt;
&lt;li&gt;Django Compressor&lt;/li&gt;
&lt;li&gt;Django Storages&lt;/li&gt;
&lt;li&gt;Django Celery&lt;/li&gt;
&lt;li&gt;Django CORS Headers&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Django Debug Toolbar
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://django-debug-toolbar.readthedocs.io/en/latest/"&gt;Django Debug Toolbar&lt;/a&gt; is an incredibly useful package that helps you to debug your Django application. It adds a toolbar to your application that shows information about the current request, including the SQL queries that were run, the time it took to process the request, and much more. This package is a must-have for any developer working on a Django application.&lt;/p&gt;

&lt;p&gt;You can read &lt;a href="https://dev.to/devmehta/unlock-the-full-potential-of-your-django-app-with-these-performance-optimization-techniques-1ig6-temp-slug-1703814"&gt;this post&lt;/a&gt;, to learn how to add django-debug-toolbar to your app, for optimizng the performance of your app&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Django Rest Framework
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.django-rest-framework.org/"&gt;Django Rest Framework&lt;/a&gt; is a powerful package that makes it easy to create RESTful APIs with Django. It provides a simple, consistent interface for interacting with your Django models and includes support for authentication, permissions, and more. If you're building a web application that needs to provide an API, then Django Rest Framework is a must-have package.&lt;/p&gt;

&lt;p&gt;You can read &lt;a href="https://dev.to/devmehta/build-scalable-and-secure-apis-with-django-rest-framework-dhg"&gt;this post&lt;/a&gt;, to learn how to use django-rest-framework to build scalable and secure APIs&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Django Extension
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://django-extensions.readthedocs.io/en/latest/"&gt;Django Extension&lt;/a&gt; is a collection of useful management commands for Django. It includes commands for generating UML diagrams, running shell commands, and much more. This package is an absolute must-have for any Django developer who wants to save time and automate repetitive tasks.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Django Crispy Forms
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://django-crispy-forms.readthedocs.io/en/latest/"&gt;Django Crispy Forms&lt;/a&gt; is a package that makes it easy to create beautiful forms in Django. It provides a simple, consistent interface for rendering forms, and includes support for Bootstrap, Foundation, and other popular CSS frameworks. If you're tired of dealing with the hassle of creating forms in Django, then this package is for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Django Haystack
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://django-haystack.readthedocs.io/en/latest/"&gt;Django Haystack&lt;/a&gt; is a package that makes it easy to add search functionality to your Django application. It provides a simple, consistent interface for working with search engines like Solr, Elasticsearch, and Whoosh. If you need to add search functionality to your application, then this package is a must-have.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Django Allauth
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.intenct.nl/projects/django-allauth/"&gt;Django Allauth&lt;/a&gt; is a package that makes it easy to add authentication and authorization functionality to your Django application. It includes support for social authentication, email verification, and more. If you're building a web application that needs to provide authentication and authorization functionality, then this package is a must-have.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Django Compressor
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://django-compressor.readthedocs.io/en/latest/"&gt;Django Compressor&lt;/a&gt; is a powerful package that can help you optimize your application's performance by compressing and merging your CSS and JavaScript files. This can help reduce the number of requests your application needs to make, resulting in faster load times and improved user experience. Additionally, it also supports compressing and merging of images, which is an added bonus. With Django Compressor, you can easily minify your assets and improve the load time of your web pages.&lt;/p&gt;

&lt;p&gt;You can read &lt;a href="https://dev.to/devmehta/unlock-the-full-potential-of-your-django-app-with-these-performance-optimization-techniques-1ig6-temp-slug-1703814"&gt;this post&lt;/a&gt;, to learn how to optimize the performance of your app.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Django Storages
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://django-storages.readthedocs.io/en/1.1.8/index.html"&gt;Django Storages&lt;/a&gt; is a third-party package that allows you to use different file storage backends such as Amazon S3, Google Cloud Storage, and more. This is particularly useful when your application is growing, and you need to store large amounts of files or when you want to use a cloud-based storage solution. With Django Storages, you can easily switch between different storage backends and take advantage of their scalability and reliability.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Django Celery
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pypi.org/project/django-celery/"&gt;Django Celery&lt;/a&gt; is a powerful package that allows you to run asynchronous tasks in the background. This can be extremely useful for tasks such as sending emails, processing images, and more. With Django Celery, you can easily schedule and manage background tasks, which can help improve the performance of your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Django CORS Headers
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pypi.org/project/django-cors-headers/"&gt;Django CORS Headers&lt;/a&gt; - This package allows you to easily add Cross-Origin Resource Sharing (CORS) headers to your Django views. CORS headers are necessary to allow cross-origin requests, which can be a common issue when developing web applications. This package provides a simple and flexible way to configure CORS headers for your project, and supports both class-based views and function-based views. It is also easily configurable and can be integrated with other libraries like Django Rest Framework. With Django CORS Headers, you can easily handle cross-origin requests and ensure that your application is accessible to all clients.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In conclusion, these are some of the most essential third-party packages that can help you build better web applications with Django. Whether you need to optimize your application's performance, build REST APIs, or manage background tasks, these packages have got you covered. So, the next time you're starting a new project, consider using these packages to take your development to the next level.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Build Scalable and Secure APIs with Django REST Framework</title>
      <dc:creator>Dev Mehta</dc:creator>
      <pubDate>Fri, 27 Jan 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/devmehta/build-scalable-and-secure-apis-with-django-rest-framework-dhg</link>
      <guid>https://dev.to/devmehta/build-scalable-and-secure-apis-with-django-rest-framework-dhg</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;This blog post was originally published on &lt;a href="https://simplifiedweb.netlify.app/build-scalable-and-secure-apis-with-django-rest-framework"&gt;my blog&lt;/a&gt;. You can check it out for more such posts&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Welcome to the world of &lt;a href="https://www.django-rest-framework.org/"&gt;Django REST framework&lt;/a&gt;, where building scalable and secure APIs is as easy as wielding the Founding Titan's power. In this blog post, we will be exploring the ins and outs of &lt;a href="https://www.django-rest-framework.org/"&gt;Django REST framework&lt;/a&gt;, a popular choice for building APIs.&lt;/p&gt;

&lt;p&gt;From setting up a new &lt;a href="https://www.djangoproject.com/"&gt;Django&lt;/a&gt; project to handling requests and responses with &lt;a href="https://www.django-rest-framework.org/api-guide/serializers/"&gt;serializers&lt;/a&gt; and &lt;a href="https://docs.djangoproject.com/en/3.2/topics/http/views/"&gt;views&lt;/a&gt;, we will be covering it all. You'll learn how to implement &lt;a href="https://www.django-rest-framework.org/api-guide/authentication/"&gt;authentication&lt;/a&gt; and &lt;a href="https://www.django-rest-framework.org/api-guide/permissions/"&gt;authorization&lt;/a&gt;, and how to use the built-in features of Django REST framework to make your API development process a breeze. So grab your 3D Maneuver Gear and let's begin our journey to building the ultimate API.&lt;/p&gt;

&lt;p&gt;In case you didn’t understand Founding Titan and 3D Maneuver Gear, it refers to the popular anime and manga series Attack on Titan, which is used as a creative and unique angle for the blog post, so young readers can grasp the content easily by correlating to it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Brief Overview of the Importance of APIs and why Django REST Framework is a good choice for building them
&lt;/h3&gt;

&lt;p&gt;Welcome to the world of &lt;a href="https://www.django-rest-framework.org/"&gt;Django REST framework&lt;/a&gt;, the gateway to the digital world like the &lt;a href="https://attackontitan.fandom.com/wiki/Door_of_Truth"&gt;Door of Truth&lt;/a&gt; of the Survey Corps.&lt;/p&gt;

&lt;p&gt;APIs, or &lt;a href="https://en.wikipedia.org/wiki/Application_programming_interface"&gt;Application Programming Interfaces&lt;/a&gt;, are the backbone of modern web and mobile applications, just like the &lt;a href="https://attackontitan.fandom.com/wiki/Vertical_Maneuvering_Equipment"&gt;Vertical Maneuvering Equipment&lt;/a&gt; is to the Survey Corps. They allow different software systems to communicate with each other, share data, and perform actions, much like how the Survey Corps share information and coordinate attacks against the Titans.&lt;/p&gt;

&lt;p&gt;With the rise of cloud-based services and the Internet of Things, the need for robust and scalable APIs has never been greater, just like how the Titans continue to threaten humanity. Django REST framework is a powerful tool for building APIs that can handle large amounts of data and traffic, just like how the Survey Corps use their equipment to fight Titans.&lt;/p&gt;

&lt;p&gt;In this article, we'll delve into the world of Django REST framework and how it can help you build powerful and efficient APIs, just like how the Survey Corps work together to protect humanity. Join us on this journey as we explore the ins and outs of Django REST framework and discover the secrets to building great APIs.&lt;/p&gt;

&lt;h3&gt;
  
  
  What we will cover?
&lt;/h3&gt;

&lt;p&gt;By the end of this blogpost, you will have&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A brief understanding of API Development Fundamentals&lt;/li&gt;
&lt;li&gt;How to build an API in Django - which is robust and secure framework in itself&lt;/li&gt;
&lt;li&gt;A brief understanding of API Development Lifecycle&lt;/li&gt;
&lt;li&gt;Building a Simple API with Django REST Framework&lt;/li&gt;
&lt;li&gt;Advanced Features: An exploration of more advanced features of Django REST Framework, such as authentication, permissions, and pagination.&lt;/li&gt;
&lt;li&gt;Deployment and Testing: A discussion of best practices for deploying and testing APIs in a production environment.&lt;/li&gt;
&lt;li&gt;Additional Resources to Continue Learning Further&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting Up a Django Project for API Development
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Steps for Creating a new Django project and app.
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Steps for Creating a new Django Project:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Install Django by running &lt;code&gt;pip install django&lt;/code&gt; in your command prompt or terminal.&lt;/li&gt;
&lt;li&gt;Create a new Django project by running &lt;code&gt;django-admin startproject projectname&lt;/code&gt; in your command prompt or terminal. Replace "projectname" with the desired name of your project.&lt;/li&gt;
&lt;li&gt;Change into the project directory by running &lt;code&gt;cd projectname&lt;/code&gt; in your command prompt or terminal.&lt;/li&gt;
&lt;li&gt;Run the development server by running &lt;code&gt;python manage.py runserver&lt;/code&gt; in your command prompt or terminal. This will start the server and make the project available at &lt;code&gt;http://127.0.0.1:8000/&lt;/code&gt; in your web browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Steps for Creating a new Django App:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Change into the project directory by running &lt;code&gt;cd projectname&lt;/code&gt; in your command prompt or terminal.&lt;/li&gt;
&lt;li&gt;Create a new Django app by running &lt;code&gt;python manage.py startapp appname&lt;/code&gt; in your command prompt or terminal. Replace "appname" with the desired name of your app.&lt;/li&gt;
&lt;li&gt;Add your app to the &lt;code&gt;INSTALLED_APPS&lt;/code&gt; list in the &lt;code&gt;settings.py&lt;/code&gt; file of your project.&lt;/li&gt;
&lt;li&gt;Create a new directory called &lt;code&gt;templates&lt;/code&gt; inside your app directory and create an HTML template file within it.&lt;/li&gt;
&lt;li&gt;Create a new directory called &lt;code&gt;models&lt;/code&gt; inside your app directory and create a new Python file within it to define your app's models.&lt;/li&gt;
&lt;li&gt;Create a new file called &lt;code&gt;views.py&lt;/code&gt; inside your app directory to define your app's views.&lt;/li&gt;
&lt;li&gt;Create a new file called &lt;code&gt;urls.py&lt;/code&gt; inside your app directory to define your app's URLs.&lt;/li&gt;
&lt;li&gt;Add the app's URLs to the project's &lt;code&gt;urls.py&lt;/code&gt; file to make the app accessible in the browser.&lt;/li&gt;
&lt;li&gt;Run the development server by running &lt;code&gt;python manage.py runserver&lt;/code&gt; in your command prompt or terminal to see your app in action.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Configuring the project for API Development
&lt;/h3&gt;

&lt;p&gt;Install Django REST framework by running &lt;code&gt;pip install djangorestframework&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Install the Django REST framework by adding &lt;code&gt;'rest_framework'&lt;/code&gt; to the &lt;code&gt;INSTALLED_APPS&lt;/code&gt; list in your &lt;code&gt;settings.py&lt;/code&gt;file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling Requests and Responses with Serializers and Views
&lt;/h2&gt;

&lt;p&gt;In the world of APIs, serializers and views are like the Titans and Survey Corps of the Django REST framework - they work together to handle incoming requests and outgoing responses.&lt;/p&gt;

&lt;p&gt;Serializers, are responsible for handling the data and providing a clean, structured format for it to be passed on to the views. They allow for easy data validation and conversion between Python objects and JSON.&lt;/p&gt;

&lt;p&gt;Views, are responsible for handling the incoming requests and providing an appropriate response. They use the data from the serializers to create a response that follows the proper format and structure.&lt;/p&gt;

&lt;p&gt;In the Django REST framework, there are a variety of generic views that can be used to handle different types of requests, such as the &lt;code&gt;ListAPIView&lt;/code&gt; for handling GET requests for a list of items, or the &lt;code&gt;CreateAPIView&lt;/code&gt; for handling POST requests to create a new item.&lt;/p&gt;

&lt;p&gt;To use these views, simply import them and extend them in your views.py file, like so:&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="nn"&gt;rest_framework&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;generics&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyView&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="k"&gt;pass&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Additionally, the Django REST framework provides a simple way to handle authentication and authorization using JWT tokens, similar to how the Survey Corps uses their omni-directional mobility gear to navigate and fight the Titans.&lt;/p&gt;

&lt;p&gt;In this section, we will cover the basics of using serializers and views in the Django REST framework, and provide examples of how to use them to create a functional API. We will also go over some of the more advanced features of the framework, such as pagination and filtering, and provide tips for debugging and testing your API. So, ready your blades and gear up, because we're about to take on the Titans of API development!&lt;/p&gt;

&lt;h3&gt;
  
  
  Wait, What are Serializers?
&lt;/h3&gt;

&lt;p&gt;In the world of APIs, data is often sent and received in the form of JSON or XML. Serializers in Django REST framework allow us to easily convert complex data types, such as models and querysets, into JSON or XML format.&lt;/p&gt;

&lt;p&gt;For example, in the world of Attack on Titan, let's say we have a database of information on Titans, including their height, weight, and abilities. We can use a serializer to convert this information into a format that can be easily sent over the API to our Survey Corps app, allowing them to easily access and use this information in the field.&lt;/p&gt;

&lt;p&gt;Just like Eren and his team have to be careful while handling Titans, we have to be careful while handling serializers and views in Django REST framework to make sure that our API is working properly and efficiently.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Use Serializers to Handle Data
&lt;/h3&gt;

&lt;p&gt;Imagine you are creating an API for an e-commerce website where users can view and purchase products. You have a &lt;code&gt;Product&lt;/code&gt; model with fields for name, description, price, and image. To display this information to the user, you would need to convert the &lt;code&gt;Product&lt;/code&gt; model instance into a JSON object.&lt;/p&gt;

&lt;p&gt;To do this, you would first create a serializer class that inherits from &lt;code&gt;Serializer&lt;/code&gt;, and define the fields that should be included in the JSON representation of the &lt;code&gt;Product&lt;/code&gt; model. 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="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&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="nn"&gt;.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProductSerializer&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;Serializer&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;serializers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="o"&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;CharField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&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;DecimalField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="o"&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;ImageField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Then, in your views, you can use this serializer to handle data.&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="nn"&gt;rest_framework&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Product&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;.serializers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ProductSerializer&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProductView&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;APIView&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;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&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="nb"&gt;format&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;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Product&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="nb"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;serializer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ProductSerializer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;many&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;serializer&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;ProductView&lt;/code&gt; is handling a GET request and returning a JSON representation of all products in the database. The &lt;code&gt;ProductSerializer&lt;/code&gt; is used to convert the queryset of &lt;code&gt;Product&lt;/code&gt; model instances into a JSON object.&lt;/p&gt;

&lt;p&gt;In the AOT universe, consider a scenario where you are creating an API for a database of Titans. You have a &lt;code&gt;Titan&lt;/code&gt; model with fields for name, description, size, and type. To display this information to the user, you would need to convert the &lt;code&gt;Titan&lt;/code&gt; model instance into a JSON object.&lt;/p&gt;

&lt;p&gt;To do this, you would first create a serializer class that inherits from &lt;code&gt;Serializer&lt;/code&gt;, and define the fields that should be included in the JSON representation of the &lt;code&gt;Titan&lt;/code&gt; model. 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="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&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="nn"&gt;.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Titan&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TitanSerializer&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;Serializer&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;serializers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="o"&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;CharField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="o"&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;CharField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="o"&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;CharField&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Then, in your views, you can use this serializer to handle data.&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="nn"&gt;rest_framework&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Titan&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;.serializers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;TitanSerializer&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TitanView&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;APIView&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;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&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="nb"&gt;format&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;titans&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Titan&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="nb"&gt;all&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="n"&gt;serializer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;TitanSerializer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;titans&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;many&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;serializer&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;TitanView&lt;/code&gt; is handling a GET request and returning a JSON representation of all Titans in the database. The &lt;code&gt;TitanSerializer&lt;/code&gt; is used to convert the queryset of &lt;code&gt;Titan&lt;/code&gt; model instances into a JSON object.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Use Views to Handle Responses?
&lt;/h3&gt;

&lt;p&gt;In the world of AOT, views can be thought of as the gatekeepers to your API's inner workings. Just like how a skilled member of the Survey Corps would be responsible for controlling access to the inner walls, views in Django REST framework are responsible for handling incoming requests and returning appropriate responses.&lt;/p&gt;

&lt;p&gt;To use views in Django REST framework, you first need to import the necessary classes from the &lt;code&gt;rest_framework&lt;/code&gt; package. These classes include &lt;code&gt;APIView&lt;/code&gt;, &lt;code&gt;ViewSet&lt;/code&gt;, and &lt;code&gt;ModelViewSet&lt;/code&gt;. Each of these classes provides different levels of functionality for handling requests and returning responses.&lt;/p&gt;

&lt;p&gt;For example, you can use the &lt;code&gt;APIView&lt;/code&gt; class for basic views that handle simple requests and return simple responses. On the other hand, you can use the &lt;code&gt;ModelViewSet&lt;/code&gt; class for views that handle requests related to a specific model and return responses that include data from that model.&lt;/p&gt;

&lt;p&gt;In addition to these classes, you can also use other tools provided by Django REST framework to handle views such as &lt;code&gt;@action&lt;/code&gt; decorator, &lt;code&gt;@authentication_classes&lt;/code&gt; and &lt;code&gt;@permission_classes&lt;/code&gt; decorators.&lt;/p&gt;

&lt;p&gt;Here's an example of how you might use the &lt;code&gt;APIView&lt;/code&gt; class to handle a request and return a response in an AOT themed API:&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="nn"&gt;rest_framework.views&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;APIView&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;rest_framework.response&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Response&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AttackOnTitanAPIView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;APIView&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;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&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;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'message'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Attack on Titan API is ready for action!'&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;Response&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the &lt;code&gt;AttackOnTitanAPIView&lt;/code&gt; class is a basic view that inherits from the &lt;code&gt;APIView&lt;/code&gt; class. The &lt;code&gt;get&lt;/code&gt; method is overridden to handle GET requests and return a response containing a message. This is just a simple example but it can be further built upon to handle more complex requests and return more dynamic responses.&lt;/p&gt;

&lt;p&gt;So, like the Survey Corps, you can use views to control access to your API and protect it from Titans. With the help of Django REST framework's views, you can handle incoming requests and return appropriate responses, just like how Survey Corps protects the walls from Titans.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing Authentication and Authorization
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Authentication Methods Available in Django REST Framework
&lt;/h3&gt;

&lt;p&gt;Authentication in Django REST framework is used to verify the identity of the user making a request to the API. There are several authentication methods available in Django REST framework, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Basic Authentication&lt;/strong&gt; : This is a simple method of authentication where the client sends a request with a username and password in the headers. The server then verifies the credentials and returns a response.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Token Authentication&lt;/strong&gt; : This method uses a token-based approach for authentication, where the client sends a request with a token in the headers. The server then verifies the token and returns a response.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Session Authentication&lt;/strong&gt; : This method uses session-based authentication, where the client sends a request with a session ID in the headers. The server then verifies the session ID and returns a response.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JWT Authentication&lt;/strong&gt; : JWT (JSON Web Token) is a popular way of handling authentication and authorization in RESTful APIs. With JWT, the client sends a request with a token in the headers, which is verified by the server.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this blogpost, we will discuss how to use JWT tokens for authentication and authorization in AOT style.&lt;/p&gt;

&lt;p&gt;It's important to mention that in this example, we will be using &lt;code&gt;djangorestframework-jwt&lt;/code&gt; package.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to use JWT Tokens for Authentication and Authorization
&lt;/h3&gt;

&lt;p&gt;It is important to keep the secrets of the Survey Corps safe. And just like how Eren's Founding Titan powers are protected by a special key, we can use JSON Web Tokens (JWT) to keep our API secure.&lt;/p&gt;

&lt;p&gt;JWT is a type of token-based authentication that is often used in RESTful APIs. It allows the server to authenticate the client without the need for a session or cookies. Instead, the client sends a token with each request, and the server verifies that the token is valid before processing the request.&lt;/p&gt;

&lt;p&gt;To use JWT in a Django REST framework project, we can use the &lt;code&gt;djangorestframework-jwt&lt;/code&gt; package. First, install it using pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;djangorestframework-jwt

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

&lt;/div&gt;



&lt;p&gt;Next, add &lt;code&gt;'rest_framework_jwt.authentication.JSONWebTokenAuthentication'&lt;/code&gt; to the &lt;code&gt;AUTHENTICATION_CLASSES&lt;/code&gt; setting in your &lt;code&gt;settings.py&lt;/code&gt; 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;REST_FRAMEWORK&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;'DEFAULT_PERMISSION_CLASSES'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;'rest_framework.permissions.IsAuthenticated'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="s"&gt;'DEFAULT_AUTHENTICATION_CLASSES'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;'rest_framework_jwt.authentication.JSONWebTokenAuthentication'&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;Now, when a client sends a request to your API with a valid JWT in the &lt;code&gt;Authorization&lt;/code&gt; header, the request will be considered authenticated.&lt;/p&gt;

&lt;p&gt;Incorporating JWT tokens for authentication and authorization in views can be done by using the &lt;code&gt;djangorestframework_simplejwt&lt;/code&gt; library. This library provides a set of views and authentication classes that can be easily added to your Django REST framework views.&lt;/p&gt;

&lt;p&gt;First, you will need to install the library by running &lt;code&gt;pip install djangorestframework_simplejwt&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, you will need to add the library's views and authentication classes to your project's &lt;code&gt;urls.py&lt;/code&gt; file. Here is&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="nn"&gt;rest_framework_simplejwt.views&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;TokenObtainPairView&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TokenRefreshView&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="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'api/token/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TokenObtainPairView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&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="s"&gt;'token_obtain_pair'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'api/token/refresh/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;TokenRefreshView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&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="s"&gt;'token_refresh'&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;Then, you can add the &lt;code&gt;JWTAuthentication&lt;/code&gt; class to the &lt;code&gt;authentication_classes&lt;/code&gt; attribute of your views. Here is an example of how to do 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="nn"&gt;rest_framework_simplejwt.authentication&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;JWTAuthentication&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyAPIview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;APIView&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;authentication_classes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;JWTAuthentication&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 way you will be able to authenticate and authorize your API views using JWT tokens.&lt;/p&gt;

&lt;p&gt;Note that this is a very basic example, in real-world scenarios you will need to configure the JWT settings and handle token expiration, refresh, etc.&lt;/p&gt;

&lt;p&gt;It's also possible to use JWT for authorization. When a client sends a request with a JWT, the server can decode the token to access the claims (e.g. user id, role) and use them to determine if the client has access to the requested resource.&lt;/p&gt;

&lt;p&gt;However, keep in mind that JWT is not an encryption, it's a signed token. Never put sensitive data on JWT.&lt;/p&gt;

&lt;p&gt;It is always recommended to use JWT in combination with an SSL certificate to encrypt the data in transit.&lt;/p&gt;

&lt;p&gt;You can also use other authentication methods like &lt;strong&gt;SessionAuthentication&lt;/strong&gt; and &lt;strong&gt;BasicAuthentication&lt;/strong&gt; , but JWT is considered more secure and is often preferred for use in RESTful APIs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Best Practices for Securing an API
&lt;/h3&gt;

&lt;p&gt;When it comes to securing your API, there are a few best practices you should keep in mind to ensure the safety and privacy of your users' data.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Use token-based authentication.&lt;/strong&gt; JSON Web Tokens (JWT) are a popular choice for authentication in RESTful APIs. They allow for stateless authentication and provide a secure way to transmit data between parties.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Implement role-based access control.&lt;/strong&gt; By assigning roles to users, you can limit the actions they can perform within your API. For example, a user with the role of "admin" would have more permissions than a user with the role of "guest".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use HTTPS.&lt;/strong&gt; When transmitting data over the internet, it's important to use a secure protocol like HTTPS to encrypt the data and prevent man-in-the-middle attacks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validate user input.&lt;/strong&gt; Always validate user input to prevent malicious attacks like SQL injection or cross-site scripting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Monitor your API for suspicious activity.&lt;/strong&gt; Keep an eye out for unusual patterns of usage, such as a high number of failed login attempts or a large number of requests coming from a single IP address.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these best practices, you can ensure that your API is secure and protected from potential threats.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; : Always keep the theme in mind and you can use Attack on Titans references like "Just like how Eren protects the walls, it's important to protect your API with token-based authentication" etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Features of Django REST Framework
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Pagination and Filtering of API Responses
&lt;/h3&gt;

&lt;p&gt;Pagination and filtering are important features of any API, as they allow the client to retrieve only the relevant data and avoid overwhelming them with unnecessary information. In Django REST framework, these features are built-in and can be easily implemented.&lt;/p&gt;

&lt;p&gt;To paginate the results of a view, you can use the &lt;code&gt;pagination_class&lt;/code&gt; attribute on the view. For example, if you want to use the built-in &lt;code&gt;PageNumberPagination&lt;/code&gt; class, you can do the following:&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="nn"&gt;rest_framework.pagination&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;PageNumberPagination&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;APIView&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;pagination_class&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PageNumberPagination&lt;/span&gt;
    &lt;span class="c1"&gt;# ...
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also customize the pagination class to suit your needs. For example, you can change the default page size or add a custom header to the response.&lt;/p&gt;

&lt;p&gt;Filtering the results of a view is also a straightforward process. You can use the &lt;code&gt;filter_backends&lt;/code&gt; attribute on the view to specify the filter classes that you want to use. For example, you can use the built-in &lt;code&gt;DjangoFilterBackend&lt;/code&gt; class to filter the results based on query parameters:&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="nn"&gt;rest_framework.filters&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;DjangoFilterBackend&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;APIView&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;filter_backends&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;DjangoFilterBackend&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;filterset_fields&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'age'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="c1"&gt;# ...
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows clients to filter the results by sending query parameters like &lt;code&gt;?name=John&amp;amp;age=30&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To implement pagination and filtering, you can use &lt;code&gt;Query&lt;/code&gt; and &lt;code&gt;QuerySet&lt;/code&gt; provided by &lt;code&gt;django-filter&lt;/code&gt; package and &lt;code&gt;Pagination&lt;/code&gt; provided by &lt;code&gt;django-rest-framework&lt;/code&gt; package. Also, you can add a custom header to response like &lt;code&gt;X-Total-Count&lt;/code&gt; to show the total number of results.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to use Built-In Documentation Feature?
&lt;/h3&gt;

&lt;p&gt;The Django REST framework comes with a built-in documentation feature that allows developers to easily generate documentation for their APIs. This feature is based on the popular &lt;code&gt;drf-yasg&lt;/code&gt; (Django REST framework - Yet Another Swagger Generator) library, which uses the OpenAPI specification to generate interactive documentation for APIs.&lt;/p&gt;

&lt;p&gt;To use the built-in documentation feature, first, you need to install &lt;code&gt;drf-yasg&lt;/code&gt; by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;drf-yasg

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

&lt;/div&gt;



&lt;p&gt;Next, add &lt;code&gt;'drf_yasg'&lt;/code&gt; to your &lt;code&gt;INSTALLED_APPS&lt;/code&gt; in the &lt;code&gt;settings.py&lt;/code&gt; 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;INSTALLED_APPS&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="s"&gt;'drf_yasg'&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;Then, add the following to your &lt;code&gt;urls.py&lt;/code&gt; file to enable the documentation feature:&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="nn"&gt;drf_yasg.views&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;get_schema_view&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;drf_yasg&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;openapi&lt;/span&gt;

&lt;span class="n"&gt;schema_view&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_schema_view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;openapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Info&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="s"&gt;"Snippets API"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;default_version&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'v1'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Test description"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;terms_of_service&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"https://www.google.com/policies/terms/"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;contact&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;openapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Contact&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="s"&gt;"contact@snippets.local"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="n"&gt;license&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;openapi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;License&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="s"&gt;"BSD License"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="p"&gt;),&lt;/span&gt;
   &lt;span class="n"&gt;public&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;permission_classes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;permissions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AllowAny&lt;/span&gt;&lt;span class="p"&gt;,),&lt;/span&gt;
&lt;span class="p"&gt;)&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="p"&gt;...&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;r&lt;/span&gt;&lt;span class="s"&gt;'^swagger(?P&amp;lt;format&amp;gt;\.json|\.yaml)$'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;schema_view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;without_ui&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cache_timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&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="s"&gt;'schema-json'&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;span class="sa"&gt;r&lt;/span&gt;&lt;span class="s"&gt;'^swagger/$'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;schema_view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;with_ui&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'swagger'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cache_timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&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="s"&gt;'schema-swagger-ui'&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;span class="sa"&gt;r&lt;/span&gt;&lt;span class="s"&gt;'^redoc/$'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;schema_view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;with_ui&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'redoc'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cache_timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&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="s"&gt;'schema-redoc'&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;With this setup, you can now access the API documentation by visiting the &lt;code&gt;/swagger/&lt;/code&gt; or &lt;code&gt;/redoc/&lt;/code&gt; URLs in your browser. The documentation is generated based on the &lt;code&gt;APIViews&lt;/code&gt; and &lt;code&gt;Serializers&lt;/code&gt; you have defined in your project and will be automatically updated whenever you make changes to them.&lt;/p&gt;

&lt;p&gt;You can also customize the appearance and functionality of the documentation by configuring the &lt;code&gt;get_schema_view()&lt;/code&gt; function and the &lt;code&gt;Info&lt;/code&gt; class to suit your needs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tips for Debugging and Testing the API endpoints
&lt;/h3&gt;

&lt;p&gt;When building APIs with Django REST framework, it's important to have a solid debugging and testing strategy in place to ensure that your endpoints are functioning correctly. One way to do this is to use the built-in debugging tools provided by Django, such as the &lt;code&gt;django-debug-toolbar&lt;/code&gt; and the &lt;code&gt;logging&lt;/code&gt; module.&lt;/p&gt;

&lt;p&gt;Another helpful tip for debugging API endpoints is to use the &lt;code&gt;django-extensions&lt;/code&gt; package, which adds additional management commands to the Django command line, including a &lt;code&gt;shell_plus&lt;/code&gt; command that allows you to interact with your models directly from the command line.&lt;/p&gt;

&lt;p&gt;For testing, Django REST framework provides a &lt;code&gt;APITestCase&lt;/code&gt; class that you can use to create test cases for your views and serializers. This class provides a &lt;code&gt;client&lt;/code&gt; attribute that allows you to make requests to your API endpoints and check the responses.&lt;/p&gt;

&lt;p&gt;In addition to these built-in tools, it's also a good idea to use a tool like &lt;code&gt;Postman&lt;/code&gt; or &lt;code&gt;Insomnia&lt;/code&gt; to manually test your API endpoints and ensure that they are returning the expected results.&lt;/p&gt;

&lt;p&gt;You can imagine Eren Yeager trying to break through the wall, as an example of debugging and testing your API endpoints, in order to ensure that they are working correctly and securely, you should test and debug your API endpoints to make sure they are functioning as expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Designing and Structuring API endpoints
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Best Practices for Designing and Structuring API endpoints
&lt;/h3&gt;

&lt;p&gt;When designing and structuring API endpoints for your Attack on Titan themed project, it's important to consider a few best practices to ensure that your API is easy to use, maintainable, and scalable.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Use clear and consistent naming conventions&lt;/strong&gt; : Use descriptive, easy-to-understand names for your endpoints. For example, instead of naming an endpoint &lt;code&gt;/api/get_data&lt;/code&gt;, use &lt;code&gt;/api/titans/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Group related endpoints together&lt;/strong&gt; : Group related endpoints together under a common prefix. For example, all endpoints related to Titans should be grouped under &lt;code&gt;/api/titans/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use HTTP methods appropriately&lt;/strong&gt; : Use the appropriate HTTP method for each endpoint. For example, use &lt;code&gt;GET&lt;/code&gt; for retrieving data, &lt;code&gt;POST&lt;/code&gt; for creating new resources, &lt;code&gt;PUT&lt;/code&gt; for updating existing resources, and &lt;code&gt;DELETE&lt;/code&gt; for deleting resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Version your API&lt;/strong&gt; : Versioning your API is important to ensure that changes to your API do not break existing clients. This can be done by including the version number in the endpoint's URL. For example, &lt;code&gt;/api/v1/titans/&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Provide clear and accurate error messages&lt;/strong&gt; : If an error occurs, provide clear and accurate error messages to help developers troubleshoot the issue.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these best practices, you can ensure that your API is easy to use, maintainable, and scalable, making it a valuable tool for your team and your audience.&lt;/p&gt;

&lt;h3&gt;
  
  
  Different Types of API endpoints
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Resource-based endpoints&lt;/strong&gt; : These endpoints are based on a specific resource, such as a user or a product. For example, &lt;code&gt;/users/&lt;/code&gt; or &lt;code&gt;/products/&lt;/code&gt;. These endpoints are typically used for CRUD (create, read, update, delete) operations on the resource.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Collection-based endpoints&lt;/strong&gt; : These endpoints are based on a collection of resources, such as a list of users or a list of products. For example, &lt;code&gt;/users/&lt;/code&gt; or &lt;code&gt;/products/&lt;/code&gt;. These endpoints are typically used to retrieve a list of resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Action-based endpoints&lt;/strong&gt; : These endpoints are based on a specific action that can be performed on a resource or a collection of resources. For example, &lt;code&gt;/users/search/&lt;/code&gt; or &lt;code&gt;/products/order/&lt;/code&gt;. These endpoints are typically used to perform non-CRUD operations on the resource.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hybrid endpoints&lt;/strong&gt; : These endpoints combine the functionality of resource-based and action-based endpoints. For example, &lt;code&gt;/users/{id}/activate/&lt;/code&gt; or &lt;code&gt;/products/{id}/reviews/&lt;/code&gt;. These endpoints are typically used for more complex operations on the resource.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is important to note that the above types of endpoints are not mutually exclusive and a single endpoint can have multiple types of functionality. For example, a &lt;code&gt;/users/&lt;/code&gt; endpoint may have functionality for both resource-based and collection-based operations.&lt;/p&gt;

&lt;p&gt;When designing and structuring API endpoints, it is important to follow RESTful principles and use clear, consistent, and predictable URL structures. Additionally, it is also important to consider how the endpoints will be consumed by the client and to provide clear documentation for each endpoint.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Version the API?
&lt;/h3&gt;

&lt;p&gt;When building an API, it's important to plan for the possibility of making changes to the API in the future. One way to do this is by versioning the API.&lt;/p&gt;

&lt;p&gt;There are a few different ways to version an API, but the most common approach is to include the version number in the URL. For example, instead of having a URL like &lt;code&gt;https://example.com/api/users&lt;/code&gt;, you might have &lt;code&gt;https://example.com/api/v1/users&lt;/code&gt; to indicate that this is version 1 of the API.&lt;/p&gt;

&lt;p&gt;Another approach is to include the version number in the request headers. For example, you might include a &lt;code&gt;Accept&lt;/code&gt; header with the value &lt;code&gt;application/vnd.example.v1+json&lt;/code&gt; to indicate that the client is requesting version 1 of the API.&lt;/p&gt;

&lt;p&gt;When making changes to the API, it's important to keep the old version available for a period of time to give existing clients time to update. This is known as deprecation. It's also a good practice to document the changes made in each version of the API to make it easier for developers to understand the changes.&lt;/p&gt;

&lt;p&gt;When it comes to AOT themed API versioning, we can use the concept of the different arcs and arcs in the anime series to version different endpoints. So an endpoint under the &lt;code&gt;AOT: Arc 1&lt;/code&gt; version would be different than an endpoint under the &lt;code&gt;AOT: Arc 2&lt;/code&gt; version.&lt;/p&gt;

&lt;p&gt;It's important to keep in mind that versioning is a trade-off between flexibility and stability. The more versions you have, the more flexibility you have to make changes, but the more complexity you introduce for clients to keep track of. It's important to find a balance that works for your use case.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  What have we learned?
&lt;/h3&gt;

&lt;p&gt;In this blog post, we have learned about the importance of APIs and how Django REST framework can be used to build them. We have covered various topics such as setting up a Django project for API development, handling requests and responses with serializers and views, implementing authentication and authorization, and additional features of Django REST framework such as pagination and filtering of API responses, documentation and debugging and testing.&lt;/p&gt;

&lt;p&gt;We also went over AOT themed examples and how it can be incorporated in various sections of the blog post. Additionally, we discussed best practices for designing and structuring API endpoints, different types of endpoints, and how to version the API.&lt;/p&gt;

&lt;h3&gt;
  
  
  Additional Resources
&lt;/h3&gt;

&lt;p&gt;To continue learning about Django REST framework and building APIs, we recommend the following resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The official Django REST framework documentation: &lt;a href="https://www.django-rest-framework.org/"&gt;https://www.django-rest-framework.org/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;The Django REST framework tutorial: &lt;a href="https://www.django-rest-framework.org/tutorial/quickstart/"&gt;https://www.django-rest-framework.org/tutorial/quickstart/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Building APIs with Django and Django REST framework: &lt;a href="https://www.safaribooksonline.com/library/view/building-apis-with/9781491949268/"&gt;https://www.safaribooksonline.com/library/view/building-apis-with/9781491949268/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Django REST framework: The beginner's guide: &lt;a href="https://www.oreilly.com/library/view/django-rest-framework/9781789802482/"&gt;https://www.oreilly.com/library/view/django-rest-framework/9781789802482/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you for reading, and I hope you found this blog post helpful in your journey to building APIs with Django REST framework.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This blog post was originally published on &lt;a href="https://simplifiedweb.netlify.app/build-scalable-and-secure-apis-with-django-rest-framework"&gt;my blog&lt;/a&gt;. You can check it out for more such posts&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>django</category>
      <category>api</category>
      <category>restframework</category>
      <category>python</category>
    </item>
    <item>
      <title>The Ultimate Django Cheatsheet: Because Even Ninjas Need a Little Help Sometimes</title>
      <dc:creator>Dev Mehta</dc:creator>
      <pubDate>Thu, 26 Jan 2023 00:00:00 +0000</pubDate>
      <link>https://dev.to/devmehta/the-ultimate-django-cheatsheet-because-even-ninjas-need-a-little-help-sometimes-21bf</link>
      <guid>https://dev.to/devmehta/the-ultimate-django-cheatsheet-because-even-ninjas-need-a-little-help-sometimes-21bf</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This blogpost was originally published on &lt;a href="https://simplifiedweb.netlify.app/the-ultimate-django-cheatsheet-because-even-ninjas-need-a-little-help-sometimes" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;. You can check it out for more such posts.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Are you a Django ninja who's feeling a little rusty on the details? Or a newbie just trying to navigate the complex world of web development? Well, we've got you covered with the ultimate Django cheatsheet!&lt;/p&gt;

&lt;p&gt;First up, let's start with some basic commands. Want to create a new project? Just type &lt;code&gt;django-admin startproject projectname&lt;/code&gt; into the command line. Want to start the development server? &lt;code&gt;python manage.py runserver&lt;/code&gt; is your new best friend.&lt;/p&gt;

&lt;p&gt;But what about when things get a little more complex? Fear not, because we've got you covered with some handy tips and tricks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Want to create a new app within your project? &lt;code&gt;python manage.py startapp appname&lt;/code&gt; is all you need.&lt;/li&gt;
&lt;li&gt;Need to create a new database table? Use &lt;code&gt;python manage.py makemigrations appname&lt;/code&gt; to create the initial migration, and then &lt;code&gt;python manage.py migrate&lt;/code&gt; to actually apply the changes.&lt;/li&gt;
&lt;li&gt;Want to add a new field to an existing table? Use &lt;code&gt;python manage.py makemigrations appname&lt;/code&gt; again, and then &lt;code&gt;python manage.py migrate&lt;/code&gt; as before.&lt;/li&gt;
&lt;li&gt;Need to create a new superuser? &lt;code&gt;python manage.py createsuperuser&lt;/code&gt; will prompt you for a username, email, and password.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And those are just the basics! With the ultimate Django cheatsheet by your side, you'll be able to tackle even the most complex projects with ease. So don't be afraid to get your hands dirty and dive into the world of web development. Because with Django, the possibilities are endless.&lt;/p&gt;

&lt;p&gt;So, don't be afraid to use this cheatsheet like a pro and remember, even the most experienced Ninjas needs a little help sometimes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining Django Models
&lt;/h2&gt;

&lt;p&gt;Here's an example of a Django model for a &lt;code&gt;Ninja&lt;/code&gt; class in the style of the popular anime/manga series Naruto:&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Ninja&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;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;village&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;jutsu&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="n"&gt;chakra&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;registered_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;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;sharingan&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;BooleanField&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="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;team&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;blank&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;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;span class="n"&gt;picture&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;ImageField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;upload_to&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ninja_images&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;blank&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;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;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;name&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This model defines a &lt;code&gt;Ninja&lt;/code&gt; class that inherits from &lt;code&gt;models.Model&lt;/code&gt;. It has several fields: &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;village&lt;/code&gt;, &lt;code&gt;jutsu&lt;/code&gt;, &lt;code&gt;chakra&lt;/code&gt;, &lt;code&gt;registered_date&lt;/code&gt;, &lt;code&gt;sharingan&lt;/code&gt;, &lt;code&gt;team&lt;/code&gt;, and &lt;code&gt;picture&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;village&lt;/code&gt; fields are both &lt;code&gt;CharField&lt;/code&gt;s, with a maximum length of 255 characters. The &lt;code&gt;jutsu&lt;/code&gt; field is a &lt;code&gt;TextField&lt;/code&gt;, which can store longer pieces of text, and &lt;code&gt;chakra&lt;/code&gt; is an &lt;code&gt;IntegerField&lt;/code&gt; representing the amount of chakra a ninja has.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;registered_date&lt;/code&gt; field is a &lt;code&gt;DateTimeField&lt;/code&gt; with &lt;code&gt;auto_now_add=True&lt;/code&gt; this means that it will automatically be set to the current date and time when a new &lt;code&gt;Ninja&lt;/code&gt; is created.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;sharingan&lt;/code&gt; field is a &lt;code&gt;BooleanField&lt;/code&gt; which can be used to represent whether the ninja has the Sharingan or not, with a default value of False.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;team&lt;/code&gt; field is a &lt;code&gt;CharField&lt;/code&gt; with a maximum length of 255 characters, and is optional(&lt;code&gt;blank=True, null=True&lt;/code&gt;) since not all ninjas are part of a team.&lt;/p&gt;

&lt;p&gt;Finally, the &lt;code&gt;picture&lt;/code&gt; field is an &lt;code&gt;ImageField&lt;/code&gt; that will handle the upload of an image of the ninja and store it in the 'ninja_images' directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Defining views
&lt;/h2&gt;

&lt;p&gt;Here's an example of a &lt;code&gt;views.py&lt;/code&gt; file for a Django application in the style of the popular anime/manga series Naruto:&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.shortcuts&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;render&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;HttpResponse&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;Ninja&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&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;all_ninjas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Ninja&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;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;all_ninjas&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;all_ninjas&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;ninjas/index.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;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ninja_detail&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;ninja_id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;ninja&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Ninja&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="n"&gt;pk&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;ninja_id&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;ninja&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ninja&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;ninjas/ninja_detail.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;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;team&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;team_name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;team_members&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Ninja&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;team__iexact&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;team_name&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;team_members&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;team_members&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team_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;team_name&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;ninjas/team.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;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;search&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;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="n"&gt;search_results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Ninja&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="n"&gt;query&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;search_results&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;search_results&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;query&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;ninjas/search.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;This &lt;code&gt;views.py&lt;/code&gt; file contains several functions that handle different pages of the application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;index&lt;/code&gt; function retrieves all the &lt;code&gt;Ninja&lt;/code&gt; objects from the database using the &lt;code&gt;Ninja.objects.all()&lt;/code&gt; method and stores them in the context dictionary. Then it renders the &lt;code&gt;index.html&lt;/code&gt; template and passes the context to it.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;ninja_detail&lt;/code&gt; function retrieves a specific &lt;code&gt;Ninja&lt;/code&gt; object from the database using the &lt;code&gt;Ninja.objects.get(pk=ninja_id)&lt;/code&gt; method and stores it in the context dictionary. Then it renders the &lt;code&gt;ninja_detail.html&lt;/code&gt; template and passes the context to it.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;team&lt;/code&gt; function retrieves all the &lt;code&gt;Ninja&lt;/code&gt; objects that belong to a specific team using the &lt;code&gt;Ninja.objects.filter(team__iexact=team_name)&lt;/code&gt; method and stores them in the context dictionary. Then it renders the &lt;code&gt;team.html&lt;/code&gt; template and passes the context to it.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;search&lt;/code&gt; function retrieves all the &lt;code&gt;Ninja&lt;/code&gt; objects whose name contains a specific query using the &lt;code&gt;Ninja.objects.filter(name__icontains=query)&lt;/code&gt; method and stores them in the context dictionary. Then it renders the &lt;code&gt;search.html&lt;/code&gt; template and passes the context to it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's important to notice that this is just an example, and you may need to adapt this views to your specific use case and also to add the URL routing for these views.&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting Views to URL Endpoints
&lt;/h2&gt;

&lt;p&gt;Here’s an example of how to connect the views we have defined to their endpoints&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.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;.&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;views&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;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="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;ninja/&amp;lt;int:ninja_id&amp;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;views&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ninja_detail&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;ninja_detail&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;team/&amp;lt;str:team_name&amp;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;views&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;team&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;team&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;search/&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;search&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;search&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;This &lt;code&gt;urls.py&lt;/code&gt; file maps URLs to the corresponding views in the &lt;code&gt;views.py&lt;/code&gt; file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The empty string &lt;code&gt;''&lt;/code&gt; maps to the &lt;code&gt;index&lt;/code&gt; view.&lt;/li&gt;
&lt;li&gt;The string &lt;code&gt;'ninja/&amp;lt;int:ninja_id&amp;gt;/'&lt;/code&gt; maps to the &lt;code&gt;ninja_detail&lt;/code&gt; view, and includes a variable &lt;code&gt;ninja_id&lt;/code&gt; that is passed to the view as an argument.&lt;/li&gt;
&lt;li&gt;The string &lt;code&gt;'team/&amp;lt;str:team_name&amp;gt;/'&lt;/code&gt; maps to the &lt;code&gt;team&lt;/code&gt; view, and includes a variable &lt;code&gt;team_name&lt;/code&gt; that is passed to the view as an argument.&lt;/li&gt;
&lt;li&gt;The string &lt;code&gt;'search/'&lt;/code&gt; maps to the &lt;code&gt;search&lt;/code&gt; view.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also notice that each path has a name, this names can be used in the template to generate URLs. Also, it's important to include this urls.py file in the main urls.py file of your project with the &lt;code&gt;include&lt;/code&gt; function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rendering HTML Responses for above views
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;index.html&lt;/strong&gt;&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="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Welcome to the Ninja Database&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 ninja in all_ninjas %}
    &lt;span class="nt"&gt;&amp;lt;li&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"{% url 'ninja_detail' ninja.id %}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;{{ ninja.name }}&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt; from {{ ninja.village }}&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
{% endfor %}
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This template displays a heading and an unordered list of all the ninjas in the database. Each list item contains a link to the &lt;code&gt;ninja_detail&lt;/code&gt; view for that specific ninja, using the &lt;code&gt;{% url %}&lt;/code&gt; template tag to generate the URL and passing the &lt;code&gt;ninja.id&lt;/code&gt; as the &lt;code&gt;ninja_id&lt;/code&gt; argument.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ninja_detail.html&lt;/strong&gt;&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="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;{{ ninja.name }}&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Village: {{ ninja.village }}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Jutsu: {{ ninja.jutsu }}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Chakra: {{ ninja.chakra }}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Has Sharingan: {% if ninja.sharingan %}yes{% else %}no{% endif %}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Team: {% if ninja.team %}{{ ninja.team }}{% else %}none{% endif %}&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
{% if ninja.picture %}
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"{{ ninja.picture.url }}"&lt;/span&gt; &lt;span class="na"&gt;alt=&lt;/span&gt;&lt;span class="s"&gt;"{{ ninja.name }}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
{% endif %}

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

&lt;/div&gt;



&lt;p&gt;This template displays the details of a single ninja, including name, village, jutsu, chakra, Sharingan,etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Function Based Views
&lt;/h2&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.shortcuts&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;get_object_or_404&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;Ninja&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&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;all_ninjas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Ninja&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;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;all_ninjas&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;all_ninjas&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;ninjas/index.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;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;ninja_detail&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;ninja_id&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;ninja&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_object_or_404&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Ninja&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pk&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;ninja_id&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;ninja&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ninja&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;ninjas/ninja_detail.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;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;team&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;team_name&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;team_members&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Ninja&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;team__iexact&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;team_name&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;team_members&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;team_members&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team_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;team_name&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;ninjas/team.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;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;search&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;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="n"&gt;search_results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Ninja&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="n"&gt;query&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;search_results&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;search_results&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;query&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;query&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;ninjas/search.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;This is similar to the previous example, the main difference is the use of get_object_or_404 function in ninja_detail view to handle cases when the ninja_id passed in the URL is not found.&lt;/p&gt;

&lt;p&gt;It's important to notice that this is just an example, and you may need to adapt this views to your specific use case and also to add the URL routing for these views.&lt;/p&gt;

&lt;h2&gt;
  
  
  Class Based Views
&lt;/h2&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.shortcuts&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;render&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="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;DetailView&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;Ninja&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NinjaListView&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="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Ninja&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;ninjas/index.html&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;all_ninjas&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;NinjaDetailView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DetailView&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;Ninja&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;ninjas/ninja_detail.html&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ninja&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;TeamView&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="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;ninjas/team.html&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team_members&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;get_queryset&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;Ninja&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;team__iexact&lt;/span&gt;&lt;span class="o"&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;kwargs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team_name&lt;/span&gt;&lt;span class="sh"&gt;'&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;get_context_data&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;kwargs&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="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;get_context_data&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;context&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team_name&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kwargs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team_name&lt;/span&gt;&lt;span class="sh"&gt;'&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;context&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SearchView&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="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;ninjas/search.html&lt;/span&gt;&lt;span class="sh"&gt;'&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;search_results&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;get_queryset&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;query&lt;/span&gt; &lt;span class="o"&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;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;return&lt;/span&gt; &lt;span class="n"&gt;Ninja&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="n"&gt;query&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;get_context_data&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;kwargs&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="nf"&gt;super&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;get_context_data&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;context&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;query&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="n"&gt;self&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;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;return&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This is similar to the function-based views example, but it uses Django's built-in class-based views to handle the common logic of displaying lists and details of objects. The &lt;code&gt;ListView&lt;/code&gt; and &lt;code&gt;DetailView&lt;/code&gt; classes are generic views that handle fetching and paginating the objects from the database, and rendering the template.&lt;/p&gt;

&lt;p&gt;It's important to notice that &lt;code&gt;TeamView&lt;/code&gt; and &lt;code&gt;SearchView&lt;/code&gt; are subclasses of &lt;code&gt;ListView&lt;/code&gt; and they implement the methods &lt;code&gt;get_queryset&lt;/code&gt; and &lt;code&gt;get_context_data&lt;/code&gt; to handle the specific logic of displaying a team members and search results. It's important also to notice that this is just an example, and you may need to adapt this views to your specific use case and also to add the URL routing for these views.&lt;/p&gt;

&lt;h2&gt;
  
  
  Forms
&lt;/h2&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&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;forms&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;Ninja&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NinjaForm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;forms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ModelForm&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;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Ninja&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;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;village&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;jutsu&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;chakra&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;sharingan&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;team&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;picture&lt;/span&gt;&lt;span class="sh"&gt;'&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="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="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;jutsu&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;widget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attrs&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;class&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;jutsu-selector&lt;/span&gt;&lt;span class="sh"&gt;'&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;fields&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;team&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;widget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attrs&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;class&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;team-selector&lt;/span&gt;&lt;span class="sh"&gt;'&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;fields&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;picture&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;widget&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;attrs&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;class&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;picture-input&lt;/span&gt;&lt;span class="sh"&gt;'&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;SearchForm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;forms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Form&lt;/span&gt;&lt;span class="p"&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;forms&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;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Search for a ninja&lt;/span&gt;&lt;span class="sh"&gt;'&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;team&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;forms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ChoiceField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;choices&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="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;All teams&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;Team 7&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;Team 7&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;Team 8&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;Team 8&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;Team 10&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;Team 10&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;Akatsuki&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;Akatsuki&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This is an example of how to create a form to manage the data of a Ninja model. The &lt;code&gt;NinjaForm&lt;/code&gt; class is a &lt;code&gt;ModelForm&lt;/code&gt; that is created using the Ninja model and the fields specified in the &lt;code&gt;fields&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;__init__&lt;/code&gt; method is overriden to add classes to the widgets of the form fields, for example &lt;code&gt;jutsu-selector&lt;/code&gt; to jutsu field, &lt;code&gt;team-selector&lt;/code&gt; to team field and &lt;code&gt;picture-input&lt;/code&gt; to picture field.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;SearchForm&lt;/code&gt; class is a regular &lt;code&gt;Form&lt;/code&gt; class, it's created using the &lt;code&gt;CharField&lt;/code&gt; and &lt;code&gt;ChoiceField&lt;/code&gt; form fields. The &lt;code&gt;query&lt;/code&gt; field is used to search for a ninja by name, and the &lt;code&gt;team&lt;/code&gt;field is used to filter the search results by team.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;choices&lt;/code&gt; attribute of the &lt;code&gt;ChoiceField&lt;/code&gt; is used to specify the available teams to filter by, and the &lt;code&gt;required=False&lt;/code&gt; attribute is used to make the field optional, so the user can search for ninjas without filtering by team.&lt;/p&gt;

&lt;h2&gt;
  
  
  Django Apps
&lt;/h2&gt;



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

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

&lt;/div&gt;



&lt;p&gt;This command will create a new directory called "uchiha-clan" in your project's root directory. This directory will contain the necessary files and directories for a Django app, such as &lt;code&gt;models.py&lt;/code&gt;, &lt;code&gt;views.py&lt;/code&gt;, &lt;code&gt;urls.py&lt;/code&gt;, &lt;code&gt;migrations/&lt;/code&gt; and &lt;code&gt;admin.py&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can then use the files in this directory to define your models, views, URLs, and other components of your app related to uchiha clan.&lt;/p&gt;

&lt;p&gt;It's important to notice that after creating the app, you should make sure to add it to the &lt;code&gt;INSTALLED_APPS&lt;/code&gt; list in the &lt;code&gt;settings.py&lt;/code&gt; file of your project so that Django knows about the new app and can use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding an app to settings.py
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Database Configuration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&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.postgresql&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;mydatabase&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;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;mydatabaseuser&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;mypassword&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;127.0.0.1&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;5432&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;/div&gt;



&lt;h3&gt;
  
  
  Timezone Configuration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;TIME_ZONE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;UTC&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Application Configuration
&lt;/h3&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="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="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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;app1&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;app2&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;app3&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;h3&gt;
  
  
  Middleware Configuration
&lt;/h3&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.middleware.security.SecurityMiddleware&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.middleware.SessionMiddleware&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.middleware.common.CommonMiddleware&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.middleware.csrf.CsrfViewMiddleware&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.middleware.AuthenticationMiddleware&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.middleware.MessageMiddleware&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.middleware.clickjacking.XFrameOptionsMiddleware&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;h3&gt;
  
  
  Allowed Hosts
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;ALLOWED_HOSTS&lt;/code&gt; setting is used to specify a list of hostnames that this Django site can serve. This is a security measure to prevent an attacker from poisoning caches and password reset emails with links to malicious hosts by submitting requests with a fake HTTP Host header, which is possible even under many seemingly-safe webserver configurations.&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;ALLOWED_HOSTS&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;example.com&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;www.example.com&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;
  
  
  Static files
&lt;/h3&gt;

&lt;p&gt;In production, you will also want to serve your static files using a separate web server, such as Apache or Nginx, rather than using the built-in Django development server. You'll need to configure your web server to serve the files in the &lt;code&gt;STATIC_ROOT&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Here's an example of how you might set &lt;code&gt;STATIC_ROOT&lt;/code&gt; and &lt;code&gt;STATIC_URL&lt;/code&gt; in your &lt;code&gt;settings.py&lt;/code&gt; 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;STATIC_ROOT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;static&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;STATIC_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;/static/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition, you might want to configure your web server to serve media files as well.&lt;/p&gt;

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

&lt;p&gt;In conclusion, mastering the art of Django can be a daunting task, but with our ninja-inspired cheatsheet, you'll be slicing and dicing your way to the top of the leaderboard in no time! Whether you're a beginner or an expert, you'll be able to unleash the full power of the Sharingan with our easy-to-use guide. Remember, just like the Uchiha clan, you too can have the power to control the nine-tailed fox (or in this case, your code) with the right training. So what are you waiting for? Grab your kunai and get coding like a true ninja!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This blogpost was originally published on &lt;a href="https://simplifiedweb.netlify.app/the-ultimate-django-cheatsheet-because-even-ninjas-need-a-little-help-sometimes" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;. You can check it out for more such posts.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>django</category>
      <category>cheatsheet</category>
      <category>python</category>
      <category>tips</category>
    </item>
  </channel>
</rss>
