<rss version="2.0">
  <channel>
    <title>Meet Gor - Tag: django</title>
    <link>https://www.meetgor.com</link>
    <description>Posts tagged with django</description>
    <language>en-us</language>
    <pubDate>Sat, 10 Oct 2026 09:08:39 UTC</pubDate>
    <item>
      <title>On not using Django in 2026 due to AI?</title>
      <link>https://www.meetgor.com/links/on-not-using-django-in-2026</link>
      <description>I don&#39;t quite get it. Maybe its true. Django provided a good start but then it was like a lock in. With LLMs its quite easy to generate the boilerplatey code th</description>
      <pubDate>Thu, 08 Jan 2026 00:00:00 UTC</pubDate>
      <content>I don&#39;t quite get it. Maybe its true. Django provided a good start but then it was like a lock in.&#xA;&#xA;With LLMs its quite easy to generate the boilerplatey code that django provides out of the box, so that demand is lost?&#xA;&#xA;Its not the only reason django is here right? It has extensions, best python community and even more best documentation.&#xA;&#xA;I think it will be the best framework to build with LLMs in the future if the ecosystem continues to improve</content>
      <type>links</type>
    </item>
    <item>
      <title>Create a Non-Clustered Index in Django with Postgres as DB</title>
      <link>https://www.meetgor.com/til/django-non-clustered-index-pg</link>
      <description>Understanding how to add a non-clustered index in a postgres database in a django project.</description>
      <pubDate>Thu, 10 Nov 2022 00:00:00 UTC</pubDate>
      <content>## What is a non-clustered index?&#xA;&#xA;A non-clustered index is a seperate structure than an actual table in the database, it stores the non-clustered index key(the column which we want to sort in the table), and a pointer to the actual values based on the index key. So, non-clustered indexes do not change the physical order of the table records, instead it holds a structure that can provide a easier and distinct way to fetch objects based on a particular column as the primary key in the structure.&#xA;&#xA;## How to create a non-clustered index in django&#xA;&#xA;In django, we can use the [db_index](https://docs.djangoproject.com/en/4.1/ref/models/indexes/) property on a field(s) to create a index on the table/model. &#xA;&#xA;### Add the property to the field in the model&#xA;&#xA;Chose a field in which, you want to add a index. It can be a foreign key or any other normal field defined in your model.&#xA;&#xA;We have used the typical blog model, so used in the some of my [TILS](https://www.meetgor.com/tils/) in django, it is just convenient to explain and understand as well. We have a django project named `core` and it has a app `blog` with a model defined below. The model `Article` has a few attributes like `title`, `description`, `content` and `status`.&#xA;&#xA;```python&#xA;from django.db import models&#xA;&#xA;ARTICLE_STATUS = [&#xA;    (&#34;PUBLISHED&#34;, &#34;Published&#34;),&#xA;    (&#34;DRAFT&#34;, &#34;Draft&#34;),&#xA;]&#xA;&#xA;class Article(models.Model):&#xA;    title = models.CharField(max_length=128, db_index=True)&#xA;    description = models.CharField(max_length=512)&#xA;    content = models.TextField()&#xA;    status = models.CharField(max_length=16, choices=ARTICLE_STATUS, default=&#34;DRAFT&#34;)&#xA;&#xA;    def __str__(self):&#xA;        return self.title&#xA;```&#xA;&#xA;So, we have added a `db_index` to the title column in the model as a property. This will be equivalent to creating a index in `SQL` as follows:&#xA;&#xA;```&#xA;$ python manage.py makemigrations&#xA;&#xA;Migrations for &#39;blog&#39;:&#xA;  blog/migrations/0002_alter_article_title.py&#xA;    - Alter field title on article&#xA;```&#xA;&#xA;```&#xA;$ python manage.py migrate&#xA;&#xA;Operations to perform:&#xA;  Apply all migrations: admin, auth, blog, contenttypes, sessions&#xA;Running migrations:&#xA;  Applying blog.0002_alter_article_title... OK&#xA;&#xA;```&#xA;&#xA;Indexes are not standard as in SQL, but each vendor(sqlite, postgres, mysql) have their own flavour of syntax and naunces.&#xA;&#xA;```sql&#xA;CREATE INDEX &#34;blog_article_title_3c514952&#34; ON &#34;blog_article&#34; (&#34;title&#34;);&#xA;&#xA;CREATE INDEX &#34;blog_article_title_3c514952_like&#34; ON &#34;blog_article&#34; (&#34;title&#34; varchar_pattern_ops);&#xA;```&#xA;&#xA;The above index commands are specific to the field, as the title field is a varchar, it has two types of index, it can generate one with simple match and other for `LIKE` comparisons because of string comparison behaviour.&#xA;&#xA;So, we just created a simple index and now if we query the db for a particular `title` which now has its own index for the table `blog_article`. This means, we will be able to fetch queries quickly if we are specifically filtering for `title`.&#xA;&#xA;### Adding some data records&#xA;&#xA;We can add a few data records to test the query from the databse, you can ignore this part as it would be just setting up a django project and adding a few records to the databse. This part won&#39;t make sense for people reading to get the actual stuff done, move to the next part please.&#xA;&#xA;```&#xA;python manage.py createsuperuser&#xA;# Create a super user and run the server&#xA;&#xA;python manage.py runserver&#xA;# Locate to http://127.0.0.1:8000/admin&#xA;# Create some records in the artilce model&#xA;```&#xA;&#xA;So, after creating some records, you should have a simple database and a working django application.&#xA;&#xA;```sql&#xA;SELECT * FROM blog_article;&#xA;```&#xA;```&#xA;blog_test=# SELECT * FROM blog_article;&#xA;&#xA; id |  title   | description |          content          |  status   &#xA;----+----------+-------------+---------------------------+-----------&#xA;  1 | test     | test 1      | test content              | DRAFT&#xA;  2 | testpost | test 2      | test content more content | DRAFT&#xA;  3 | newpost  | test 3      | test nothing              | PUBLISHED&#xA;(3 rows)&#xA;```&#xA;&#xA;## Testing Queries&#xA;&#xA;We can now use SQL queries or django filters to check if we get results by a sequential or an index scan. If we have a filter of `title` we will get the results after performing an `Index Scan` which means, it will look up in the index columns rather than scanning the entire table of records. This is a way **we can test the indexes are working, efficiency is a differnet topic.** We can&#39;t get a idea of performance with this little data and just one connection. A real time database and having multiple conncurrent requests and connections is a good environment to test(don&#39;t do it in a production db :)&#xA;&#xA;&#xA;```sql&#xA;EXPLAIN SELECT * FROM blog_article WHERE description LIKE &#39;test 2&#39;;&#xA;```&#xA;&#xA;```&#xA;blog_test=# EXPLAIN ANALYSE SELECT * FROM blog_article WHERE description LIKE &#39;test&#39;;&#xA;---------------------------------------------------------------------------------------------------------&#xA;&#xA; Seq Scan on blog_article  (cost=0.00..11.00 rows=1 width=880) (actual time=0.180..0.181 rows=0 loops=1)&#xA;   Filter: ((description)::text ~~ &#39;test&#39;::text)&#xA;   Rows Removed by Filter: 3&#xA; Planning Time: 0.189 ms&#xA; Execution Time: 0.217 ms&#xA;(5 rows)&#xA;&#xA;```&#xA;&#xA;The above query selects the records whose `description` is like `test 2`, this performs a `Sequenitial Scan` in the database i.e. iterating over the records one by one of the order of the primary key / id of the records in the table. &#xA;&#xA;```sql&#xA;EXPLAIN SELECT * FROM blog_article WHERE title LIKE &#39;test 2&#39;;&#xA;```&#xA;&#xA;```&#xA;blog_test=# EXPLAIN ANALYSE SELECT * FROM blog_article WHERE title LIKE &#39;test&#39;;&#xA;---------------------------------------------------------------------------------------------------------&#xA;&#xA;Index Scan using blog_article_title_3c514952_like on blog_article  (cost=0.14..8.16 rows=1 width=880) (actual time=0.043..0.048 rows=1 loops=1)&#xA;   Index Cond: ((title)::text = &#39;test&#39;::text)&#xA;   Filter: ((title)::text ~~ &#39;test&#39;::text)&#xA; Planning Time: 0.208 ms&#xA; Execution Time: 0.093 ms&#xA;(5 rows)&#xA;```&#xA;&#xA;In the above query, the select statement has a filter with the title being like `test 2`, and since we have a index for looking for like of title column, the database performs a index scan on that table and fetches the result.&#xA;&#xA;Here are some tradeoffs, the planning is more and the execution time is less, this is quite logical as it would take time to make decision because the database has more options than before creating indexes.&#xA;&#xA;In the query where we filtered the description, the planning time was less as it makes sense there was just one option to go for sequential scan, but it took time to perform the operation as it would scan the entire table one by one.&#xA;&#xA;## Using Django to test queries&#xA;&#xA;We can even use django to filter out the objects in the table. We simply use the `filter` method to check with a particular value.&#xA;&#xA;We can use the shell, to perform some queries. You can use this in your views or viewsets as per your requirements and constraints.&#xA;&#xA;We can even use `explain` to see what the underlying `sql` got executed out from the ORM. The [explain](https://docs.djangoproject.com/en/3.1/ref/models/querysets/#explain) function is similar to the `EXPLAIN ANALYSE` command in the `sql` queries. It gives a bit of context on how the query was executed.&#xA;&#xA;```&#xA;$ python manage.py shell&#xA;```&#xA;&#xA;```python&#xA;&gt;&gt;&gt; from blog.models import Article                                                                &#xA;&gt;&gt;&gt; Article.objects.filter(description=&#39;test 1&#39;)                                                   &#xA;&#xA;&lt;QuerySet [&lt;Article: test&gt;]&gt;                                                                       &#xA;&#xA;&#xA;&gt;&gt;&gt; Article.objects.filter(description=&#39;test 1&#39;).explain()                                         &#xA;&#xA;&#34;Seq Scan on blog_article  (cost=0.00..11.00 rows=1 width=880)\n  Filter: ((description)::text = &#39;t&#xA;est 1&#39;::text)&#34;                                                                                     &#xA;&#xA;&#xA;&gt;&gt;&gt; Article.objects.filter(title=&#39;test&#39;)                                                           &#xA;&#xA;&lt;QuerySet [&lt;Article: test&gt;]&gt;                                                                       &#xA;&#xA;&#xA;&gt;&gt;&gt; Article.objects.filter(title=&#39;test&#39;).explain()                                                 &#xA;&#xA;&#34;Index Scan using blog_article_title_3c514952_like on blog_article  (cost=0.14..8.16 rows=1 width=8&#xA;80)\n  Index Cond: ((title)::text = &#39;test&#39;::text)&#34;                                                 &#xA;&#xA;```&#xA;&#xA;We can use `__contains` for replicating the behaviour of `LIKE` in python/django from SQL. The below example will check if the title has a word `test` in any records of the database.&#xA;&#xA;```&#xA;&gt;&gt;&gt; Article.objects.filter(title__contains=&#39;test&#39;)&#xA;&#xA;&lt;QuerySet [&lt;Article: test&gt;, &lt;Article: testpost&gt;]&gt; &#xA;```&#xA;&#xA;BONUS: We can even get the underlying SQL with the `.query.__str__()` method. &#xA;&#xA;```&#xA;articles = Article.objects.filter(title__contains=&#39;test&#39;)&#xA;&#xA;articles.query.__str__()&#xA;```&#xA;&#xA;```&#xA;&#39;SELECT &#34;blog_article&#34;.&#34;id&#34;, &#34;blog_article&#34;.&#34;title&#34;, &#34;blog_article&#34;.&#34;description&#34;, &#34;blog_article&#34;.&#34;&#xA;content&#34;, &#34;blog_article&#34;.&#34;status&#34; FROM &#34;blog_article&#34; WHERE &#34;blog_article&#34;.&#34;title&#34;::text LIKE %test&#xA;%&#39;&#xA;```&#xA;&#xA;Here, we are able to see that clearly, that the django orm used the `LIKE` clause for comparing the title.&#xA;&#xA;Further readings and references: &#xA;&#xA;- [Indexing in Postgres](https://medium.com/geekculture/indexing-in-postgres-db-4cf502ce1b4e)&#xA;- [Indexing refernece Django docs](https://docs.djangoproject.com/en/4.1/ref/models/indexes/)&#xA;- [Non-Clustered indexing](https://gudevsoc.com/what-is-non-clustered-index-in-sql-with-example/)</content>
      <type>til</type>
    </item>
    <item>
      <title>Django Bulk Update QuerySet objects</title>
      <link>https://www.meetgor.com/til/django-bulk-update-queryset</link>
      <description>Using bulk_update to update multiple objects in one go.</description>
      <pubDate>Mon, 31 Oct 2022 00:00:00 UTC</pubDate>
      <content>Let&#39;s say, I have a lots of objects which I want to update with a particular field or fields. We can use the [bulk_update](https://docs.djangoproject.com/en/4.1/ref/models/querysets/#bulk-update) method with the model name.&#xA;&#xA;```python&#xA;# blog/models.py&#xA;&#xA;from django.db import models&#xA;&#xA;ARTICLE_STATUS = [&#xA;    (&#34;PUBLISHED&#34;, &#34;Published&#34;),&#xA;    (&#34;DRAFT&#34;, &#34;Draft&#34;),&#xA;]&#xA;&#xA;class Article(models.Model):&#xA;    title = models.CharField(max_length=128)&#xA;    description = models.CharField(max_length=512)&#xA;    content = models.TextField()&#xA;    status = models.CharField(max_length=16, choices=ARTICLE_STATUS, default=&#34;DRAFT&#34;)&#xA;&#xA;    def __str__(self):&#xA;        return self.title&#xA;&#xA;```&#xA;&#xA;Let&#39;s say we have a simple model `Article` with a few typical attributes like `title`, `description`, `content`, and `status`. We have the status as a choice field from two options as `Draft` and `Published`. It could be a boolean field, but that looks too gross for a article status.&#xA;&#xA;&#xA;```python&#xA;&#xA;from blog.models import Article&#xA;&#xA;articles = Article.objects.filter(status=&#34;draft&#34;)&#xA;&#xA;for i in range(len(articles)):&#xA;    articles[i].status = &#34;published&#34;&#xA;&#xA;Article.objects.bulk_update(articles, [&#34;status&#34;,])&#xA;&#xA;```&#xA;&#xA;&#xA;In the above code, the `Articles` model is filtered by the status of `draft`. We iterate over the `QuerySet` which will contain the objects of the articles, by setting the object&#39;s properties to the value we want to set. We are jsut setting the value of the property of the object for each object.&#xA;&#xA;This just makes a changes to the `QuerySet`, by using the `bulk_update` method, the two parameters required are the `QuerySet` and the list of `fields` which are to be updated. The function returns the number of objects/records updated.&#xA;&#xA;```python&#xA;&gt;&gt;&gt; from blog.models import Article&#xA;&gt;&gt;&gt; articles = Article.objects.filter(status=&#34;DRAFT&#34;)&#xA;&gt;&gt;&gt; articles&#xA;&lt;QuerySet [&lt;Article: test 1&gt;, &lt;Article: test 3&gt;]&gt;&#xA;&#xA;&gt;&gt;&gt; for i in range(len(articles)):&#xA;...     articles[i].status = &#34;PUBLISHED&#34;&#xA;...&#xA;&gt;&gt;&gt; articles&#xA;&lt;QuerySet [&lt;Article: test 1&gt;, &lt;Article: test 3&gt;]&gt;&#xA;&gt;&gt;&gt;&#xA;&#xA;&gt;&gt;&gt; Article.objects.bulk_update(articles, [&#39;status&#39;,])&#xA;2&#xA;&#xA;&gt;&gt;&gt; Article.obejcts.get(title=&#34;test 1&#34;).status&#xA;&#39;PUBLISHED&#39;&#xA;&#xA;&gt;&gt;&gt; Article.objects.filter(status=&#34;DRAFT&#34;)&#xA;&lt;QuerySet []&gt;&#xA;&gt;&gt;&gt;&#xA;```&#xA;&#xA;As, we can see here there were two obejcts `test 1` and `test 2` objects with the status as `Draft`. By iterating over the queryset and assigning the status of the object to published, the query set was changed and modified locally.&#xA;By using the `bulk_update` method, we parsed the queryset and the list of attributes to be updated into the function. This gives us the number of objects which were updated, in this case `2`. We then look into the article actual record in the database and it has indeed updated to the value we set in this operation.</content>
      <type>til</type>
    </item>
    <item>
      <title>Django-Mermaid</title>
      <link>https://www.meetgor.com/projects/django-mermaid</link>
      <description>Generate ER Diagram for your Django project in Markdown with Mermaid</description>
      <pubDate>Wed, 03 Aug 2022 00:00:00 UTC</pubDate>
      <content>&#xA;&#xA;Create ER Diagrams (Entity Relationship Diagrams) of your Django projects related to different models of associated applications in Mermaid.&#xA;&#xA;Tech Stack:&#xA;- Python&#xA;- Django&#xA;</content>
      <type>projects</type>
    </item>
    <item>
      <title>Django: Get list of all models and associated fields in a django project</title>
      <link>https://www.meetgor.com/til/django-list-models</link>
      <description>Get the list of all the models and associated fields/attributes in a django project or an application</description>
      <pubDate>Tue, 02 Aug 2022 00:00:00 UTC</pubDate>
      <content>## Context&#xA;&#xA;Let&#39;s say we want the list of all the models and associated attributes in all the applications of a django project, we can do that using the [django.apps](https://docs.djangoproject.com/en/4.0/ref/applications/) with apps method. &#xA;&#xA;## Get all the models in a project&#xA;&#xA;To fetch all the models, we can use the [get_models](https://docs.djangoproject.com/en/4.0/ref/applications/#django.apps.AppConfig.get_models) methods, it will return a list of model classes in all the entire project(all applications). We can import all the models in the django project with the command:&#xA; &#xA;```python&#xA;from django.apps import apps&#xA;models = apps.get_models()&#xA;```&#xA;&#xA;```&#xA;[&lt;class &#39;django.contrib.admin.models.LogEntry&#39;&gt;, &lt;class &#39;django.contrib.auth.models.Permission&#39;&gt;, &#xA;&lt;class &#39;django.contrib.auth.models.Group&#39;&gt;, &lt;class &#39;django.contrib.contenttypes.models.ContentType&#39;&gt;,&#xA; &lt;class &#39;django.contrib.sessions.models.Session&#39;&gt;, &lt;class &#39;allauth.account.models.EmailAddress&#39;&gt;, &#xA;&lt;class &#39;allauth.account.models.EmailConfirmation&#39;&gt;, &lt;class &#39;allauth.socialaccount.models.SocialApp&#39;&gt;, &#xA;&lt;class &#39;allauth.socialaccount.models.SocialAccount&#39;&gt;, &lt;class &#39;allauth.socialaccount.models.SocialToken&#39;&gt;, &#xA;&lt;class &#39;user.models.TimeStampedModel&#39;&gt;, &lt;class &#39;user.models.User&#39;&gt;, &lt;class &#39;articles.models.Tags&#39;&gt;,&#xA; &lt;class &#39;articles.models.Series&#39;&gt;, &lt;class &#39;articles.models.Article&#39;&gt;, &lt;class &#39;blog.models.Blog&#39;&gt;]&#xA;```&#xA;&#xA;We are importing the apps and creating a list of the models in our django project. The Django app command will load all the applications in the project, and the [get_models]() method will fetch the associated models. This has resulted in a list of model class objects, we can iterate over them and fetch the required details, we want.&#xA;&#xA;For instance, If I am interested in the name of these models, I can use the `__name__` property to fetch the model&#39;s name. &#xA;&#xA;```python&#xA;from django.apps import apps&#xA;model_list = apps.get_models()&#xA;&#xA;for model in model_list:&#xA;    print(model.__name__)&#xA;```&#xA;&#xA;```&#xA;LogEntry&#xA;Permission&#xA;Group&#xA;ContentType&#xA;Session&#xA;EmailAddress&#xA;EmailConfirmation&#xA;SocialApp&#xA;SocialAccount&#xA;SocialToken&#xA;TimeStampedModel&#xA;User&#xA;Tags&#xA;Series&#xA;Article&#xA;Blog&#xA;&#xA;```&#xA;&#xA;So, from the above example, we can see we have accessed all the model names in our entire django project. &#xA;&#xA;## Access Application name associated with a model&#xA;&#xA;For accessing the name of the application from the model class, we can use the `_meta` attribute followed by the `app_label` property to get the `app_name` associated with the model.&#xA;&#xA;```python&#xA;from django.apps import apps&#xA;model_list = apps.get_models()&#xA;&#xA;for model in model_list:&#xA;    print(f&#34;{model._meta.app_label}  -&gt; {model.__name__}&#34;)&#xA;```&#xA;&#xA;```&#xA;admin  -&gt; LogEntry&#xA;auth  -&gt; Permission&#xA;auth  -&gt; Group&#xA;contenttypes  -&gt; ContentType&#xA;sessions  -&gt; Session&#xA;account  -&gt; EmailAddress&#xA;account  -&gt; EmailConfirmation&#xA;socialaccount  -&gt; SocialApp&#xA;socialaccount  -&gt; SocialAccount&#xA;socialaccount  -&gt; SocialToken&#xA;user  -&gt; TimeStampedModel&#xA;user  -&gt; User&#xA;articles  -&gt; Tags&#xA;articles  -&gt; Series&#xA;articles  -&gt; Article&#xA;blog  -&gt; Blog&#xA;```&#xA;&#xA;In the above example, we can see we have printed all the models with their associated application names. &#xA;&#xA;## Accessing all the attributes associated with a model&#xA; &#xA;To access all the fields/property/attributes associated with a model, we can again use the `_meta` attribute followed by the `get_fields` method.  This method will return a list of field objects. For accessing the name of those attributes/fields, we have to iterate over the list and then further use `name` property.&#xA;&#xA;```python&#xA;from django.apps import apps&#xA;model_list = apps.get_models()&#xA;for model in model_list:&#xA;    print(model.__name__)&#xA;    field_list = model._meta.get_fields()&#xA;    for field in field_list:&#xA;        print(field.name)&#xA;```&#xA;&#xA;```&#xA;LogEntry&#xA;id&#xA;action_time&#xA;user&#xA;content_type&#xA;object_id&#xA;object_repr&#xA;action_flag&#xA;change_message&#xA;Permission&#xA;group&#xA;user&#xA;id&#xA;...&#xA;...&#xA;Blog&#xA;article&#xA;id&#xA;name&#xA;description&#xA;authors&#xA;```&#xA;So, that is how we get all the associated field names in the associated models in our django projects. Also, there are a lot of attributes, we can access with the apps property. The `__dict__.keys()` can be used to get the list of all associated properties or other methods in a class instance.&#xA;&#xA;```&#xA;&gt;&gt;&gt; m[14]._meta.get_fields()[4].__dict__.keys()&#xA;&#xA;dict_keys([&#39;name&#39;, &#39;verbose_name&#39;, &#39;_verbose_name&#39;, &#39;primary_key&#39;, &#39;max_length&#39;, &#39;_unique&#39;, &#39;blank&#39;, &#39;null&#39;, &#39;remote_field&#39;,&#xA; &#39;is_relation&#39;, &#39;default&#39;, &#39;editable&#39;, &#39;serialize&#39;, &#39;unique_for_date&#39;, &#39;unique_for_month&#39;, &#39;unique_for_year&#39;, &#39;choices&#39;, &#xA;&#39;help_text&#39;, &#39;db_index&#39;, &#39;db_column&#39;, &#39;_db_tablespace&#39;, &#39;auto_created&#39;, &#39;creation_counter&#39;, &#39;_validators&#39;, &#39;_error_messages&#39;, &#xA;&#39;error_messages&#39;, &#39;db_collation&#39;, &#39;validators&#39;, &#39;attname&#39;, &#39;column&#39;, &#39;concrete&#39;, &#39;model&#39;])&#xA;```&#xA;In the above example, I am using a list of models and getting the list of all the attributes associated with a field of a model. This can be applied and other properties can be accessed. &#xA;&#xA;## Get Models with a specific app&#xA;&#xA;Let&#39;s say we want all the models associated with a particular application in the project, we can do that by specifying the name of the application.&#xA;&#xA;```python&#xA;from django.apps import apps&#xA;&#xA;app_info = apps.get_app_config(&#39;articles&#39;)&#xA;&#xA;print(app_info.__dict__.keys()&#xA;&#xA;print(app_info.verbose_name)&#xA;&#xA;print(app_info.models)&#xA;&#xA;print(app_info.models[&#39;article&#39;].__dict__.keys())&#xA;```&#xA;&#xA;```&#xA;dict_keys([&#39;name&#39;, &#39;module&#39;, &#39;apps&#39;, &#39;label&#39;, &#39;verbose_name&#39;, &#39;path&#39;, &#39;models_module&#39;, &#39;models&#39;])&#xA;&#xA;&#39;Article&#39;&#xA;&#xA;{&#39;tags&#39;: &lt;class &#39;articles.models.Tags&#39;&gt;, &#39;series&#39;: &lt;class &#39;articles.models.Series&#39;&gt;, &#39;article&#39;: &lt;class &#39;articles.models.Article&#39;&gt;}&#xA;&#xA;dict_keys([&#39;__module__&#39;, &#39;Article_Status&#39;, &#39;__str__&#39;, &#39;get_absolute_url&#39;, &#39;__doc__&#39;, &#39;_meta&#39;, &#39;DoesNotExist&#39;, &#39;MultipleObjectsReturned&#39;, &#39;title&#39;, &#39;description&#39;, &#39;content&#39;, &#39;status&#39;, &#39;get_status_display&#39;, &#39;blog_id&#39;, &#39;blog&#39;, &#39;author_id&#39;, &#39;author&#39;, &#39;timestampedmodel_ptr_id&#39;, &#39;timestampedmodel_ptr&#39;])&#xA;```&#xA;&#xA;So, we can see that we have got the information about the app `articles` in the proejct where we can get the `verbose_name` property to fetch the human-readable format of the article model. Further, we can get all the models associated with the `articles` application. We get back a dict with the model name as the key and the class reference as the value.&#xA;&#xA;We have accessed the `article` model in the `articles` application and fetched all the associated properties or methods in the model.&#xA;&#xA;For further references, you can visit the [django apps documentation](https://docs.djangoproject.com/en/4.0/ref/applications/) to get more relevant methods and properties.</content>
      <type>til</type>
    </item>
    <item>
      <title>Django Blog DevLog: Load Frontmatter data into Template/Model Form Fields</title>
      <link>https://www.meetgor.com/til/django-form-load-frontmatter</link>
      <description>Rendering frontatter from content field into the Template Form field using HTMX and frontmatter libraries</description>
      <pubDate>Mon, 01 Aug 2022 00:00:00 UTC</pubDate>
      <content>## Introduction&#xA;&#xA;I have an Article Form where I load my post into it directly, it might have frontmatter. So what I wish to achieve is when I paste in the content, the frontmatter should be picked up and it should render the form fields like `title`, `description`, and then also remove the frontmatter from the content.&#xA;&#xA;To do that, we will require a model to work with and a form based on that model. We will exclude a few fields from that model so as to process these attributes on the server side. I am working on my Blog project which is a simple Django application.  You can get the source code for the project on the [GitHub repository](https://github.com/mr-destructive/techstructive-blog/).&#xA;&#xA;## Django Project Context&#xA;&#xA;The techstructive-blog is a django project, which has a couple of applications currently, not in a good situation. There are apps like `article`, `blog`, and `user`. This project has templates and static folder in the base directory. The project is deployed on [railway](https://www.railway.app) this is an always under development project, you can check out the [techstructive-blog](https://django-blog.up.railway.app). We can get the bits and pieces of the project details required for understanding what I want to do with the following sections.&#xA;&#xA;### Article Model&#xA;&#xA;We have an `Article` model with attributes like `title`, `description`,  `author` as a Foreign Key to the user model, and a few other attributes which is not related to what we are trying to achieve right now or at least don&#39;t require an explanation. We have a model method called `get_absolute_url` for getting a URL in order to redirect the client when the model instance is created or updated from the backend. You can definitely check out the details of these small components or templates in the project repository. &#xA;&#xA;```python&#xA;# articles/models.py&#xA;&#xA;&#xA;class Article(TimeStampedModel):&#xA;    class Article_Status(models.TextChoices):&#xA;        DRAFT = (&#xA;            &#34;DRAFT&#34;,&#xA;            _(&#34;Draft&#34;),&#xA;        )&#xA;        PUBLISHED = (&#xA;            &#34;PUBLISHED&#34;,&#xA;            _(&#34;Published&#34;),&#xA;        )&#xA;&#xA;    title = models.CharField(max_length=128)&#xA;    description = models.CharField(max_length=256)&#xA;    content = models.TextField(default=&#34;&#34;, null=False, blank=False)&#xA;    status = models.CharField(&#xA;        max_length=16,&#xA;        choices=Article_Status.choices,&#xA;        default=Article_Status.DRAFT,&#xA;    )&#xA;    blog = models.ForeignKey(Blog, on_delete=models.CASCADE, null=True, blank=True)&#xA;    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name=&#34;author&#34;)&#xA;&#xA;    def __str__(self):&#xA;        return self.title&#xA;&#xA;    def get_absolute_url(self):      &#xA;        return reverse(&#39;articles:article-detail&#39;, args=[str(self.id)])&#xA;```&#xA;&#xA;In the below snippet, we have the forms defined in the article application for creating or updating of article instance.  We will be using model forms as our form data should contain fields related to a model in this case the `Article` model. So when we inherit the `forms.ModelForm` in our custom `ArticleForm` we simply need to specify the model and it will take in all the attributes of that model by default, but if we specify the `fields` or `exclude` tuples, it will include only or exclude only the provided list of attributes from the model. &#xA;&#xA;We have also added the widgets for the form fields which will allow us to customize the way the fields in the template/form will render. We can specify the HTML attributes like `width`, `height`, `style`, etc.  &#xA;&#xA;### Article Form&#xA;&#xA;```python&#xA;# article/forms.py&#xA;&#xA;&#xA;from django import forms&#xA;from .models import Article&#xA;&#xA;&#xA;class ArticleForm(forms.ModelForm):&#xA;    class Meta:&#xA;        model = Article&#xA;        exclude = (&#xA;            &#34;created&#34;,&#xA;            &#34;updated&#34;,&#xA;            &#34;author&#34;,&#xA;        )&#xA;        widgets = {&#xA;            &#34;title&#34;: forms.TextInput(&#xA;                attrs={&#xA;                    &#34;class&#34;: &#34;form-control&#34;,&#xA;                    &#34;style&#34;: &#34;max-width: 450px; align: center;&#34;,&#xA;                    &#34;placeholder&#34;: &#34;Title&#34;,&#xA;                }&#xA;            ),&#xA;            &#34;description&#34;: forms.TextInput(&#xA;                attrs={&#xA;                    &#34;class&#34;: &#34;form-control&#34;,&#xA;                    &#34;style&#34;: &#34;max-width: 900px; &#34;,&#xA;                    &#34;placeholder&#34;: &#34;Description&#34;,&#xA;                }&#xA;            ),&#xA;            &#34;content&#34;: forms.Textarea(&#xA;                attrs={&#xA;                    &#34;class&#34;: &#34;form-control post-body&#34;,&#xA;                    &#34;id&#34;: &#34;text-content&#34;,&#xA;                    &#34;style&#34;: &#34;max-width:900px;&#34;,&#xA;                    &#34;hx-post&#34;: &#34;/article/meta/&#34;,&#xA;                    &#34;placeholder&#34;: &#34;Content&#34;,&#xA;                }&#xA;            ),&#xA;            &#34;blog&#34;: forms.Select(&#xA;                attrs={&#xA;                    &#34;class&#34;: &#34;form-control&#34;,&#xA;                    &#34;placeholder&#34;: &#34;Blog Publication&#34;,&#xA;                }&#xA;            ),&#xA;        }&#xA;&#xA;```&#xA;&#xA;So, these are my models and form files in the article app. Using htmx and without any javascript I want to update the form so that it picks up the front matter in the content field which is a text area and fills the title, description other attributes automatically for me. &#xA;&#xA;This can be done in a lot of ways, but I will be sharing one of the ways that I recently used in my blog project. This process involves creating a class-based view and adding a `POST` method that won&#39;t post any data to the backend but will send necessary data to the view.&#xA;&#xA;&#xA;Let&#39;s see the process before diving into any of the code:&#xA;&#xA;## Gist of the Process&#xA;&#xA;- Attach a `hx-post` attribute to the form field for sending the request to a view&#xA;- When the request is sent, the data is loaded with `request.POST`, it is cleaned and converted in python-readable format with json.&#xA;- Once we have the data, we try to use the `frontmatter.loads` function that will load the content and if we have a frontmatter in the text, it will load it as a `frontmatter.POST` object.&#xA;- We will extract `title`, `description`, and other data fields from the object.&#xA;- We will initialize a Form instance of Article, with the initial data values as the extracted data from the frontmatter.&#xA;- Now we have two options:&#xA;    - If the article instance already exists i.e. we are updating the article&#xA;   - Else we are creating a new article&#xA;&#xA;Accordingly, we will load the form in the respective templates i.e. `update.html` for updating the existing articles and `article-form.html` for a new article.&#xA;&#xA;## Adding HTMX Magic&#xA;&#xA;If you&#39;d have seen we have a `hx-post` attribute in the `article/forms.py` file, the `content` widget has a `hx-post` attribute which sends a post request to the `article/meta/` URL route. This route we will bind to the `ArticleMetaView` which we will define in a few moments. This is usually sent once we change a certain text in the content field, so we can modify it as per your requirement with `hx-trigger` that can enable us to specify the trigger event or the type of trigger we want. Further, you can read from the [htmx docs](https://htmx.org/docs/#trigger-modifiers) about these triggers and other attributes. &#xA;&#xA;```python&#xA;# article/urls.py&#xA;&#xA;from django.urls import path&#xA;from . import views&#xA;&#xA;app_name = &#34;articles&#34;&#xA;&#xA;urlpatterns = [&#xA;    path(&#34;&#34;, views.ArticleCreateView.as_view(), name=&#34;article-create&#34;),&#xA;    path(&#34;&lt;int:pk&gt;/&#34;, views.ArticleDetailView.as_view(), name=&#34;article-detail&#34;),&#xA;    path(&#34;delete/&lt;int:pk&gt;/&#34;, views.ArticleDetailView.as_view(), name=&#34;article-delete&#34;),&#xA;    path(&#34;edit/&lt;int:pk&gt;&#34;, views.ArticleDetailView.as_view(), name=&#34;article-update&#34;),&#xA;&#xA;    # the new view that we will create&#xA;    path(&#34;meta/&#34;, views.ArticleMetaView.as_view(), name=&#34;article-meta&#34;),&#xA;]&#xA;```&#xA;&#xA;## Capture Frontmatter Meta-data View &#xA;&#xA;Along with the Create, Detail/List, Update, Delete View, I will create a separate class called `ArticleMetaView` that will fetch the form fields and render the templates again but this time it will fill in the frontmatter meta-data in the fields if the content is parsed with the relvant frontmatter.&#xA;&#xA;```python&#xA;# articles/view.py&#xA;&#xA;class ArticleMetaView(View):&#xA;    model = Article&#xA;&#xA;    def post(self, request, *args, **kwargs):&#xA;        &#xA;        data = json.loads(json.dumps(dict(request.POST)))&#xA;        loaded_frontmatter = frontmatter.loads(data[&#39;content&#39;][0])&#xA;&#xA;       # frontmatter has keys i.e. attributes like title, description, etc.&#xA;        if dict(loaded_frontmatter):&#xA;            article_title = loaded_frontmatter[&#39;title&#39;]&#xA;            article_description = loaded_frontmatter[&#39;description&#39;]&#xA;            form = ArticleForm(initial={&#39;title&#39;: article_title, &#xA;            &#39;description&#39;: article_description, &#39;content&#39;: loaded_frontmatter.content})&#xA;            context = {&#39;form&#39;: form}&#xA;            article_list = Article.objects.filter(title=article_title)&#xA;            if article_list:&#xA;                article = article_list.last()&#xA;                context[&#39;article&#39;] = article&#xA;                return render(request, &#39;articles/edit_article.html&#39;, context)&#xA;            return render(request, &#39;articles/article_form.html&#39;, context)&#xA;&#xA;        article_list = Article.objects.filter(title=data[&#39;title&#39;][0])&#xA;       &#xA;       # if the article title has been already taken i.e. we are updating an article&#xA;&#xA;        if article_list:&#xA;            article = article_list.last()&#xA;            form = ArticleForm(data=request.POST)&#xA;            context = {&#39;form&#39;: form}&#xA;            context[&#39;article&#39;] = article&#xA;            return render(request, &#39;articles/edit_article.html&#39;, context)&#xA;&#xA;        form = ArticleForm(data=request.POST)&#xA;        context = {&#39;form&#39;: form}&#xA;        return render(request, &#39;articles/article_form.html&#39;, context)&#xA;&#xA;```&#xA;&#xA;In the above `ArticleMetaView` we have created a `post` method as we want to get hold of the content from the form. So, we start by extracting and converting the `request.body` data into an appropriate type for easily working with python. So, the `request.body` will contain the data like `csrf_token`, `form_data`, etc. received from the frontend template. We store the received data as `data` and now from this data, we can load the content field which will have the content information.&#xA;&#xA;Firstly we will extract the `request.body` which will contain the data from the form as we have made a `POST` request to this endpoint. For doing that we need to parse the content in a apropriate format such that it is python friendly. So we wrap the `request.body` into json format and then decode it back into the json string. This will give us the dict of the request data.&#xA;&#xA;```python&#xA;data = json.loads(json.dumps(dict(request.POST)))&#xA;```&#xA;&#xA;```&#xA;{&#39;csrfmiddlewaretoken&#39;: [&#39;bSYJxD39XH509tD1tZGd0WU21PUaKaLeqjjGbyzRvLXF4P8iIxb5l0fmTWVFjELQ&#39;], &#39;title&#39;: [&#39;test2&#39;], &#39;description&#39;: [&#39;test&#39;], &#39;content&#39;: [&#39;test something&#39;], &#39;status&#39;: [&#39;DRAFT&#39;], &#39;blog&#39;: [&#39;&#39;]}&#xA;```&#xA;&#xA;So, this will grab the request data as a dict, we can then extract the data from this as it has data from the Form fields. We are interested in the content field in the Form, so we can get it by specifying the key `content` from the extracted data. But as we can see the data doesn&#39;t contain the actual data instead it is wrapped in a list i.e. `[&#39;test something&#39;]`, so we will have to index it and then fetch it.&#xA;&#xA;```python&#xA;content_string = data[&#39;content&#39;][0]&#xA;```&#xA;&#xA;This will give us the exact content field as a string. So, we can now move into extracting the frontmatter from the fields. &#xA;&#xA;Now, we can use the [frontmatter](https://python-frontmatter.readthedocs.io/en/latest/index.html) library to parse the content into the [loads](https://python-frontmatter.readthedocs.io/en/latest/api.html#frontmatter.loads) funciton and extract the frontmatter if it is present in the content field. The frontmatter library has a `loads` function which takes in a string and can give out a [frontmatter.Post](https://python-frontmatter.readthedocs.io/en/latest/api.html#post-objects) object. The loads function is differnet from the [load](https://python-frontmatter.readthedocs.io/en/latest/api.html#frontmatter.load) function as the load frunciton is for reading data from a stream of bytes i.e. a file or othe related byte object. The differnece is subtle but it took a read at the [documentation](https://python-frontmatter.readthedocs.io/en/latest/api.html#module-frontmatter).&#xA;&#xA;```python&#xA;post = data[&#39;content&#39;][0]&#xA;loaded_frontmatter = frontmatter.loads(post)&#xA;```&#xA;&#xA;This wil load the content and give us a `frontmatter.Post` as said earlier. This will contain a dict with all the frontmatter if it has any and will by default parse the non-frontmatter data i.e. the remaining text into the `content` key. We need a chack if the Form field had any fronmatter this can be checked by the `dict(loaded_frontmatter)` which will return None if it cannot load the frontmatter.&#xA;&#xA;```python&#xA;loaded_frontmatter = frontmatter.loads(data[&#39;content&#39;][0])&#xA;if dict(loaded_frontmatter):&#xA;  print(loaded_frontmatter.keys())&#xA;```&#xA;&#xA;```&#xA;dict_keys([&#39;templateKey&#39;, &#39;title&#39;, &#39;description&#39;, &#39;date&#39;, &#39;status&#39;])&#xA;```&#xA;&#xA;So once we have the frontmatter loaded we can get specific keys from it and initialize the form vaues to them. But we have made clear distictions that we want to perform a specific task if we have frontmatter keys in the content field of the Form else we can do something else.&#xA;&#xA;First let&#39;s handle the loading of the frontmatter into the form. For doing that we will get all the required attributes from the frontmatter like `title`, `description`, `content`, etc which can be accessed normally as we extract the value from a key in a dict.&#xA;&#xA;Once we have got those keys, we can start filling in the Form data with initial values. The [Django Model form](https://docs.djangoproject.com/en/4.0/topics/forms/modelforms/) takes in a parameter like [initial](https://docs.djangoproject.com/en/4.0/topics/forms/modelforms/#providing-initial-values) which can be a dict of the fiields along with the value that can be used for rendering the form initially when we load the template.&#xA;&#xA;```python&#xA;article_title = loaded_frontmatter[&#39;title&#39;]&#xA;article_description = loaded_frontmatter[&#39;description&#39;]&#xA;&#xA;form = ArticleForm(initial={&#39;title&#39;: article_title, &#39;description&#39;: article_description, &#39;content&#39;: loaded_frontmatter.content})&#xA;```&#xA;&#xA;This will take in a `ArticleForm` and fill the initial values like `title`, `description`, etc which we have provided in the dict with the values. Now, we need to parse this form in the current template or re-render the template. But before that, we need to parse this context into the template. We will create a dict with `form` as the key which can be used to render in the template.&#xA;&#xA;Also, we have a two ways here, either the user is creating a new article or it is updating a existing article. We need to make sure that we preserve the initial fields in the form as we are updating the existing article. So, we can filter the article objects as per the title of the current title and then if we find a article with that title, we will parse the context with that article object.&#xA;&#xA;```python&#xA;article_list = Article.objects.filter(title=article_title)&#xA;if article_list:&#xA;    article = article_list.last()&#xA;    context{&#xA;      &#39;form&#39;: form,&#xA;      &#39;article&#39;: article&#xA;    }&#xA;    return render(request, &#39;articles/edit_article.html&#39;, context)&#xA;context = {&#39;form&#39;: form}&#xA;return render(request, &#39;articles/article_form.html&#39;, context)&#xA;```&#xA;&#xA;Now, we have form data along with the article instance used for rendering the form with appropriate content. So, this will work for editing an already existing article. For a new article, we have to simply parse the form to the template and it will render the title picked from the fotnmatter or leave it empty.&#xA;&#xA;Similarly, for the article with no frontmatter we will iterate over the article and if the article&#39;s title already exist, we will render the article data with the form else render the form with the parsed title and other meta-data in the form.&#xA;&#xA;&#xA;&lt;video width=&#34;800&#34; height=&#34;450&#34; controls&gt;&#xA;  &lt;source src=&#34;https://res.cloudinary.com/techstructive-blog/video/upload/v1659370006/blog-media/frontmatter-load-htmx.mp4&#34; type=&#34;video/mp4&#34;&gt;&#xA;&lt;/video&gt;&#xA;&#xA;So that is how we render the form data with frontmatter into appropriate meta-data in the form. We have used Django forms and make use of HTMX for the dynamic updation of form.</content>
      <type>til</type>
    </item>
    <item>
      <title>DJ Notes</title>
      <link>https://www.meetgor.com/projects/dj-notes</link>
      <description>A simple note app with core django and postgres</description>
      <pubDate>Sat, 25 Jun 2022 00:00:00 UTC</pubDate>
      <content>&#xA;&#xA;A note taking application with features like markdown, syntax highlighting and filtering.&#xA;&#xA;Tech Stack:&#xA;- Django&#xA;&#xA;</content>
      <type>projects</type>
    </item>
    <item>
      <title>Django App from Scratch Using Docker with Debian Base Image</title>
      <link>https://www.meetgor.com/til/django-app-from-scratch</link>
      <description>Creating a django basic application with configuration for static files, templates and user authentication using docker and debian base image.</description>
      <pubDate>Tue, 24 May 2022 00:00:00 UTC</pubDate>
      <content>## Pull a Fresh Debian Image&#xA;&#xA;Create a docker container from a Debian image, the following command can be used to pull a debain 11-slim image and create a container of it, also enter into the container in a interactive environment `-it` mode. &#xA;&#xA;```&#xA;docker run -v $(pwd):/var/www --rm -it -p 8001:80 debian:11-slim&#xA;```&#xA;&#xA;## Create a Django App from Shell script&#xA;&#xA;Now, since we are inside a Debian container, we can enter a few commands, you can refer to the Mark Gibney&#39;s [GitHub repository](https://github.com/no-gravity/web_app_from_scratch) for the script.&#xA;&#xA;```&#xA;apt update&#xA;&#xA;apt install wget&#xA;&#xA;wget https://raw.githubusercontent.com/no-gravity/web_app_from_scratch/main/django.sh&#xA;```&#xA;&#xA;Also, if you want to do a few adjustments, you can install an editor, get used to vim or use nano.&#xA;&#xA;```&#xA;apt install vim &#xA;&#xA;OR&#xA;&#xA;apt install nano&#xA;```&#xA;&#xA;```&#xA;chmod +x django.sh&#xA;bash django.sh&#xA;```&#xA;&#xA;I also have a few adjustment of the original script, that accepts a project name and creates a django project based on the positional parameter given to it. You can get it from the [quick-setup-script repository](https://github.com/Mr-Destructive/quick-setup-scripts/blob/main/django_docker.sh) or directly the [script](https://raw.githubusercontent.com/Mr-Destructive/quick-setup-scripts/main/django_docker.sh).&#xA;&#xA;To use the above file, you need to execute the command as :&#xA;&#xA;```&#xA;chmod +x django_docker.sh&#xA;bash django_docker.sh &lt;project_name&gt;&#xA;```&#xA;&#xA;This will generate the project in the `/var/www/` folder with the name of the project. The script will prompt you with a few things for setting up at some iterations like basic application setup , `static file` configuration, `basic tempalte` setup and the `user authentication` setup.&#xA;&#xA;## Copy the contents from the docker container&#xA;&#xA;You can copy the contents of the folder into your local machine by entering the [cp](https://docs.docker.com/engine/reference/commandline/cp/) command in docker. &#xA;&#xA;```&#xA;docker cp &lt;container_id&gt;:/var/www/&lt;project_name&gt; /path/in_local_machine/&#xA;```&#xA;&#xA;This will copy the project in the docker container into the local machine for you to extend and tweak it as per your needs.&#xA;&#xA;That&#39;s a basic Django Project Setup by using Docker with a Debian Image.</content>
      <type>til</type>
    </item>
    <item>
      <title>Dockerize a Django project</title>
      <link>https://www.meetgor.com/til/dockerize-django-prj</link>
      <description>Dockerize a Django project We can run our Django projects in a Docker Container by creating a Docker image for our project. It is really easy and intuitive to c</description>
      <pubDate>Wed, 02 Mar 2022 00:00:00 UTC</pubDate>
      <content>## Dockerize a Django project&#xA;&#xA;We can run our Django projects in a Docker Container by creating a Docker image for our project. It is really easy and intuitive to create a Dockerfile for any given application as it really is a matter of writing commands in a file and basically running it is a isolated environment. To create a Docker image, we first need a Dockerfile. A Dockerfile is simply a Blueprint to create a image in Docker. In this file we specify the instructions/commands/environment variables to create a image for our app to run. &#xA;&#xA;### Create a Docker File&#xA;&#xA;To create a dockerfile, simply create a file named as `Dockerfile` without any extension(not a .txt file). We need to then add the following code into the file. &#xA;&#xA;```dockerfile&#xA;FROM python:3.9-buster&#xA;&#xA;ENV PYTHONUNBUFFERED=1&#xA;&#xA;WORKDIR /code&#xA;&#xA;COPY requirements.txt .&#xA;&#xA;RUN pip install -r requirements.txt&#xA;&#xA;COPY . .&#xA;&#xA;EXPOSE 8000&#xA;&#xA;CMD [&#34;python&#34;,&#34;manage.py&#34;,&#34;runserver&#34;,&#34;0.0.0.0:8000&#34;]&#xA;```&#xA;&#xA;- Let&#39;s see what are we doing here, first we are pulling a base image of Python in this case it is [3.9-buster](https://github.com/docker-library/python/blob/a4b368154b7e3c33c76385f1be7a998fcf3123eb/3.9/buster/Dockerfile), but it can be any of the [Official Python images](https://hub.docker.com/_/python) from [dockerhub](https://hub.docker.com). It is simply a environment for our Django project to run. You can even use linux environments like ubuntu, debian or alpine. &#xA;&#xA;- Next we add a `PYTHONUNBUFFERED` as a environment variable and initialize it to `1`, which basically allows to parse python output straight into the terminal without actually buffering first. &#xA;&#xA;- We set the `WORKDIR` as the `code` directory, this sets the provided directory as the base to run any command or instructions in the Dockerfile. &#xA;&#xA;- We then `COPY` the `requirements.txt` file in the current(`.`) directory which is `code` as mentioned earlier. We simply want the `requirements.txt` file in the current directory so we can proceed with the next step which is to install the dependencies for the Django project&#xA;&#xA;- After copying the `requirements.txt` file, we simply install the packages mentioned int the file with `pip` we use the `RUN` command to execute any shell commands in the environment. &#xA;&#xA;- Next step is to `COPY` all the files from the current folder(local machine) into the docker environment image. So, we have the source code files in the Container. &#xA;&#xA;- Expose the port `8000`(default) or any other port you would want to run Django in the image. We use the expose command in complement to the `-p` argument when we want to create the container. I have explained the [port-forwarding](https://mr-destructive.github.io/techstructive-blog/docker-port-forward) concept in the previous TIL. &#xA;&#xA;- Finally we can run the server. We need to parse every command as a comma separated string in the list to the `CMD` which the container actually executes the command when the image is build. So, all the commands previously were to build an image for the django project, the `CMD` will actually run the server in a container after building the image of the django project.  &#xA;&#xA;### Build the image&#xA;&#xA;Using the Dockerfile, we can create the image for the Django project by a simple command.&#xA;&#xA;```&#xA;docker build . -t django-app&#xA;```&#xA;&#xA;Assuming the Dockerfile is in the same folder from which you are running this command.&#xA;&#xA;![Build the django-app image](https://res.cloudinary.com/techstructive-blog/image/upload/v1646230907/blog-media/jj04subyvkuvfb5obytu.png)&#xA;&#xA;![Build Complete Success](https://res.cloudinary.com/techstructive-blog/image/upload/v1646230988/blog-media/ugjoakqgyhiwelqkyaat.png)&#xA;&#xA;### Run the Image and Create a Container&#xA;&#xA;```&#xA;docker run -p 8000:8000 django-app&#xA;```&#xA;&#xA;![Create Container](https://res.cloudinary.com/techstructive-blog/image/upload/v1646231023/blog-media/yneuz46burorz4b5vzp4.png)&#xA;&#xA;You can use any port like `3000` or `4000` on your local system by changing the first port number as `3000:8000` and so on. So, the first port number is the local system&#39;s port number and the port number followed by a `:` is the port number for the environment inside the container of the django project. The container port is the same which we have `exposed` while creating the image. You can now go to [127.0.0.1:8000](http://127.0.0.1:8000) or your chosen port in your local environment and see the django app running. &#xA;&#xA;If you are using a Docker as a Virtual Machine, you need to find the IP-address of that Virtual-Machine(`docker-machine ip`) and use that instead of `localhost` or `127.0.0.1`. &#xA;&#xA;&#xA;### Cleaning the Container for iterations&#xA;&#xA;We need to stop the container to run it again after making a few changes in the app if any. Simply pressing the `CTRL + D` option wont stop the container here. We need to stop the container by executing: &#xA;&#xA;```&#xA;docker ps&#xA;&#xA;docker stop &lt;container-id&gt;&#xA;```&#xA;&#xA;This will stop the container. Also if you want to remove the images you built, you can run the command:&#xA;&#xA;```&#xA;docker image ls&#xA;&#xA;docker rmi -f &lt;image-id&gt;&#xA;```&#xA;&#xA;  So, we now have a image of our Django project, this image can be used by anyone inside a docker environment and thus creating much more easier to test/work on a given project irrespective of the system is being used.&#xA;</content>
      <type>til</type>
    </item>
    <item>
      <title>BackChat</title>
      <link>https://www.meetgor.com/projects/backchat</link>
      <description>A simple chat app with django-channels and HTMX</description>
      <pubDate>Mon, 31 Jan 2022 00:00:00 UTC</pubDate>
      <content>&#xA;&#xA;A simple chat/messaging app created for exploring HTMX and django-channels.&#xA;&#xA;Tech Stack:&#xA;- Django&#xA;- Django-Channels&#xA;- Daphne&#xA;- HTMX&#xA;&#xA;</content>
      <type>projects</type>
    </item>
    <item>
      <title>DevQuotes</title>
      <link>https://www.meetgor.com/projects/devquotes</link>
      <description>Dev quotes is a platform for developers to write inspireing quotes.</description>
      <pubDate>Wed, 25 Aug 2021 00:00:00 UTC</pubDate>
      <content>&#xA;&#xA;A simple quote/post sharing app created for exploring Django.&#xA;&#xA;Tech Stack:&#xA;- Django&#xA;- Bootstrap&#xA;&#xA;&#xA;</content>
      <type>projects</type>
    </item>
    <item>
      <title>World Atlas</title>
      <link>https://www.meetgor.com/projects/world-atlas</link>
      <description>A Geographical based word game.</description>
      <pubDate>Sun, 25 Jul 2021 00:00:00 UTC</pubDate>
      <content>&#xA;&#xA;A simple word game using some django in the backend and javascript for frontend. &#xA;&#xA;Tech stack:&#xA;1. Django&#xA;2. Django-Rest-Framework&#xA;3. Javascript&#xA;&#xA;Django rest framework as an api for most of the operations in the game like:&#xA;&#xA;1. Validating a user input place from database&#xA;2. Listing out all places from a given letter&#xA;3. Get a random place from a given letter&#xA;4. CRUD endpoint for the places in the database.&#xA;&#xA;API Link: https://djangonaut.pythonanywhere.com/world-atlas/api/&#xA;</content>
      <type>projects</type>
    </item>
  </channel>
</rss>