<?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: Ralphkay</title>
    <description>The latest articles on DEV Community by Ralphkay (@raamponsah).</description>
    <link>https://dev.to/raamponsah</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%2F974807%2F5d4c8122-ba34-4072-85e7-cbb16200de29.jpeg</url>
      <title>DEV Community: Ralphkay</title>
      <link>https://dev.to/raamponsah</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raamponsah"/>
    <language>en</language>
    <item>
      <title>How to simply create custom filters in Django</title>
      <dc:creator>Ralphkay</dc:creator>
      <pubDate>Sun, 04 Dec 2022 16:11:58 +0000</pubDate>
      <link>https://dev.to/raamponsah/how-to-simply-create-custom-filters-in-django-1l1o</link>
      <guid>https://dev.to/raamponsah/how-to-simply-create-custom-filters-in-django-1l1o</guid>
      <description>&lt;p&gt;Django template system, allows for the use and creation of custom filters. Using custom filters is a powerful feature that allows the developer to bend the templating system to do things other than already defined. &lt;/p&gt;

&lt;p&gt;For instance, you could develop a custom filter to return a lookup value once applied to a field ID.&lt;/p&gt;

&lt;p&gt;You can find more information on the Official &lt;a href="https://docs.djangoproject.com/en/4.1/howto/custom-template-tags/" rel="noopener noreferrer"&gt;Django Documentation website&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Extending your template tags with custom filters offers flexibility in your code. 
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Assumptions
&lt;/h4&gt;

&lt;p&gt;This tutorial assumes a basic understanding of Django and Python. The project also assumes that a basic Django app has been setup already.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keep in mind filters are functions. So a good understanding of python functions is required.
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;What our app will do?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The app will store students’ names and their classroom&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What our filter will do?&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  We will define two filters: &lt;code&gt;number_of_students_filter&lt;/code&gt; and &lt;code&gt;classroom_of_students&lt;/code&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Step 1
&lt;/h4&gt;

&lt;p&gt;To define your template filters or tags you need to create a subfolder with the name &lt;code&gt;templatetags&lt;/code&gt; in your app folder.&lt;/p&gt;

&lt;p&gt;Add the &lt;code&gt;__init__.py&lt;/code&gt; into your folder to render it as a module. Latest versions of python do not require this. &lt;/p&gt;

&lt;p&gt;For example given an app named &lt;code&gt;students&lt;/code&gt; you could have this folder structure:&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;students&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
    &lt;span class="n"&gt;templatetags&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;
        &lt;span class="n"&gt;__init__&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;student_filters&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  The file name of the file to contain out custom filters is named &lt;code&gt;student_filters.py&lt;/code&gt;
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Step 2: Lets build our filters
&lt;/h4&gt;

&lt;p&gt;Open the file &lt;code&gt;student_filters.py&lt;/code&gt; :&lt;/p&gt;

&lt;p&gt;Our first filter will be named number_of_students_filter.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;number_of_student_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&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;value&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number_of_students&lt;/span&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Walkthrough
&lt;/h4&gt;

&lt;p&gt;The above function like any other filter takes in an argument value. The value is the parameter that comes before the use of the pipe | symbol when writing tags with filters&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{{ var | filter }}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Therefore, the value is var in this case. In the body of the function, you define what the filter must do, you can do anything but you cannot throw exceptions as filters can output them by design. &lt;/p&gt;

&lt;h2&gt;
  
  
  In this function, we return the number of students in our students’ table by chaining our query with the &lt;code&gt;count()&lt;/code&gt; method. 
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Step 3
&lt;/h4&gt;

&lt;p&gt;The function will now have to be registered as a filter. Django offers a simple interface for this. &lt;/p&gt;

&lt;p&gt;Near the top of your file, place the following:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="n"&gt;register&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Library&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Walkthrough
&lt;/h4&gt;

&lt;p&gt;It is required that for a valid library tag, the file/module must contain a variable named &lt;code&gt;register&lt;/code&gt; which must be an instance of the &lt;code&gt;templated.Library&lt;/code&gt; class. &lt;/p&gt;

&lt;p&gt;To register the filter there are two major ways; using the register function itself or as a decorator. &lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;register&lt;/code&gt; as a function approach
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;register.filter('number_of_students&lt;/code&gt;’&lt;code&gt;, number_of_students)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This takes two arguments, the first argument is the name of the filter and the second argument is the filter function itself.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;register&lt;/code&gt; as a decorator approach
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nd"&gt;@register.filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;number_of_students&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;number_of_students_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="n"&gt;number_of_students&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Students&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;count&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;number_of_students&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  Step 4
&lt;/h4&gt;

&lt;p&gt;Now, you just need to load the module, in this case, &lt;code&gt;student_filters&lt;/code&gt; it in your template file. This is done just like the way you load &lt;code&gt;static&lt;/code&gt;, however in this case you load it with the name of the file/module that contains the filters. In our scenario &lt;code&gt;{{ load student_filters }}&lt;/code&gt; .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;{{load student_filters }}

&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt;&lt;span class="na"&gt;DOCTYPE&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Custom Filters&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Students&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&lt;/span&gt;Number of {{ students|number_of_students }}&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Adding arguments to the filter
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;{{ var | classroom_of_students: 40}}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, how about we allow our custom filter to accept an argument, say &lt;code&gt;arg&lt;/code&gt;. But we’ll create a new custom filter and name it &lt;code&gt;classroom_students_are_in_filter&lt;/code&gt;. This filter will take a number as an argument and based on that number indicate whether by number of students; they are in a larger classroom or not.&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;@register.filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;classroom_of_students&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;classroom_students_are_in_filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

    &lt;span class="n"&gt;number_of_students&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Students&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;count&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;number_of_students&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Students are in the larger classroom&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;
    &lt;span class="k"&gt;elif&lt;/span&gt; &lt;span class="n"&gt;number_of_students&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;arg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;Students are in the smaller classroom&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Walkthrough&lt;/p&gt;

&lt;p&gt;This filter classroom_of_students takes an arg, the argument is what appears after the colon when placed in a template. This argument can be anything, string, float, integer etc. In this case it is an integer. Then run what you would like to do in the body of the function.&lt;/p&gt;

&lt;p&gt;Don't forget to register the filter.&lt;/p&gt;

&lt;p&gt;Voila!&lt;/p&gt;

</description>
      <category>watercooler</category>
    </item>
    <item>
      <title>Live Pulsating animation</title>
      <dc:creator>Ralphkay</dc:creator>
      <pubDate>Fri, 18 Nov 2022 08:54:49 +0000</pubDate>
      <link>https://dev.to/raamponsah/live-pulsating-animation-5243</link>
      <guid>https://dev.to/raamponsah/live-pulsating-animation-5243</guid>
      <description>&lt;p&gt;This morning, I wanted to build a live pulsating animation using CSS and it works! Please check out the code here. Feel free to use it in your projects. Thanks a lot.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/raamponsah/embed/ExRbWmP?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>codepen</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
