<?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: KagemaNjoroge</title>
    <description>The latest articles on DEV Community by KagemaNjoroge (@kagemanjoroge).</description>
    <link>https://dev.to/kagemanjoroge</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%2F1143144%2F4b4e23e9-789b-439f-bf77-d40b9796695b.jpeg</url>
      <title>DEV Community: KagemaNjoroge</title>
      <link>https://dev.to/kagemanjoroge</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kagemanjoroge"/>
    <language>en</language>
    <item>
      <title>How One Line of Code Almost Cost My Client Their OpenAI Credits</title>
      <dc:creator>KagemaNjoroge</dc:creator>
      <pubDate>Fri, 25 Jul 2025 15:07:36 +0000</pubDate>
      <link>https://dev.to/kagemanjoroge/how-one-line-of-code-almost-cost-my-client-their-openai-credits-385c</link>
      <guid>https://dev.to/kagemanjoroge/how-one-line-of-code-almost-cost-my-client-their-openai-credits-385c</guid>
      <description>&lt;p&gt;A few months ago, I started offering consulting services as a Django developer. One of my first clients had an urgent issue:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Our OpenAI credits are draining at a crazy rate, and we don’t know why.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They suspected misuse. I dug into their codebase and deployment setup — and one of the first red flags was this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# settings.py
&lt;/span&gt;&lt;span class="n"&gt;DEBUG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Their &lt;strong&gt;production&lt;/strong&gt; server was running with debug mode enabled. &lt;/p&gt;

&lt;p&gt;It didn't take long to find something more worrying...&lt;/p&gt;

&lt;h2&gt;
  
  
  An Endpoint Was Leaking Their OpenAI API Key
&lt;/h2&gt;

&lt;p&gt;Django's debug mode is helpful during development. But in production, it exposes detailed &lt;strong&gt;stack traces&lt;/strong&gt; and &lt;strong&gt;local variable values&lt;/strong&gt; in the browser when an exception occurs.&lt;/p&gt;

&lt;p&gt;In their case, one endpoint was trying to parse JSON from a request body...but when the request was malformed(for example, accessing the endpoint via GET instead of POST, a malformed JSON body etc), it raised a &lt;code&gt;JSONDecodeError&lt;/code&gt;. Here is a simplified version of the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="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;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;JsonResponse&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dotenv&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;load_dotenv&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;

&lt;span class="nf"&gt;load_dotenv&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;# loads env variables from a .env file
&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;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;JsonResponse&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;open_ai_key&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="nf"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;OPENAI_KEY&lt;/span&gt;&lt;span class="sh"&gt;"&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="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loads&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;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;utf-8&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;# Boom on bad requests
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;JsonResponse&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;With DEBUG=True, that simple mistake triggered Django’s detailed error page, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The full stack trace&lt;/li&gt;
&lt;li&gt;Request info&lt;/li&gt;
&lt;li&gt;Local variables — including open_ai_key 😱&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s a screenshot from a proof of concept (POC) I recreated to demonstrate what was happening:&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%2Fg5ixm5l8n6nceroxma2j.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%2Fg5ixm5l8n6nceroxma2j.png" alt="Screenshot from a proof of concept (POC) " width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Fix
&lt;/h2&gt;

&lt;p&gt;I immediately advised two things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rotate the OpenAI key&lt;/strong&gt; — assume it’s compromised.&lt;/li&gt;
&lt;li&gt;Re-deploy the app with &lt;code&gt;DEBUG = False&lt;/code&gt; in &lt;code&gt;settings.py&lt;/code&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;DEBUG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With DEBUG set to false, Django shows a standard error 500 page without leaking any sensitive data.&lt;/p&gt;

&lt;p&gt;I also recommended wrapping JSON parsing in a try/except block to avoid unhandled exceptions:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try:
    data = json.loads(request.body.decode("utf-8"))
except json.JSONDecodeError:
    return JsonResponse({"error": "Invalid JSON"}, status=400)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Never deploy Django with &lt;code&gt;Django=True&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Wrap dangerous logic (like JSON parsing) in proper error handling.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What looked like a billing issue turned out to be a security hole. If you’re building with Django (or any framework), treat debug tools like loaded guns: safe in dev, dangerous in prod.&lt;/p&gt;

&lt;p&gt;Happy Django-ing 👋&lt;/p&gt;

</description>
      <category>django</category>
      <category>webdev</category>
      <category>security</category>
      <category>cybersecurity</category>
    </item>
    <item>
      <title>I met my girlfriend through a GitHub Pull Request</title>
      <dc:creator>KagemaNjoroge</dc:creator>
      <pubDate>Tue, 21 Jan 2025 11:37:46 +0000</pubDate>
      <link>https://dev.to/kagemanjoroge/i-met-my-girlfriend-through-a-github-pull-request-13p8</link>
      <guid>https://dev.to/kagemanjoroge/i-met-my-girlfriend-through-a-github-pull-request-13p8</guid>
      <description>&lt;p&gt;How cool is that?&lt;/p&gt;

</description>
      <category>coding</category>
      <category>programming</category>
      <category>jokes</category>
      <category>beginners</category>
    </item>
    <item>
      <title>3 Books every Python developer should read</title>
      <dc:creator>KagemaNjoroge</dc:creator>
      <pubDate>Thu, 20 Jun 2024 11:08:28 +0000</pubDate>
      <link>https://dev.to/kagemanjoroge/3-books-every-python-developer-should-read-3k95</link>
      <guid>https://dev.to/kagemanjoroge/3-books-every-python-developer-should-read-3k95</guid>
      <description>&lt;p&gt;Books are an incredible resource on the journey to becoming a successful programmer. Here are three that have been invaluable to me, and I believe they'll be just as beneficial for you.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;a href="https://amzn.to/3z9HCzb" rel="noopener noreferrer"&gt;Clean Code&lt;/a&gt; by Robert C. Martin
&lt;/h3&gt;

&lt;p&gt;Despite being a bit old, "Clean Code" is packed with principles that help you write clean, readable, and maintainable code. Ignore the naysayers – this book is a must-read for anyone serious about coding, regardless of the language.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. &lt;a href="https://amzn.to/3VQXOxO" rel="noopener noreferrer"&gt;The Pragmatic Programmer &lt;/a&gt;by Andrew Hunt and David Thomas
&lt;/h3&gt;

&lt;p&gt;This classic is filled with practical advice and tips that will make you a better programmer. It covers topics from debugging and testing to project management and career development. While the first two chapters may seem a bit dry, the rest is pure gold. This book’s insights are especially valuable for Python developers aiming to enhance their coding practices.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. &lt;a href="https://amzn.to/3RzExyy" rel="noopener noreferrer"&gt;Code Complete&lt;/a&gt; by Steve McConnell
&lt;/h3&gt;

&lt;p&gt;My favorite out of all these books, "Code Complete" dives deep into software construction, covering everything from design to debugging. It is filled with best practices and timeless principles. For Python developers, this book offers a comprehensive guide to writing robust and efficient code.&lt;/p&gt;

&lt;p&gt;Happy debugging! 😊&lt;/p&gt;




