<?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: Lody G Mtui</title>
    <description>The latest articles on DEV Community by Lody G Mtui (@lodyne).</description>
    <link>https://dev.to/lodyne</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%2F643977%2Fd88265be-94f8-44a9-aff6-edd6d000cb9d.png</url>
      <title>DEV Community: Lody G Mtui</title>
      <link>https://dev.to/lodyne</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lodyne"/>
    <language>en</language>
    <item>
      <title>Retrofit Workflow</title>
      <dc:creator>Lody G Mtui</dc:creator>
      <pubDate>Sat, 05 Jul 2025 09:52:12 +0000</pubDate>
      <link>https://dev.to/lodyne/retrofit-workflow-320l</link>
      <guid>https://dev.to/lodyne/retrofit-workflow-320l</guid>
      <description>&lt;p&gt;Retrofit is a type-safe REST client library that is used to consume RESTful Web Services.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add Dependency&lt;/strong&gt;&lt;br&gt;
Go to &lt;a href="https://square.github.io/retrofit/" rel="noopener noreferrer"&gt;Retrofit&lt;/a&gt;, copy the following line &lt;code&gt;implementation 'com.squareup.retrofit2:retrofit:(insert latest version)'&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
To get the latest version, navigate to &lt;a href="https://github.com/square/retrofit" rel="noopener noreferrer"&gt;Square Github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Retrofit works with different serialization/deserialization library such as Moshi,Gson etc.In this case, we use Gson.&lt;/p&gt;

&lt;p&gt;From the same page, &lt;a href="https://square.github.io/retrofit/" rel="noopener noreferrer"&gt;Retrofit&lt;/a&gt;, copy this line &lt;code&gt;com.squareup.retrofit2:converter-gson&lt;/code&gt; and add the latest version as Retrofit.&lt;/p&gt;