</description>
      <category>programming</category>
      <category>python</category>
      <category>webdev</category>
      <category>ai</category>
    </item>
    <item>
      <title>Super-charging Django: Tips &amp; Tricks</title>
      <dc:creator>KagemaNjoroge</dc:creator>
      <pubDate>Sun, 16 Jun 2024 14:43:32 +0000</pubDate>
      <link>https://dev.to/kagemanjoroge/super-charging-django-tips-tricks-24bi</link>
      <guid>https://dev.to/kagemanjoroge/super-charging-django-tips-tricks-24bi</guid>
      <description>&lt;p&gt;Django encourages rapid development and clean, pragmatic design. However, as your application scales, ensuring optimal performance becomes crucial. In this article, we'll go beyond the basics and delve deeper into advanced techniques and tools to optimize your Django application.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Database Optimization&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;a. Indexing and Query Optimization:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Proper indexing can significantly enhance query performance. Use Django’s &lt;code&gt;db_index&lt;/code&gt; and unique constraints appropriately. Analyze and optimize your SQL queries to avoid redundant data retrievals.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Hikers&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;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;db_index&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;unique_code&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;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;unique&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Monitoring tools like &lt;a href="https://www.postgresql.org/docs/current/pgstatstatements.html" rel="noopener noreferrer"&gt;&lt;code&gt;pg_stat_statements&lt;/code&gt;&lt;/a&gt; for PostgreSQL can help identify slow queries and further optimize them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b. Advanced Query Techniques:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Leverage Django's ORM capabilities with complex queries, such as &lt;code&gt;annotate()&lt;/code&gt;, &lt;code&gt;aggregate()&lt;/code&gt;, and &lt;code&gt;Subquery&lt;/code&gt; to perform calculations directly in the database, minimizing data transfer to your application.&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.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Subquery&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;OuterRef&lt;/span&gt;

&lt;span class="n"&gt;subquery&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Hiker&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;related_model_id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;OuterRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;pk&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;values&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;related_model_id&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;annotated_queryset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;RelatedModel&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;annotate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subquery&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the &lt;code&gt;Prefetch&lt;/code&gt; object to optimize querying of related objects and avoid the &lt;a href="https://dev.to/herchila/how-to-avoid-n1-queries-in-django-tips-and-solutions-2ajo"&gt;N+1 problem&lt;/a&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.db.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Prefetch&lt;/span&gt;

&lt;span class="n"&gt;prefetched_hikers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Hiker&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;prefetch_related&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Prefetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;related_model&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;queryset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;RelatedModel&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Always consider performing database operations at the lowest level:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performing operations in a QuerySet:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# QuerySet operation on the database
# fast, because that's what databases are good at
&lt;/span&gt;  &lt;span class="n"&gt;hikers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Performing operations using Python:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# counting Python objects
# slower, because it requires a database query anyway, and processing
# of the Python objects
&lt;/span&gt;&lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hikers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Performing operations using template filters:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--
Django template filter
slower still, because it will have to count them in Python anyway,
and because of template language overheads
--&amp;gt;&lt;/span&gt;
 {{ hikers|length }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;c. Database Connection Pooling:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Database connection pooling reduces the cost of opening and closing connections by maintaining a pool of open connections.&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;# settings.py
&lt;/span&gt;&lt;span class="n"&gt;DATABASES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;default&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;ENGINE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.db.backends.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;localhost&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;PORT&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;CONN_MAX_AGE&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;# Use persistent connections with a timeout
&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;Consider using third-party packages like &lt;code&gt;django-postgrespool2&lt;/code&gt; for more advanced pooling features if needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Caching Strategies&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;a. Advanced Caching Techniques:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Utilize different caching strategies like per-view caching, template fragment caching, and low-level caching to optimize your Django app. Use Redis or Memcached for fast, in-memory data storage.&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;# settings.py
&lt;/span&gt;&lt;span class="n"&gt;CACHES&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;BACKEND&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.core.cache.backends.redis.RedisCache&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;LOCATION&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;redis://127.0.0.1:6379/1&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;# views.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.views.decorators.cache&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;cache_page&lt;/span&gt;

&lt;span class="nd"&gt;@cache_page&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Cache view for 15 minutes
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="bp"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Include an example of low-level caching:&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.core.cache&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_or_set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;my_data_key&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expensive_function&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300&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;JsonResponse&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;&lt;strong&gt;b. Distributed Caching:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For large-scale applications, implement distributed caching to share cache data across multiple servers, ensuring consistency and scalability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;c. Using Cached Sessions:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For better performance, store session data using Django’s cache system. Ensure you have configured your cache system properly, particularly if using Memcached or Redis.&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;# settings.py
&lt;/span&gt;&lt;span class="n"&gt;SESSION_ENGINE&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.contrib.sessions.backends.cache&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. &lt;strong&gt;Efficient Middleware Use&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;a. Custom Middleware Optimization:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Write custom middleware efficiently, avoiding unnecessary processing. Minimize the number of middleware layers to reduce overhead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SimpleMiddleware&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;get_response&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;get_response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;get_response&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__call__&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;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;response&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="nf"&gt;get_response&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;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;

&lt;span class="c1"&gt;# settings.py
&lt;/span&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="c1"&gt;# Add only necessary middleware
&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;b. Asynchronous Middleware:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Django 3.1+ supports asynchronous views and middleware. Use async middleware for I/O-bound tasks to improve throughput.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AsyncMiddleware&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__call__&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;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;get_response&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;get_response&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;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. &lt;strong&gt;Static and Media File Optimization&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;a. Serve Static and Media Files Efficiently:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use a dedicated service like Amazon S3, or a CDN to serve static and media files, reducing load on your Django application server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b. Static File Compression and Minification:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Compress and minify your static files (CSS, JS) to reduce load times. Use tools like &lt;code&gt;django-compressor&lt;/code&gt; and &lt;code&gt;whitenoise&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# settings.py
&lt;/span&gt;&lt;span class="n"&gt;STATICFILES_STORAGE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;whitenoise.storage.CompressedManifestStaticFilesStorage&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. &lt;strong&gt;Asynchronous Processing and Task Queues&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;a. Celery for Background Tasks:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Offload long-running tasks to Celery to keep your application responsive. Configure Celery with Redis or RabbitMQ as the message broker.&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;# tasks.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;celery&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;shared_task&lt;/span&gt;

&lt;span class="nd"&gt;@shared_task&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_background_task&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;param&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Perform time-consuming task
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;param&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An example of setting up periodic tasks with Celery Beat:&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;# celery.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;celery&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Celery&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;celery.schedules&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;crontab&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Celery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;my_project&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;conf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;beat_schedule&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;task-name&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;task&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;my_app.tasks.my_periodic_task&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;schedule&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;crontab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;minute&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;hour&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="c1"&gt;# every day at midnight
&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;&lt;strong&gt;b. Async Views for High I/O Operations:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use Django’s async views to handle high I/O operations efficiently.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# Simulating a long I/O operation
&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="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;message&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;This is an async view&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="nc"&gt;JsonResponse&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;Consider integrating Django with &lt;a href="https://fastapi.tiangolo.com/" rel="noopener noreferrer"&gt;FastAPI&lt;/a&gt; for asynchronous endpoints if your application needs high-performance, non-blocking I/O operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. &lt;strong&gt;Load Balancing and Scalability&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;a. Horizontal Scaling:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Distribute your application load across multiple servers using load balancers. Compare different load balancers (e.g., Nginx vs. HAProxy) and their benefits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b. Kubernetes for Container Orchestration:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Set up Kubernetes to manage and scale your Django application.&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="c1"&gt;# Kubernetes Deployment Example&lt;/span&gt;
&lt;span class="na"&gt;apiVersion&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;apps/v1&lt;/span&gt;
&lt;span class="na"&gt;kind&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Deployment&lt;/span&gt;
&lt;span class="na"&gt;metadata&lt;/span&gt;&lt;span class="pi"&gt;:&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-deployment&lt;/span&gt;
&lt;span class="na"&gt;spec&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;replicas&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;matchLabels&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;django&lt;/span&gt;
  &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;metadata&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;labels&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;app&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;django&lt;/span&gt;
    &lt;span class="na"&gt;spec&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;containers&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&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&lt;/span&gt;
        &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;my_django_image&lt;/span&gt;
        &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;containerPort&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7. &lt;strong&gt;Monitoring and Profiling&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;a. Comprehensive Monitoring:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Implement monitoring tools like Prometheus and Grafana for real-time metrics and alerting. Set up alerts and dashboards to monitor application health proactively.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;b. In-depth Profiling:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use profiling tools like Django Debug Toolbar, Silk, and Py-Spy to analyze performance bottlenecks and optimize your code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Profiling with Py-Spy
&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;spy&lt;/span&gt; &lt;span class="n"&gt;top&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;django&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;process&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  8. &lt;strong&gt;Optimizing Template Performance&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;a. Cached Template Loader:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use Django’s cached template loader to cache compiled templates, reducing rendering time.&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;# settings.py
&lt;/span&gt;&lt;span class="n"&gt;TEMPLATES&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;BACKEND&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.template.backends.django.DjangoTemplates&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;OPTIONS&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;loaders&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.template.loaders.cached.Loader&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.template.loaders.filesystem.Loader&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;django.template.loaders.app_directories.Loader&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="p"&gt;]),&lt;/span&gt;
            &lt;span class="p"&gt;],&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;b. Efficient Template Design:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use Django’s &lt;code&gt;select_related&lt;/code&gt; and &lt;code&gt;prefetch_related&lt;/code&gt; in views to optimize data retrieval for templates. Avoid unnecessary template tags and filters to keep rendering fast.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# views.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.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;.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Hiker&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hiker_list&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;hikers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Hiker&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;select_related&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;related_model&lt;/span&gt;&lt;span class="sh"&gt;'&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="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;hikers/list.html&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;hikers&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;hikers&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Note that using &lt;code&gt;{% block %}&lt;/code&gt; is faster than using &lt;code&gt;{% include %}&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  9. &lt;strong&gt;Advanced Python Techniques&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;a. Lazy Evaluation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Utilize Django’s &lt;code&gt;@cached_property&lt;/code&gt; decorator for caching expensive computations within a model instance.&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.utils.functional&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;cached_property&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyModel&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="nd"&gt;@cached_property&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;expensive_property&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="nf"&gt;expensive_computation&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;b. Using PyPy:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://www.pypy.org/" rel="noopener noreferrer"&gt;PyPy&lt;/a&gt; is an alternative Python implementation that can execute code faster for CPU-bound operation&lt;/p&gt;

&lt;p&gt;Consider using PyPy for performance improvements, but be aware of compatibility issues with certain Django dependencies. Test thoroughly before deploying to production.&lt;/p&gt;

&lt;h3&gt;
  
  
  10. &lt;strong&gt;Using the Latest Version of Django&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Always use the latest version of Django to benefit from performance improvements, security patches, and new features. Keep your dependencies updated alongside Django for optimal performance and security.&lt;/p&gt;

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

&lt;p&gt;Remember, optimization is an ongoing process. Continuously monitor your application’s performance and be proactive in identifying and addressing bottlenecks. Happy coding! 😊&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Django Admin - Zero to Hero</title>
      <dc:creator>KagemaNjoroge</dc:creator>
      <pubDate>Sun, 26 May 2024 15:01:56 +0000</pubDate>
      <link>https://dev.to/kagemanjoroge/django-admin-zero-to-hero-4lp6</link>
      <guid>https://dev.to/kagemanjoroge/django-admin-zero-to-hero-4lp6</guid>
      <description>&lt;p&gt;One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site.&lt;br&gt;
The admin’s recommended use is limited to an organization’s internal management tool. It’s not intended for building your entire front end around. In this article, we will cover everything you need to know to become a Django Admin pro. We will start from scratch and gradually move to advanced topics.&lt;br&gt;
By the end of this article, you will be able to manage your Django project efficiently using Django Admin.&lt;br&gt;
This will be a long article, feel free to jump to the section you are interested in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Activating Django Admin&lt;/li&gt;
&lt;li&gt;  Creating Superuser&lt;/li&gt;
&lt;li&gt;  Accessing Django Admin Site&lt;/li&gt;
&lt;li&gt;  An Overview of Django Admin Interface&lt;/li&gt;
&lt;li&gt;  Creating and registering models&lt;/li&gt;
&lt;li&gt;  ModelAdmin class&lt;/li&gt;
&lt;li&gt;  ModelAdmin Actions&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Activating Django Admin
&lt;/h3&gt;

&lt;p&gt;The admin is enabled in the default project template used by startproject. If you’re not using the default project template, here are the requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;    Add &lt;code&gt;django.contrib.admin&lt;/code&gt; and its dependencies - &lt;code&gt;django.contrib.auth&lt;/code&gt;, &lt;code&gt;django.contrib.contenttypes&lt;/code&gt;, &lt;code&gt;django.contrib.messages&lt;/code&gt;, and &lt;code&gt;django.contrib.sessions&lt;/code&gt; - to your INSTALLED_APPS setting.&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;admin.site.urls&lt;/code&gt; to your URLconf. Modify the &lt;code&gt;urls.py&lt;/code&gt; file in your project folder to include the following line:
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;admin/&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;urls&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Creating  Superuser account
&lt;/h3&gt;

&lt;p&gt;To access the Django admin, you need to create a superuser. Run the following command in your terminal:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;You will be prompted to enter a username, email address, and password for the superuser.&lt;/p&gt;

&lt;h3&gt;
  
  
  Accessing Django Admin Site
&lt;/h3&gt;

&lt;p&gt;Run the Django development server:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;By default, Django development server will run on port 8000. Open your browser and navigate to &lt;a href="http://localhost:8000/admin/" rel="noopener noreferrer"&gt;http://localhost:8000/admin/&lt;/a&gt;. You will see the Django admin login page.&lt;br&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%2Frrydcrd2wywvdzhi2orx.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%2Frrydcrd2wywvdzhi2orx.png" alt="Django Admin Page" width="800" height="460"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enter the superuser credentials you created earlier to log in.&lt;/p&gt;
&lt;h3&gt;
  
  
  An Overview of Django Admin Interface
&lt;/h3&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%2Fml1p7nph7ld538xqym5y.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%2Fml1p7nph7ld538xqym5y.png" alt="Django Admin site overview" width="800" height="287"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Without any other registered models, Django will display the “Groups” and “Users” models. These are part of Django’s authentication framework. You can use these models to manage users and groups in your Django project. Please note how they are categorized under the “Authentication and Authorization” section.&lt;/p&gt;
&lt;h3&gt;
  
  
  Creating and registering models