&lt;p&gt;Then add the dependencies in the &lt;code&gt;build.gradle.kts&lt;/code&gt; as follows:-&lt;br&gt;
&lt;/p&gt;

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

    implementation("com.squareup.retrofit2:converter-gson:2.11.0")

    implementation("com.squareup.retrofit2:retrofit:2.11.0")


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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create Dataclass/Modal class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Movie {
    @SerializedName("id")
    @Expose
    private Integer id;

    @SerializedName("overview")
    @Expose
    private String overview;

    @SerializedName("poster_path")
    @Expose
    private String posterPath;

    @SerializedName("release_date")
    @Expose
    private String releaseDate;

    @SerializedName("title")
    @Expose
    private String title;

    @SerializedName("vote_average")
    @Expose
    private Double voteAverage;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getOverview() {
        return overview;
    }

    public void setOverview(String overview) {
        this.overview = overview;
    }

    public String getPosterPath() {
        return posterPath;
    }

    public void setPosterPath(String posterPath) {
        this.posterPath = posterPath;
    }

    public String getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(String releaseDate) {
        this.releaseDate = releaseDate;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Double getVoteAverage() {
        return voteAverage;
    }

    public void setVoteAverage(Double voteAverage) {
        this.voteAverage = voteAverage;
    }
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create Interface class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface MovieApi {
    @GET("movie/popular")
    Call&amp;lt;Result&amp;gt; getPopularMovies(@Query("api_key") String apiKey);

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create Retrofit Instance&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class RetrofitInstance {
    private static Retrofit retrofit = null;
    private final static String BASE_URL ="https://api.themoviedb.org/3/";

    public static MovieApi getApi(){
        if (retrofit == null){
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit.create(MovieApi.class);
    }

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create Repository (Optional)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MovieRepository {
    private ArrayList&amp;lt;Movie&amp;gt; movies = new ArrayList&amp;lt;&amp;gt;();


    public MovieRepository(Application application) {
        this.application = application;
    }

    public MutableLiveData&amp;lt;List&amp;lt;Movie&amp;gt;&amp;gt; getMutableLiveData(){
        MovieApi movieApi = RetrofitInstance.getApi();

        Call&amp;lt;Result&amp;gt; call = movieApi.
                getPopularMovies(application.getApplicationContext().getString(R.string.api_key));


        call.enqueue(new Callback&amp;lt;Result&amp;gt;() {
            @Override
            public void onResponse(Call&amp;lt;Result&amp;gt; call, Response&amp;lt;Result&amp;gt; response) {
                Result result = response.body();

                if (result != null &amp;amp;&amp;amp; result.getResults() != null){
                    movies = (ArrayList&amp;lt;Movie&amp;gt;) result.getResults();
                    mutableLiveData.setValue(movies);
                }
            }

            @Override
            public void onFailure(Call&amp;lt;Result&amp;gt; call, Throwable throwable) {

            }
        });
        return mutableLiveData;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Room Database Workflow</title>
      <dc:creator>Lody G Mtui</dc:creator>
      <pubDate>Thu, 27 Feb 2025 09:37:54 +0000</pubDate>
      <link>https://dev.to/lodyne/room-database-workflow-24d8</link>
      <guid>https://dev.to/lodyne/room-database-workflow-24d8</guid>
      <description>&lt;p&gt;ROOM Database is the ORM (Object Relational Mapper) library for Android development. &lt;/p&gt;

&lt;p&gt;It is used to map database objects to Java objects.&lt;/p&gt;

&lt;p&gt;ROOM provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite. i.e., it makes it possible to access a database without the need of writing SQL queries.&lt;/p&gt;

&lt;p&gt;Room database has three main components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Entity&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;DAO (Data Access Object)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Database&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In order to implement Room in the Android development, you need to follow the following workflow:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Add ROOM dependency&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go to &lt;a href="https://developer.android.com/jetpack/androidx/releases/room" rel="noopener noreferrer"&gt;https://developer.android.com/jetpack/androidx/releases/room&lt;/a&gt; , then copy the following dependency and paste in &lt;code&gt;build.gradle.kts&lt;/code&gt; in your Android project in the dependencies section&lt;br&gt;
&lt;/p&gt;

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

    implementation("androidx.room:room-runtime:$room_version")
    annotationProcessor("androidx.room:room-compiler:$room_version")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The whole sample dependencies are as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  dependencies {
    val room_version = "2.6.1"

    implementation("androidx.room:room-runtime:$room_version")
    annotationProcessor("androidx.room:room-compiler:$room_version")

    implementation(libs.appcompat)
    implementation(libs.material)
    implementation(libs.activity)
    implementation(libs.constraintlayout)
    testImplementation(libs.junit)
    androidTestImplementation(libs.ext.junit)
    androidTestImplementation(libs.espresso.core)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create the Entity&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Entity is a class that represents a table within the ROOM database.&lt;br&gt;
Each instance of an entity class corresponds to a single row in that table, with each field in that class representing a column.&lt;br&gt;
It must be annotated with &lt;code&gt;@Entity&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.example.contactapp;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity(tableName = "contact")
public class Contact {
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name="contact_id")
    int id;
    @ColumnInfo(name = "first_name")
    String firstName;

    @ColumnInfo(name = "last_name")
    String lastName;

    @ColumnInfo(name="user_mail")
    String email;
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create the DAO&lt;/strong&gt;&lt;br&gt;
DAO is the short form for Data Access Object. It is an interface that defines the set of methods for accessing and manipulating data in the database. It must be annotated with &lt;code&gt;@Dao&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  package com.example.contactapp;


import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import java.util.List;

@Dao
public interface ContactDao {

    @Query("SELECT * FROM contact")
    List&amp;lt;Contact&amp;gt; getAllContact(Contact contact);

    @Update
    void updateContact(Contact contact);

    @Delete
    void deleteContact(Contact contact);

    @Insert
    void getContact();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create the Room Database Instance&lt;/strong&gt;&lt;br&gt;
It is the abstract class that extends RoomDatabase and serves as the main access point to the SQLite database and links the DAOs and entities together.It must be annotated with &lt;code&gt;@Database&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.example.contactapp;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

@Database(version = 1,entities = {Contact.class})
public abstract class ContactDatabase extends RoomDatabase {
    public abstract ContactDao getContactDao();

    private static ContactDatabase dbInstance;

    public static synchronized ContactDatabase getDbInstance(Context 
   context){
        if (dbInstance != null){
            dbInstance = Room.databaseBuilder(
                    context.getApplicationContext(),
                    ContactDatabase.class,
                    "contact_db"
            ).fallbackToDestructiveMigration().build();
        }
        return dbInstance;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Create Repository (Optional)&lt;/strong&gt;&lt;br&gt;
The repository is the class that manages multiple resources of data.&lt;br&gt;
It acts as an intermediary between data sources and ViewModel in the MVVM architecture.&lt;br&gt;
It gathers data from different sources such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;REST APIs&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cache&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Local database storage&lt;br&gt;
... and then provides the data to the rest of the application.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package com.example.contactapp;

import android.app.Application;
import android.os.Handler;
import android.os.Looper;

import androidx.lifecycle.LiveData;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Repository {
    public final ContactDao contactDao;

    ExecutorService executer;

    Handler handler;

    public Repository(Application application) {
        ContactDatabase contactDatabase = ContactDatabase.getDbInstance(application);

        this.contactDao = contactDatabase.getContactDao();

//        Used for background operations
        executer = Executors.newSingleThreadExecutor();

//        Used for updating UI

        handler = new Handler(Looper.getMainLooper());
    }

    public void updateContact(Contact contact){
        executer.execute(new Runnable() {
            @Override
            public void run() {
                contactDao.updateContact(contact);
            }
        });

    }

    public void deleteContact(Contact contact){
        executer.execute(() -&amp;gt; contactDao.deleteContact(contact));

    }

    public void insertContact(Contact contact){
        executer.execute(new Runnable() {
            @Override
            public void run() {
                contactDao.insertContact(contact);
            }
        });

    }

    public LiveData&amp;lt;List&amp;lt;Contact&amp;gt;&amp;gt; getAllContacts(){
        return contactDao.getAllContact();
    }

}

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

&lt;/div&gt;



</description>
      <category>android</category>
      <category>mobile</category>
      <category>androiddev</category>
      <category>java</category>
    </item>
    <item>
      <title>How to Customize Django Forms using Django Widget Tweaks</title>
      <dc:creator>Lody G Mtui</dc:creator>
      <pubDate>Wed, 03 Aug 2022 06:24:40 +0000</pubDate>
      <link>https://dev.to/lodyne/how-to-customize-django-forms-using-django-widget-tweaks-1ih9</link>
      <guid>https://dev.to/lodyne/how-to-customize-django-forms-using-django-widget-tweaks-1ih9</guid>
      <description>&lt;p&gt;Django is the "battery-included" framework as most of the functionalities are already done or automated by the framework itself.&lt;br&gt;
This makes it simpler for the developers to complete their work by the deadlines suggested.&lt;/p&gt;

&lt;p&gt;Django has many options that enable the forms to render data from the model using ModelForm class.&lt;/p&gt;

&lt;p&gt;But Django forms can be automated by using the following &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;{{form.as_p}}&lt;/code&gt; - render forms as paragraph &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;{{form.as_table}}&lt;/code&gt; - render forms as table &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;{{form.as_ul}}&lt;/code&gt; - render forms as list &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But all these automate the Django forms implicitly. For instance, you make the Django form using Model Form and by using form.as_p....&lt;/p&gt;

&lt;p&gt;&lt;em&gt;models.py&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.db import models

class Post(models.Model):
    firstname = models.CharField()
    lastname = models.CharField()
    email = models.EmailField()

    def __str__(self):
        return self.firstname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;forms.py&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django import forms
from .models import Post

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['posts', 'caption']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;views.py&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
def getPost(request):
    post = Post.objects.all()
    context = {
        'post':post
    }
    return render(request,'post_form.html',context)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;post_form.html&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="col-md-4"&amp;gt;
  &amp;lt;form class="form" method="post" enctype="multipart/form-data"&amp;gt;
     {% csrf_token %}
     &amp;lt;fieldset class="form-group"&amp;gt;
       &amp;lt;legend class="text-center mb-4"&amp;gt;Upload Post&amp;lt;/legend&amp;gt;
       {{form.as_p}}
     &amp;lt;/fieldset&amp;gt;

     &amp;lt;button type="submit" class="btn btn-primary"&amp;gt;Post&amp;lt;/button&amp;gt;
  &amp;lt;/form&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose you want to use your own HTML forms in stead of automating using Django itself.&lt;br&gt;
E.g of HTML form&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form action=""&amp;gt;
  &amp;lt;label for="fname"&amp;gt;First name:&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;input type="text" id="fname" value="John"&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;label for="lname"&amp;gt;Last name:&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;input type="text" id="lname" value="Doe"&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;label for="email"&amp;gt;Email:&amp;lt;/label&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;input type="email" id="email" value="john@gmail.com"&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;input type="submit" value="Submit"&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to render this form, you need to use a Django package known as &lt;a href="https://pypi.org/project/django-widget-tweaks/"&gt;Django Tweaks&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install django-widget-tweaks&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then, add it to the &lt;em&gt;settings.py&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    #New
    'widget_tweaks'
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in the templates, don't forget to load it as follows:-&lt;br&gt;
&lt;code&gt;{% load widget_tweaks %}&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;The Django Tweaks library includes two options to customize forms i.e.&lt;/p&gt;
&lt;h4&gt;
  
  
  Use &lt;strong&gt;render_field&lt;/strong&gt;
&lt;/h4&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form&amp;gt;
   &amp;lt;div class="form-group"&amp;gt;
     {% render_field form.firstname class="form-control" 
     placeholder=form.firstname.label type="text" %}
   &amp;lt;/div&amp;gt;

   &amp;lt;div class="form-group"&amp;gt;
     {% render_field form.lastname class="form-control" 
     placeholder=form.lastname.label type="text" %}
   &amp;lt;/div&amp;gt;

   &amp;lt;div class="form-group"&amp;gt;
     {% render_field form.email class="form-control" 
     placeholder=form.email.label type="email" %}
   &amp;lt;/div&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h4&gt;
  
  
  Using &lt;strong&gt;template filters&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In the &lt;em&gt;post_form.html&lt;/em&gt; file above, replace form.as_p with the following :-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{% for hidden in form.hidden_fields %}
  {{ hidden }}
{% endfor %}

{% for field in form.visible_fields %}
  &amp;lt;div class="form-group"&amp;gt;
    &amp;lt;label for="{{ field.id_for_label }}"&amp;gt;{{ field.label }}&amp;lt;/label&amp;gt;
    {{ field|add_class:'form-control' }}    
    {% for error in field.errors %}
      &amp;lt;span class="help-block"&amp;gt;{{ error }}&amp;lt;/span&amp;gt;
    {% endfor %}
  &amp;lt;/div&amp;gt;
{% endfor %}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Django tweaks library is essential tool to use to customize HTML forms and it is flexible to any form of your choice.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pypi.org/project/django-widget-tweaks/"&gt;Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/how-to-customize-django-forms-using-django-widget-tweaks/?ref=rp"&gt;geeksforgeeks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.javatpoint.com/how-to-use-django-widget-tweaks"&gt;javatpoint&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>django</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>You either 'let', 'var' or keep 'const' in JavaScript</title>
      <dc:creator>Lody G Mtui</dc:creator>
      <pubDate>Sun, 12 Jun 2022 07:22:44 +0000</pubDate>
      <link>https://dev.to/lodyne/you-either-make-var-let-or-keep-const-in-javascript-g0o</link>
      <guid>https://dev.to/lodyne/you-either-make-var-let-or-keep-const-in-javascript-g0o</guid>
      <description>&lt;h1&gt;
  
  
  Difference between var, let and const
&lt;/h1&gt;

&lt;p&gt;In JavaScript, variables are assigned using three main keywords/ways which are:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;&lt;strong&gt;var&lt;/strong&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;strong&gt;let&lt;/strong&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;strong&gt;const&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But, var has became the old way to declare a variable due to the introduction of JavaScript ES6. &lt;/p&gt;

&lt;p&gt;Many features were introduced such as let and const which have became de-facto ways for variable declarations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; Variable is the value that occupies space into the computer memory i.e. container that is used to store data.&lt;/p&gt;

&lt;p&gt;There is difference between var, let and const in JavaScript. These differences between all these keywords, are based on the following concepts:-&lt;/p&gt;

&lt;h2&gt;
  
  
  Scope
&lt;/h2&gt;

&lt;p&gt;This refers to the way in which variables are accessed i.e. It's how we determine the accessibility of variables.&lt;br&gt;
In JavaScript, there are three scopes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Global Scope&lt;/li&gt;
&lt;li&gt;Function Scope&lt;/li&gt;
&lt;li&gt;Block Scope&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Global Scope
&lt;/h3&gt;

&lt;p&gt;This refers to when variables are declared outside the function block and can be accessed anywhere within the program.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age = 19;
let game = 'fifa';
const pie = 3.14;

function getAll(){
    console.log(age);
    console.log(game);
    console.log(pie);
}
console.log(age);
console.log(game);
console.log(pie);
getAll();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;:Any of var, let or const are used to declare global variables which can be accessed anywhere in the program.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function Scope
&lt;/h3&gt;

&lt;p&gt;This refers to when variables are declared inside the function scope and can only be accessed within a function. Also known as local scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getAge(){
    var name = "John";
    console.log(name);
}
// console.log(name); // ReferenceError: name is not defined
getAge();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;:Only &lt;em&gt;&lt;strong&gt;var&lt;/strong&gt;&lt;/em&gt; is function scoped i.e. a variable declared by var in function block can accessed only in function itself and cannot be accessed outside the function i.e. It throws a &lt;em&gt;ReferenceError&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Block Scope
&lt;/h3&gt;

&lt;p&gt;This refers to when variables are declared in the block or curly brackets and cannot be accessed outside it. A block can be function or control structures such as if, for, while etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function gaming(){
    let name = "John";
    const age = 19;
    console.log(name);
    console.log(age);
}
// console.log(name); // ReferenceError: name is not defined
// console.log(age); // ReferenceError: name is not defined

if(true){
    let name = "Paul";
    console.log(name);
}
// console.log(name);
gaming();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;:Both &lt;em&gt;&lt;strong&gt;let&lt;/strong&gt;&lt;/em&gt; and &lt;em&gt;&lt;strong&gt;const&lt;/strong&gt;&lt;/em&gt; are block scoped i.e. a variable declared by let or const in a block can accessed only in the block and cannot be accessed outside the block i.e. It throws a &lt;em&gt;ReferenceError&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Hoisting
&lt;/h2&gt;

&lt;p&gt;This refers to the behavior to move up the declarations to the top of the scope before code execution. It's a tendency that allows variables and functions to be used before their declaration i.e. the variables/ functions declarations are moved up to the start of code block before execution regardless where they are declared in the code block.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;var allows hoisting
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name = "John";
console.log(goal);
var name;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;let and const don't allow hoisting
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;goal = "business";
console.log(goal);
let goal;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;goal = "business";
console.log(goal);
const goal;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Declaration &amp;amp; Accessibility
&lt;/h2&gt;

&lt;p&gt;The variables declared using &lt;em&gt;&lt;strong&gt;var&lt;/strong&gt;&lt;/em&gt; can be declared without initialization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age;
console.log(age); // undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The variables declared using &lt;em&gt;&lt;strong&gt;var&lt;/strong&gt;&lt;/em&gt; can be accessed without initialization and returns undefined.&lt;/p&gt;

&lt;p&gt;Also those using &lt;em&gt;&lt;strong&gt;let&lt;/strong&gt;&lt;/em&gt;  can be declared without initialization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let age;
console.log(age); // undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The variables declared using &lt;em&gt;&lt;strong&gt;let&lt;/strong&gt;&lt;/em&gt; cannot be accessed without initialization and returns error.&lt;/p&gt;

&lt;p&gt;And those using &lt;em&gt;&lt;strong&gt;const&lt;/strong&gt;&lt;/em&gt; cannot be declared without initialization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pie;
console.log(pie); // SyntaxError: Missing initializer in const declaration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: The variables declared using &lt;em&gt;&lt;strong&gt;const&lt;/strong&gt;&lt;/em&gt; cannot be accessed without initialization as it cannot declared without initialization.&lt;/p&gt;

&lt;h2&gt;
  
  
  Re-declaration
&lt;/h2&gt;

&lt;p&gt;The variables declared using &lt;em&gt;&lt;strong&gt;var&lt;/strong&gt;&lt;/em&gt; can be re-declared and updated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var age = 19;
var age = 20;
age = 30;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While those using &lt;em&gt;&lt;strong&gt;let&lt;/strong&gt;&lt;/em&gt;  cannot be re-declared but can be updated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let game = 'fifa';
game = 'pes';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And &lt;em&gt;&lt;strong&gt;const&lt;/strong&gt;&lt;/em&gt; can neither be re-declared nor updated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pie = 3.14;
const pie = 2.15; // SyntaxError: Identifier 'pie' has already been declared
pie = 10; // TypeError: Assignment to constant variable.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

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

&lt;blockquote&gt;
&lt;p&gt;Image credit: &lt;a href="https://www.geeksforgeeks.org" rel="noopener noreferrer"&gt;GeeksforGeeks&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Consume REST APIs using Python</title>
      <dc:creator>Lody G Mtui</dc:creator>
      <pubDate>Thu, 17 Mar 2022 11:29:42 +0000</pubDate>
      <link>https://dev.to/lodyne/how-to-consume-rest-apis-using-python-59i1</link>
      <guid>https://dev.to/lodyne/how-to-consume-rest-apis-using-python-59i1</guid>
      <description>&lt;h4&gt;
  
  
  API
&lt;/h4&gt;

&lt;p&gt;API stands for Application Programming Interface. &lt;br&gt;
API is an interface that acts as communication layer which makes two application talks to each other.&lt;/p&gt;

&lt;p&gt;In order to build API, we use REST( there are other ways like GraphQL ) Architecture Style.&lt;/p&gt;
&lt;h4&gt;
  
  
  REST
&lt;/h4&gt;

&lt;p&gt;REST stands for Representational  State Transfer.&lt;br&gt;
REST is an architectural design style that facilitates communication between client and server in the network. &lt;/p&gt;

&lt;p&gt;In order to access resources from web services, a request is sent to a specific resource and response is expected.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Resource is any data available in web service.&lt;/li&gt;
&lt;li&gt;Request sent is called HTTP request.&lt;/li&gt;
&lt;li&gt;HTTP is a protocol which governs how client and server communicate. &lt;/li&gt;
&lt;li&gt;HTTP request uses special methods known HTTP methods to tell the API which action is needed to be performed.&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  HTTP Methods
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;GET - retrieve an existing resource&lt;/li&gt;
&lt;li&gt;POST - create a new resource&lt;/li&gt;
&lt;li&gt;PUT - update an existing resource &lt;/li&gt;
&lt;li&gt;DELETE - delete a resource&lt;/li&gt;
&lt;li&gt;PATCH - partially update an existing resource&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Status Codes
&lt;/h4&gt;

&lt;p&gt;Response from server is sent after client making request. This response comes with a code which provides the information about the it's results.&lt;/p&gt;

&lt;p&gt;The following are common status codes:-&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;200 - OK&lt;/li&gt;
&lt;li&gt;204 - No Content&lt;/li&gt;
&lt;li&gt;400 - Bad Request&lt;/li&gt;
&lt;li&gt;404 - Not Found&lt;/li&gt;
&lt;li&gt;500 - Internal Server Error&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  API Endpoints
&lt;/h4&gt;

&lt;p&gt;Referred as public URLs that are used to access resources in web services.&lt;/p&gt;

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

&lt;blockquote&gt;
&lt;p&gt;Image Credit:&lt;a href="https://rapidapi.com/hub" rel="noopener noreferrer"&gt;Rapid API&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h4&gt;
  
  
  How to Use Python To Consume REST APIs
&lt;/h4&gt;

&lt;p&gt;Use&lt;code&gt;requests&lt;/code&gt; library&lt;br&gt;
To start using it, install by using &lt;code&gt;pip&lt;/code&gt; i.e. &lt;code&gt;pip install requests&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Take example of &lt;a href="https://rapidapi.com/martin.svoboda/api/quotes15/" rel="noopener noreferrer"&gt;Quotes&lt;/a&gt; from Rapid API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import requests
url = "https://quotes15.p.rapidapi.com/quotes/random/"
headers = {
    'x-rapidapi-host': "quotes15.p.rapidapi.com",
    'x-rapidapi-key': "b799b564e4mshe9022c63f6e7e49p12a6bbjsndfd08f1175da"
    }
response = requests.request("GET", url, headers=headers)
print(response.text)
print(response.json())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;~ Here we start importing requests - a de facto library for consuming API.&lt;br&gt;
~ We assign the endpoint - the actual API to variable url.&lt;br&gt;
~ &lt;strong&gt;&lt;em&gt;headers&lt;/em&gt;&lt;/strong&gt; are extra information for each API call you can make. (don't worry - they come with API documentation)&lt;br&gt;
~ Then, we pass the endpoint using request method in &lt;br&gt;
&lt;code&gt;response = requests.request("GET", url, headers=headers)&lt;/code&gt; &lt;br&gt;
~ Then you can output using &lt;code&gt;text&lt;/code&gt; or &lt;code&gt;json()&lt;/code&gt; method.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>First-Class Function in Python</title>
      <dc:creator>Lody G Mtui</dc:creator>
      <pubDate>Fri, 04 Mar 2022 12:31:17 +0000</pubDate>
      <link>https://dev.to/lodyne/first-class-function-in-python-snake-4hi5</link>
      <guid>https://dev.to/lodyne/first-class-function-in-python-snake-4hi5</guid>
      <description>&lt;p&gt;Everything in Python is an Object (You heard it here first). Just kidding! Of course, at it's core everything in Python is an object.&lt;/p&gt;

&lt;p&gt;Not all languages have this feature, but Python is one of them, together with JavaScript, Ruby etc.&lt;br&gt;
This means, string, int, float, datatypes such as dictionary, list, tuple etc. are all objects.&lt;/p&gt;

&lt;p&gt;Object is an entity with attributes and behavior. So, function in Python is treated as object. That's why you can call them as First-class function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First-Class Function&lt;/strong&gt; means that functions are treated as values -  that you can assign a function into a variable, pass it around etc. &lt;/p&gt;
&lt;h4&gt;
  
  
  Properties of First-Class Function
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;you can assign them as variable.&lt;/li&gt;
&lt;li&gt;you can pass them to other functions as arguments. &lt;/li&gt;
&lt;li&gt;you can return them from other functions as values.&lt;/li&gt;
&lt;li&gt;you can store them in data structures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets dive into code examples for each property&lt;/p&gt;

&lt;p&gt;1.Assigning function as variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def square(num):
    return num*num

# store the function in a variable
result = square
print(result2(3))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Pass the function as a parameter to another function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def morning():
    return "good morning"

def noon():
    return "good afternoon"

def greetings(function):
    greet = function()
    print(greet)

greetings(morning)
greetings(noon)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3.Return function from other functions as values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def outer_power(num1):
    def inner_power(num2):
        return num1 ** num2
    return inner_power

power = outer_power(4)
print(power(3))

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

&lt;/div&gt;



&lt;p&gt;4.Store function in data structures&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def plusOne(num):
    return num + 1

def plusTwo(num):
    return num + 2

def plusThree(num):
    return num + 3

list_function = [plusOne,plusTwo,plusThree]

for i in list_function:
    print(i)

print(plusOne)
print(plusTwo)
print(plusThree)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>programming</category>
      <category>python</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