&lt;/h3&gt;

&lt;p&gt;In this article let's work with the &lt;code&gt;Books model&lt;/code&gt; located in the &lt;code&gt;books&lt;/code&gt; app. The &lt;code&gt;Books model&lt;/code&gt; has the following fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;title&lt;/code&gt; - The title of the book.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;author&lt;/code&gt; - The author of the book.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;price&lt;/code&gt; - The price of the book.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;published&lt;/code&gt; - A boolean field to indicate if the book has been published.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Assuming you have already created the &lt;code&gt;books&lt;/code&gt; app, let's create the &lt;code&gt;Books model&lt;/code&gt; in the &lt;code&gt;models.py&lt;/code&gt; file of the &lt;code&gt;books&lt;/code&gt; app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Model&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;models&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CharField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_length&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;price&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;DecimalField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;max_digits&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decimal_places&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;published&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="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;title&lt;/span&gt; &lt;span class="c1"&gt;# Display the title of the book in the Django admin site
&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="c1"&gt;# the attributes below are very useful for the Django admin interface, they are not required but are good practice
&lt;/span&gt;        &lt;span class="n"&gt;verbose_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;Book&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;# Singular name for the model
&lt;/span&gt;        &lt;span class="n"&gt;verbose_name_plural&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Books&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="c1"&gt;# Plural name for the model
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In your &lt;code&gt;admin.py&lt;/code&gt; file of the &lt;code&gt;books&lt;/code&gt; app, register the &lt;code&gt;Books model&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;.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt;
&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save the file and make migrations:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Restart the Django development server:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Head over to the Django admin site and you will see the &lt;code&gt;Books&lt;/code&gt; model listed under the &lt;code&gt;Books&lt;/code&gt; section. You can now add, edit, and delete books using the Django admin interface.&lt;br&gt;
Make sure to create some books using the Django admin interface.&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%2F1q0fxf23s5lozp0xhu8g.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%2F1q0fxf23s5lozp0xhu8g.png" alt="Books Model as seen in the Django Admin site" width="800" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How cool is that?&lt;/p&gt;
&lt;h2&gt;
  
  
  A little further - ModelAdmin class
&lt;/h2&gt;

&lt;p&gt;It is possible to register a model with just the &lt;code&gt;admin.site.register(ModelName)&lt;/code&gt; method.&lt;br&gt;&lt;br&gt;
Perhaps you need more control over how the model is displayed in the admin interface. This is where the &lt;code&gt;ModelAdmin&lt;/code&gt; class comes in.&lt;br&gt;&lt;br&gt;
The &lt;code&gt;ModelAdmin&lt;/code&gt; class is the representation of a model in the admin interface. It allows you to customize how the model is displayed, how it can be interacted with, and how it can be filtered.&lt;br&gt;&lt;br&gt;
You can customize the &lt;code&gt;ModelAdmin&lt;/code&gt; class by creating a class that inherits from &lt;code&gt;admin.ModelAdmin&lt;/code&gt; and then registering it with the model.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In our case, we want to display the &lt;code&gt;Books&lt;/code&gt; model with a list of books sorted by the price in ascending order. We also want to display the &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;author&lt;/code&gt; fields in the list view.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's create a &lt;code&gt;BookAdmin&lt;/code&gt; class in the &lt;code&gt;admin.py&lt;/code&gt; file of the &lt;code&gt;books&lt;/code&gt; app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BookAdmin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ModelAdmin&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;list_display&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;title&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;author&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Display title and author fields in the list view
&lt;/span&gt;    &lt;span class="n"&gt;ordering&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;price&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt; &lt;span class="c1"&gt;# Sort by price in ascending order
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Register the &lt;code&gt;BookAdmin&lt;/code&gt; class with the &lt;code&gt;Books&lt;/code&gt; model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BookAdmin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the end the &lt;code&gt;admin.py&lt;/code&gt; file of the &lt;code&gt;books&lt;/code&gt; app should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.contrib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;admin&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BookAdmin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ModelAdmin&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;list_display&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;title&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;author&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Display title and author fields in the list view
&lt;/span&gt;    &lt;span class="n"&gt;ordering&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;price&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt; &lt;span class="c1"&gt;# Sort by price in ascending order
&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BookAdmin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Head over to the Django admin site and you will see the &lt;code&gt;Books&lt;/code&gt; model listed under the &lt;code&gt;Books&lt;/code&gt; section. The books will be displayed with the &lt;code&gt;title&lt;/code&gt; and &lt;code&gt;author&lt;/code&gt; fields in the list view, sorted by price in ascending order.&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%2F3i14uaxu3mast4ndhqvo.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%2F3i14uaxu3mast4ndhqvo.png" alt="Books in Django admin site" width="800" height="395"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let me teach you an even concise way to register the &lt;code&gt;ModelAdmin&lt;/code&gt; using the &lt;code&gt;@admin.register&lt;/code&gt; decorator.&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="nd"&gt;@admin.register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Book&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;BookAdmin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ModelAdmin&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;list_display&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;title&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;author&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Display title and author fields in the list view
&lt;/span&gt;    &lt;span class="n"&gt;ordering&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;price&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt; &lt;span class="c1"&gt;# Sort by price in ascending order
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code is equivalent to the previous code where we registered the &lt;code&gt;BookAdmin&lt;/code&gt; class with the &lt;code&gt;Books&lt;/code&gt; model but in a more concise way.&lt;/p&gt;

&lt;h2&gt;
  
  
  ModelAdmin Actions
&lt;/h2&gt;

&lt;p&gt;If you have noticed, selecting multiple books provides a dropdown with the action "Delete selected books". This is an example of a &lt;code&gt;ModelAdmin&lt;/code&gt; 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%2F0l13gbyq0occ1j3291hv.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%2F0l13gbyq0occ1j3291hv.png" alt="Model actions in Django admin site" width="800" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That sounds limited, right? What if you want to create your own custom actions?&lt;/p&gt;

&lt;h3&gt;
  
  
  Custom ModelAdmin Actions
&lt;/h3&gt;

&lt;p&gt;The easiest way to explain actions is by example, so let’s dive in.&lt;br&gt;
A common use case for admin actions is the bulk updating of a model’s fields.&lt;br&gt;
To create an action, you need to define a method in the &lt;code&gt;ModelAdmin&lt;/code&gt; class and decorate it with the &lt;code&gt;@admin.action&lt;/code&gt; decorator.&lt;/p&gt;

&lt;p&gt;Let's create a custom action to mark selected books as published.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BookAdmin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ModelAdmin&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;list_display&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;title&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;author&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="c1"&gt;# Display title and author fields in the list view
&lt;/span&gt;    &lt;span class="n"&gt;ordering&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;price&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,)&lt;/span&gt;  &lt;span class="c1"&gt;# Sort by price in ascending order
&lt;/span&gt;    &lt;span class="n"&gt;actions&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;mark_as_published&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# Add the custom action
&lt;/span&gt;
    &lt;span class="nd"&gt;@admin.action&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Mark selected books as published&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;mark_as_published&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;request&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="n"&gt;queryset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;published&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;admin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;site&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;BookAdmin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2Fllk2naonkdjpmnjy8qg2.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%2Fllk2naonkdjpmnjy8qg2.png" alt="Custom ModelAdmin actions in Django" width="800" height="196"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Admin model actions take three arguments: &lt;code&gt;self&lt;/code&gt;, &lt;code&gt;request&lt;/code&gt;, and &lt;code&gt;queryset&lt;/code&gt;. The &lt;code&gt;queryset&lt;/code&gt; argument is a list of all the selected objects. In our case, it will be a list of selected books.&lt;/p&gt;

&lt;p&gt;That is just a tip of the iceberg. There is so much more you can do with Django Admin. I hope this article has given you a good foundation to start exploring Django Admin. Feel free to explore the &lt;a href="https://docs.djangoproject.com/en/5.0/ref/contrib/admin/" rel="noopener noreferrer"&gt;official Django documentation&lt;/a&gt; to learn more about Django Admin.&lt;/p&gt;

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

</description>
      <category>django</category>
      <category>webdev</category>
      <category>python</category>
    </item>
    <item>
      <title>Are you still exposing your Django SECRET_KEY?</title>
      <dc:creator>KagemaNjoroge</dc:creator>
      <pubDate>Mon, 20 May 2024 22:05:27 +0000</pubDate>
      <link>https://dev.to/kagemanjoroge/are-you-still-exposing-your-django-secretkey-4enh</link>
      <guid>https://dev.to/kagemanjoroge/are-you-still-exposing-your-django-secretkey-4enh</guid>
      <description>&lt;p&gt;Django &lt;code&gt;settings.py&lt;/code&gt; contains the most sensitive information of your project. Hard coding key details such as &lt;code&gt;SECRET_KEY&lt;/code&gt;,&lt;code&gt;ALLOWED_HOSTS&lt;/code&gt;, database settings such &lt;code&gt;database password&lt;/code&gt;, &lt;code&gt;database host&lt;/code&gt;, &lt;code&gt;database port&lt;/code&gt;, &lt;code&gt;database username&lt;/code&gt; in your codebase is a security risk. In this article, we will discuss how to secure your Django &lt;code&gt;SECRET_KEY&lt;/code&gt; by using environment variables and the python &lt;code&gt;dotenv&lt;/code&gt; library. While I will focus on the &lt;code&gt;SECRET_KEY&lt;/code&gt;, the same principles can be applied to other sensitive information in your Django project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is the Django &lt;code&gt;SECRET_KEY&lt;/code&gt; important?
&lt;/h2&gt;

&lt;p&gt;Django uses the &lt;code&gt;SECRET_KEY&lt;/code&gt; to provide cryptographic signing, protection against CSRF attacks, and other security features. If an attacker gains access to your &lt;code&gt;SECRET_KEY&lt;/code&gt;, they can potentially compromise the security of your Django project. It is crucial to keep the &lt;code&gt;SECRET_KEY&lt;/code&gt; confidential and secure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing the &lt;code&gt;python-dotenv&lt;/code&gt; library
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;python-dotenv&lt;/code&gt; library allows you to load environment variables from a &lt;code&gt;.env&lt;/code&gt; file into your Django project. To install the &lt;code&gt;python-dotenv&lt;/code&gt; library, you can use &lt;code&gt;pip&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;If you do have a &lt;code&gt;requirements.txt&lt;/code&gt; file, you can add the &lt;code&gt;python-dotenv&lt;/code&gt; library to it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a &lt;code&gt;.env&lt;/code&gt; file
&lt;/h2&gt;

&lt;p&gt;Create a &lt;code&gt;.env&lt;/code&gt; file in the root directory(the same directory as manage.py) of your Django project. Add the &lt;code&gt;SECRET_KEY&lt;/code&gt; to the &lt;code&gt;.env&lt;/code&gt; file in the following format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;SECRET_KEY&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"your_secret_key_here"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding the &lt;code&gt;.env&lt;/code&gt; file to &lt;code&gt;.gitignore&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;.env&lt;/code&gt; file contains sensitive information, and you should not commit it to your version control system. To prevent the &lt;code&gt;.env&lt;/code&gt; file from being accidentally committed, add it to your &lt;code&gt;.gitignore&lt;/code&gt; file. Here is an example of how you can add the &lt;code&gt;.env&lt;/code&gt; file to your &lt;code&gt;.gitignore&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# .gitignore&lt;/span&gt;
.env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Loading the &lt;code&gt;SECRET_KEY&lt;/code&gt; from the &lt;code&gt;.env&lt;/code&gt; file
&lt;/h2&gt;

&lt;p&gt;In your &lt;code&gt;settings.py&lt;/code&gt; file, you will need to import the &lt;code&gt;os&lt;/code&gt; and &lt;code&gt;dotenv&lt;/code&gt; modules and load the &lt;code&gt;SECRET_KEY&lt;/code&gt; from the &lt;code&gt;.env&lt;/code&gt; file. Here is an example of how you can 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;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;dotenv&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;load_dotenv&lt;/span&gt;

&lt;span class="nf"&gt;load_dotenv&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;load_dotenv()&lt;/code&gt; will load the environment variables from the &lt;code&gt;.env&lt;/code&gt; file into your Django project. After reading from the .env file, &lt;code&gt;load_dotenv()&lt;/code&gt; will update the &lt;code&gt;os.environ&lt;/code&gt; dictionary with the key-value pairs from the .env file.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is important to call &lt;code&gt;load_dotenv()&lt;/code&gt; before accessing the &lt;code&gt;SECRET_KEY&lt;/code&gt; in your &lt;code&gt;settings.py&lt;/code&gt; file.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Accessing the &lt;code&gt;SECRET_KEY&lt;/code&gt; in your &lt;code&gt;settings.py&lt;/code&gt; file
&lt;/h2&gt;

&lt;p&gt;Now that you have loaded the &lt;code&gt;SECRET_KEY&lt;/code&gt; from the &lt;code&gt;.env&lt;/code&gt; file, you can access it in your &lt;code&gt;settings.py&lt;/code&gt; file using the &lt;code&gt;os&lt;/code&gt; module:&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;SECRET_KEY&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="nf"&gt;getenv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;SECRET_KEY&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;By using &lt;code&gt;os.getenv('SECRET_KEY')&lt;/code&gt;, you can access the &lt;code&gt;SECRET_KEY&lt;/code&gt; value stored in the &lt;code&gt;.env&lt;/code&gt; file. This ensures that the &lt;code&gt;SECRET_KEY&lt;/code&gt; is not hard-coded in your codebase and is kept secure.&lt;/p&gt;

&lt;p&gt;Please note the above steps can be applied to other sensitive information in your Django project.&lt;/p&gt;

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

</description>
      <category>python</category>
      <category>django</category>
    </item>
    <item>
      <title>Why You Shouldn’t Be Worried About AI</title>
      <dc:creator>KagemaNjoroge</dc:creator>
      <pubDate>Fri, 08 Mar 2024 14:13:57 +0000</pubDate>
      <link>https://dev.to/kagemanjoroge/why-you-shouldnt-be-worried-about-ai-1p91</link>
      <guid>https://dev.to/kagemanjoroge/why-you-shouldnt-be-worried-about-ai-1p91</guid>
      <description>&lt;p&gt;Let’s be honest, when the topic of AI comes up, a lot of us jump straight to those sci-fi disaster movies. Robots with glowing red eyes, AI taking over the world — it’s the stuff of nightmares! And thanks to endless headlines about AI doing amazing (and sometimes scary) things, it’s easy to feel a little uneasy.&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%2F9ep38cw0fna3znp6149s.jpeg" 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%2F9ep38cw0fna3znp6149s.jpeg" alt="Artificial Intelligence" width="800" height="450"&gt;&lt;/a&gt;&lt;br&gt;
But here’s the thing: AI is not some self-aware villain waiting to happen. It’s a tool, and a staggeringly powerful one, but a tool nonetheless. Think of it like this: a hammer can be used to build a house or to cause harm. It’s the intent that matters, not the tool itself.&lt;/p&gt;

&lt;p&gt;The reality is, AI is already woven into our lives. It recommends movies for you to watch, helps doctors spot diseases earlier, and even helps with tasks like writing emails. It’s not out to get you — it’s out to make things easier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Misconceptions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Of course, it’s wise to be aware of the potential pitfalls of AI, just like any technology. Concerns about bias, misuse, and job displacement are valid. One prevalent fear is that AI will inevitably lead to widespread job loss and economic upheaval. While it’s true that AI may disrupt certain industries, history has shown that technological advancements ultimately create more jobs than they eliminate.&lt;/p&gt;

&lt;p&gt;For instance, while AI may automate routine tasks, it also creates opportunities for new types of jobs. Roles such as AI ethicists, data privacy officers, and AI trainers are emerging as crucial components of the AI workforce. These jobs require human skills such as critical thinking, creativity, and empathy, which AI cannot replicate.&lt;/p&gt;

&lt;p&gt;As Andrej Karpathy, wisely puts it, “We should see AI as a copilot.” It’s a perfect analogy: AI isn’t replacing us, it’s there to augment our abilities. The future belongs to those who adapt, who see how AI can augment what they do — not do it for them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Role of Ethics and Regulation &amp;amp; the future of AI&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s be real — safeguards are needed. Just like we don’t let just anyone build a nuclear power plant, we need sensible AI regulations. Global cooperation on this is vital. Think about it: we need agreements to prevent the use of autonomous AI weapons and ensure that these powerful tools are used ethically. If we do not have AI regulations, some armies of the world might take AI to the battlefield. Regulation isn’t about stifling innovation; it’s about making sure AI serves the greater good.&lt;/p&gt;

&lt;p&gt;AI can evoke a sense of unease. However, I argue that rather than fearing AI, we should embrace its potential as a transformative force for good.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;What are your thoughts on AI? Do you find the possibilities exciting, the potential risks concerning, or both?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Why do people think coding is difficult?</title>
      <dc:creator>KagemaNjoroge</dc:creator>
      <pubDate>Tue, 05 Mar 2024 15:37:39 +0000</pubDate>
      <link>https://dev.to/kagemanjoroge/why-do-people-think-coding-is-difficult-273j</link>
      <guid>https://dev.to/kagemanjoroge/why-do-people-think-coding-is-difficult-273j</guid>
      <description>&lt;p&gt;Simple amateur coding is trivial and most people can do it. Probably making ten or twenty lines of code in a programming language is something most people could do I think.&lt;/p&gt;

&lt;p&gt;Factors mess that up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Figuring out what to code. You just get asked, say, ‘Can you make it email me if more than five expense claims get rejected on the same day for the same person?’. How do you code that?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fitting it in to existing code. You have to figure out how to fit your new code in to the existing code without breaking it. It might be millions of lines you have never seen. it might be very badly constructed and might break on small changes. That bit is hard.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And then, we want to do better as professionals.&lt;/p&gt;

&lt;p&gt;We want our code to have automated tests, which changes how you design it a bit.&lt;/p&gt;

&lt;p&gt;We want it to be easy to deploy to computers, so you have to design that in.&lt;/p&gt;

&lt;p&gt;We want it free from security holes, to keep data safe.&lt;/p&gt;

&lt;p&gt;We want to never corrupt any data.&lt;/p&gt;

&lt;p&gt;We want our colleagues to easily read and easily change our code, which needs designing in.&lt;/p&gt;

&lt;p&gt;We want our code to be robust and correctly handle bad data, bad user input and anything else &lt;code&gt;unexpected&lt;/code&gt;. We have to learn to expect that.&lt;/p&gt;

&lt;p&gt;Those bits are pretty hard, too.&lt;/p&gt;

&lt;p&gt;Then, our bosses want to know when all that is going to be ready, so they can plan around it. Once the TV advert goes live, that software better be running.&lt;/p&gt;

&lt;p&gt;That’s hard.&lt;/p&gt;

&lt;p&gt;We have to juggle competing priorities of adding features, managing tech debt and fixing bugs.&lt;/p&gt;

&lt;p&gt;There’s no right answer for that. Somebody is always disappointed.&lt;/p&gt;

&lt;p&gt;We have to cope with floods of new users and feature requests that are difficult to build. The whole &lt;code&gt;scalability&lt;/code&gt; piece is hard.&lt;/p&gt;

&lt;p&gt;The code needs to stay running 24/7 or close to it - even if half the computers, disk drives, cooling systems, internet switches and electrical power fails.&lt;/p&gt;

&lt;p&gt;That adds a bit of trickiness for sure.&lt;/p&gt;

&lt;p&gt;We need to use existing libraries wherever we can, so we need to understand code we’ve not written. Tricky. But trickier still, we need to be part-time lawyers, because we have to check we are legally allowed to use that code without it affecting the business. Is it Creative Commons License or is it GNU?, that's another level of complexity.&lt;/p&gt;

&lt;p&gt;We need to remember somebody pays for all the computing our code runs on, so we have to make it play nice.&lt;/p&gt;

&lt;p&gt;We have to work for and with people who range from fantastic humans to the sort you’d rather not spend time with. And with all ranges of skill levels.&lt;/p&gt;

&lt;p&gt;That’s a real mixed bag.&lt;/p&gt;

&lt;p&gt;We have to know so much about computing, engineering, the problem area we are solving, testing.&lt;/p&gt;

&lt;p&gt;That’s the really hard bit.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Django Framework is Dead 😵</title>
      <dc:creator>KagemaNjoroge</dc:creator>
      <pubDate>Mon, 04 Mar 2024 22:26:22 +0000</pubDate>
      <link>https://dev.to/kagemanjoroge/django-framework-is-dead-5bm6</link>
      <guid>https://dev.to/kagemanjoroge/django-framework-is-dead-5bm6</guid>
      <description>&lt;p&gt;Ladies and gentlemen, gather 'round, for I bring grave news from the world of Python web development. It is with a heavy heart that I declare the passing of our dear friend, Django framework.&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%2F8esmtwufptte8he6agx5.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%2F8esmtwufptte8he6agx5.png" alt="Django Web Framework" width="800" height="278"&gt;&lt;/a&gt;&lt;br&gt;
But before you shed a tear or organize a funeral procession, let me assure you – Django is very much alive and kicking!&lt;/p&gt;

&lt;p&gt;Yes, you read that correctly. Despite the ominous title, Django is far from dead. In fact, it's thriving more than ever, and here's why.&lt;/p&gt;

&lt;p&gt;As we bid adieu to the era of monolithic frameworks and embrace the brave new world of micro services and serverless architecture, &lt;em&gt;some may have prematurely written off Django as a relic of the past&lt;/em&gt;. But let me remind you of Django's timeless virtues – its elegance, &lt;strong&gt;its simplicity, its "batteries-included" philosophy that has endeared it to developers for decades&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;While other frameworks may come and go, Django stands the test of time. Its robust ORM, its sleek admin interface, its powerful authentication system – these are not features of a dying framework, but the hallmarks of a mature, battle-tested tool-set that continues to evolve with the times.&lt;/p&gt;

&lt;p&gt;So, why the provocative title, you ask? Well, dear reader, it's all about grabbing your attention and sparking a conversation. Sometimes a little irony is the best way to cut through the noise and make a point.&lt;/p&gt;

&lt;p&gt;And the point is this – Django may not be the shiny new toy on the block, but it's the dependable workhorse that gets the job done. Whether you're building a simple blog or a complex enterprise application, &lt;strong&gt;Django has your back&lt;/strong&gt;, providing a solid foundation upon which to build your dreams.&lt;/p&gt;

&lt;p&gt;So, let's raise a toast to Django – not in mourning, but in celebration of its enduring legacy and the countless projects it has powered over the years. If you are a perfectionist with a deadline to meet, you should definitely try Django. Long live Django!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>python</category>
      <category>django</category>
      <category>programming</category>
    </item>
    <item>
      <title>What are the differences between Django and Flask?</title>
      <dc:creator>KagemaNjoroge</dc:creator>
      <pubDate>Mon, 04 Mar 2024 22:03:13 +0000</pubDate>
      <link>https://dev.to/kagemanjoroge/what-are-the-differences-between-django-and-flask-19c7</link>
      <guid>https://dev.to/kagemanjoroge/what-are-the-differences-between-django-and-flask-19c7</guid>
      <description>&lt;p&gt;Django and Flask are both popular Python web frameworks, each with its own strengths and use cases. Here are some key differences between them:&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%2Fwawfwhwtnm7fbey9wipq.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%2Fwawfwhwtnm7fbey9wipq.png" alt="Django vs Flask" width="800" height="337"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Complexity and Learning Curve&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Django is a full-stack framework that follows the "batteries-included" philosophy, providing a wide range of built-in functionalities such as authentication, admin site, ORM, and URL routing out of the box. While this makes Django powerful and feature-rich, it also contributes to a steeper learning curve, especially for beginners.&lt;/li&gt;
&lt;li&gt;Flask, on the other hand, is a lightweight micro-framework that offers more flexibility and minimalism. It provides only the essentials for building web applications, allowing developers to choose and integrate additional libraries or extensions as needed. Flask's simplicity makes it easier to pick up and learn, making it a preferred choice for smaller projects or developers new to web development.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Flexibility and Customization&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Flask's minimalist approach allows for greater flexibility and customization compared to Django. Developers have more control over the architecture and components of their applications, making Flask suitable for building APIs, micro-services, and other lightweight web applications where fine-grained control is desired.&lt;/li&gt;
&lt;li&gt;Django, while less flexible in terms of its predefined structure, offers a more opinionated approach to web development, which can be advantageous for larger projects or teams. Its built-in components and conventions help maintain consistency and scalability across projects, making it well-suited for complex, enterprise-level applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Industry Adoption and Community Support&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Django is widely considered an industry standard for Python web development, with a large and active community of developers, extensive documentation, and a rich ecosystem of third-party packages and extensions. Its robust feature set, stability, and scalability have made it a popular choice for a wide range of web applications, including content management systems, e-commerce platforms, and social networks.&lt;/li&gt;
&lt;li&gt;Flask, while not as ubiquitous as Django, also enjoys significant adoption and community support. It is favored for its simplicity, flexibility, and ease of integration with other tools and libraries. Flask's lightweight nature and extensibility make it particularly well-suited for rapid prototyping, small to medium-sized projects, and applications requiring specialized functionality.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Django's comprehensive feature set and industry-standard status make it a solid choice for large-scale applications requiring stability and scalability. However, Flask's simplicity, flexibility, and ease of learning make it an excellent option for smaller projects, prototyping, and developers seeking more control over their application architecture. Ultimately, the choice between Django and Flask depends on the specific requirements, complexity, and constraints of your project. Read my article on &lt;a href="https://dev.to/kagemanjoroge/django-the-framework-that-will-ruin-your-life-55ab"&gt;why Django can ruin your life&lt;/a&gt; for more dev stories about Django.&lt;/p&gt;

</description>
      <category>django</category>
      <category>python</category>
      <category>backenddevelopment</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The Day I Realized I Was a Noob Dev: Lessons from an Anagram Mistake</title>
      <dc:creator>KagemaNjoroge</dc:creator>
      <pubDate>Thu, 22 Feb 2024 11:32:53 +0000</pubDate>
      <link>https://dev.to/kagemanjoroge/the-day-i-realized-i-was-a-noob-dev-lessons-from-an-anagram-mistake-nab</link>
      <guid>https://dev.to/kagemanjoroge/the-day-i-realized-i-was-a-noob-dev-lessons-from-an-anagram-mistake-nab</guid>
      <description>&lt;p&gt;Every programmer has their moments of humility, and I had mine on a day when I thought I had it all figured out. It all started with a seemingly simple programming problem - checking if two strings were anagrams. Little did I know that this challenge would reveal just how much I had to learn.&lt;br&gt;
&lt;strong&gt;The Problem That Fooled Me&lt;/strong&gt;&lt;br&gt;
The problem was straightforward: &lt;code&gt;Given two strings, s and t, determine if t is an anagram of s&lt;/code&gt;. &lt;br&gt;
An anagram, as we all know, is a word or phrase formed by rearranging the letters of another word or phrase, typically using all the original letters exactly once. Piece of cake, right?&lt;/p&gt;

&lt;p&gt;Here's the question in its entirety:&lt;br&gt;
&lt;a href="https://leetcode.com/problems/valid-anagram/" rel="noopener noreferrer"&gt;LeetCode link to the problem&lt;/a&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="sh"&gt;'''&lt;/span&gt;&lt;span class="s"&gt;
Given two strings s and t, return true if t is an anagram of s, 
and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters
of a different word or phrase, typically using all the original
letters exactly once.

Example 1:

Input: s = &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;anagram&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;, t = &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;nagaram&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;
Output: true
Example 2:

Input: s = &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;rat&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;, t = &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;car&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;
Output: false


Constraints:

1 &amp;lt;= s.length, t.length &amp;lt;= 5 * 104
s and t consist of lowercase English letters.

&lt;/span&gt;&lt;span class="sh"&gt;'''&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;My Initial Solution&lt;/strong&gt;&lt;br&gt;
I dived into the problem headfirst, convinced that I could conquer it effortlessly. My initial solution? Compare the two strings character by character and return &lt;code&gt;True&lt;/code&gt; if they were the same length. Simple enough, or so I thought:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;anagram&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&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;False&lt;/span&gt;

    &lt;span class="n"&gt;data_t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
    &lt;span class="n"&gt;data_s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;data_s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;data_t&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data_s&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;data_t&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The "Aha! 😒" Moment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I ran my code, and it seemed to work for some test cases. I felt a sense of accomplishment. But then reality hit me like a ton of bricks. What about the characters' counts? An anagram isn't just about having the same characters; it's about having the same characters in the same quantities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Big Mistake&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;My "brilliant" solution had completely overlooked this crucial aspect. I realized that my code would incorrectly declare strings like "rat" and "car" as anagrams simply because they contained the same characters.&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%2Fii21zhs069fv3fufa3ay.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%2Fii21zhs069fv3fufa3ay.png" alt="LeetCode anagram challenge" width="794" height="722"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I had rushed into solving the problem without fully understanding its requirements. But there's no shame in making mistakes; the real shame is not learning from them.&lt;br&gt;
The Revised Solution&lt;/p&gt;

&lt;p&gt;I knew I had to fix my code. I learned about the importance of character counts in anagrams and came up with a revised solution that properly addressed this aspect.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;anagram&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&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;False&lt;/span&gt;

    &lt;span class="n"&gt;char_count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;char&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;s&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;char&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;char_count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;char_count&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&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;char_count&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;char&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;t&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;char&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;char_count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;char_count&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;char_count&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;char&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&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;False&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="bp"&gt;False&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Lesson&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;never underestimate the importance of understanding the problem fully&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Go ahead and tell me about the day you knew that you had to work on you programming skills.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>python</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Character vs. Word Tokenization in NLP: Unveiling the Trade-Offs in Model Size, Parameters, and Compute</title>
      <dc:creator>KagemaNjoroge</dc:creator>
      <pubDate>Thu, 22 Feb 2024 11:09:06 +0000</pubDate>
      <link>https://dev.to/kagemanjoroge/character-vs-word-tokenization-in-nlp-unveiling-the-trade-offs-in-model-size-parameters-and-compute-3jjh</link>
      <guid>https://dev.to/kagemanjoroge/character-vs-word-tokenization-in-nlp-unveiling-the-trade-offs-in-model-size-parameters-and-compute-3jjh</guid>
      <description>&lt;p&gt;In Natural Language Processing, the choice of tokenization method can make or break a model. Join me on a journey to understand the profound impact of character-level, word-level tokenization and Sub-word tokenization on model size, number of parameters, and computational complexity.&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%2Fdgeq41t7cp99j0n6yqrd.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%2Fdgeq41t7cp99j0n6yqrd.jpg" alt="Alt Text" width="800" height="648"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First Things First, What is Tokenization?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;AI operates with numbers, and for deep learning on text, we convert that text into numerical data. Tokenization breaks down textual data into smaller units, or tokens, like words or characters, which can be represented numerically.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Decoding Tokenization&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
There are various techniques in tokenization, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Word Tokenization:&lt;/strong&gt; Divides text into words, creating a vocabulary of unique terms.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Character Tokenization:&lt;/strong&gt; Breaks down text into individual characters, useful for specific tasks like morphological analysis.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sub word Tokenization:&lt;/strong&gt; Splits words into smaller units, capturing morphological information effectively. Examples &lt;a href="https://huggingface.co/docs/transformers/model_doc/bert" rel="noopener noreferrer"&gt;BERT&lt;/a&gt; and &lt;a href="https://github.com/google/sentencepiece" rel="noopener noreferrer"&gt;SentencePiece&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Character-Level Tokenization&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We essentially create tokens out of individual characters present in the text. This involves compiling a unique set of characters found in the dataset, ranging from alphanumeric characters to ASCII symbols. By breaking down the text into these elemental units, we generate a concise set of tokens, resulting in a smaller number of model parameters. This lean approach is particularly advantageous in scenarios with &lt;code&gt;limited datasets, such as low-resource languages&lt;/code&gt;, where it can efficiently capture patterns without overwhelming the model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Consider the sentence, "I love Python"
# If we tokenize by characters the result will be ['I', ' ', 'l', 'o', 'v', 'e', ' ', 'P', 'y', 't', 'h', 'n']
&lt;/span&gt;&lt;span class="n"&gt;sent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I love Python&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sent&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;# Use a set to obtain ony unique ones
# if we then represent the sentence above numerically
&lt;/span&gt;&lt;span class="n"&gt;numerical_representation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tokens&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;span class="n"&gt;number_of_tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Word-Level Tokenization:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Word-level tokenization involves breaking down the text into individual words. This process results in a vocabulary composed of unique terms present in the dataset. Unlike character-level tokenization, which deals with individual characters, word-level tokenization operates at a higher linguistic level, capturing the meaning and context of words within the text.&lt;/p&gt;

&lt;p&gt;This approach leads to a larger model vocabulary, encompassing the diversity of words used in the dataset. While this richness is beneficial for understanding the semantics of the language, it introduces challenges, particularly when working with extensive datasets. The increased vocabulary size translates to a higher number of model parameters, necessitating careful management to prevent overfitting.&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;# Consider the sentence, "I love Python"
&lt;/span&gt;&lt;span class="n"&gt;sent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I love Python&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="n"&gt;tokens&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;())]&lt;/span&gt;
&lt;span class="n"&gt;numerical_representation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ch&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;enumerate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tokens&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, the trade-off lies in the potential for overfitting, especially when dealing with smaller datasets. Striking a balance between a rich vocabulary and avoiding over-parameterization becomes a critical consideration when employing word-level tokenization in natural language processing tasks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Subword Tokenization&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Subword tokenization interpolates between word-based and character-based tokenization.&lt;/p&gt;

&lt;p&gt;Instead of treating each whole word as a single building block, subword tokenization breaks down words into smaller, meaningful pieces.&lt;br&gt;
These smaller parts, or subwords, carry meaning on their own and help the computer understand the structure of the word in a more detailed way.&lt;br&gt;
Common words get a slot in the vocabulary, but the tokenizer can fall back to word pieces and individual characters for unknown words.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's do a simple experiment to show the impacts of a tokenization method on the model&lt;/em&gt;&lt;br&gt;&lt;br&gt;
&lt;a href="https://colab.research.google.com/drive/1PwAr2Gt0x_8UUXd5ED_nBTJlpdRXeU3V?usp=sharing" rel="noopener noreferrer"&gt;Colab Link&lt;/a&gt; to the code&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%2Ffnh7ggoiuszm37q7hsta.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%2Ffnh7ggoiuszm37q7hsta.png" alt="Effects of tokenization technique on model size, performance an compute complexity" width="800" height="411"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;From the experiment I conducted above the trend is as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As the number of tokens increases: 

&lt;ul&gt;
&lt;li&gt;The model size also increases&lt;/li&gt;
&lt;li&gt;Model training and inference becomes more compute demanding&lt;/li&gt;
&lt;li&gt;The size of dataset required to achieve high accuracy also increases
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Which tokenization method should I use?&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
Sub-word tokenization is the industry standard!&lt;br&gt;&lt;br&gt;
Consider &lt;a href="https://huggingface.co/learn/nlp-course/chapter6/5?fw=pt" rel="noopener noreferrer"&gt;Byte Pair Encodinge(BPE) techniques&lt;/a&gt; such as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://github.com/openai/tiktoken" rel="noopener noreferrer"&gt;TikToken&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/google/sentencepiece" rel="noopener noreferrer"&gt;SentencePiece&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://huggingface.co/docs/transformers/model_doc/bert" rel="noopener noreferrer"&gt;BERT&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Use character level tokenization or word level tokenization where you have a smaller dataset.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>python</category>
      <category>nlp</category>
    </item>
  </channel>
</rss>
