<?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>Forem: Anandhu Prakash</title>
    <description>The latest articles on Forem by Anandhu Prakash (@anandhuprakash).</description>
    <link>https://forem.com/anandhuprakash</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%2F861013%2Ff84b3bd4-20ea-4fe6-b59d-f803a859be1b.jpeg</url>
      <title>Forem: Anandhu Prakash</title>
      <link>https://forem.com/anandhuprakash</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anandhuprakash"/>
    <language>en</language>
    <item>
      <title>Python IN Browser</title>
      <dc:creator>Anandhu Prakash</dc:creator>
      <pubDate>Sun, 15 May 2022 04:49:50 +0000</pubDate>
      <link>https://forem.com/anandhuprakash/python-in-browser-1c6a</link>
      <guid>https://forem.com/anandhuprakash/python-in-browser-1c6a</guid>
      <description>&lt;h2&gt;
  
  
  What is PyScript?
&lt;/h2&gt;

&lt;p&gt;Python in the browser! PyScript is a framework that allows users to create rich Python applications in the browser using HTML's interface and the power of Pyodide, WASM, and modern web technologies. The PyScript framework provides users at every experience level with access to an expressive, easy-to-learn programming language with countless applications.&lt;/p&gt;

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

&lt;p&gt;Output:&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Installation of PyScript:
&lt;/h2&gt;

&lt;p&gt;Just Kidding! There is no installation for pyscript. Just add these commands to your html page.&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;link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" /&amp;gt;
&amp;lt;script defer src="https://pyscript.net/alpha/pyscript.js"&amp;gt;&amp;lt;/script&amp;gt;

&amp;lt;py-script&amp;gt;
    print("Hello World!!!")#Your Python Code
&amp;lt;/py-script&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;Note Py-Script is still in Alpha Version.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>python</category>
      <category>html</category>
    </item>
    <item>
      <title>Add the slug field inside Django Model</title>
      <dc:creator>Anandhu Prakash</dc:creator>
      <pubDate>Wed, 11 May 2022 14:53:30 +0000</pubDate>
      <link>https://forem.com/anandhuprakash/add-the-slug-field-inside-django-model-5cd9</link>
      <guid>https://forem.com/anandhuprakash/add-the-slug-field-inside-django-model-5cd9</guid>
      <description>&lt;p&gt;&lt;strong&gt;## What is Slug Field in Django?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is a way of generating a valid URL, generally using data already obtained. For instance, using the title of an article to generate a URL. Let’s assume our blog have a post with the title &lt;code&gt;My First Post&lt;/code&gt; with primary key id= 2. We might refer to this post with&lt;/p&gt;

&lt;p&gt;&lt;code&gt;www.example.com/posts/2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Or, we can reference the title like&lt;/p&gt;

&lt;p&gt;&lt;code&gt;www.example.com/posts/my first post&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But the problem is spaces are not valid in URLs, they need to be replaced by %20 which is ugly, making it the following&lt;/p&gt;

&lt;p&gt;&lt;code&gt;www.example.com/posts/my%20first%20post&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;But it is not solving meaningful URL. Another option can be&lt;/p&gt;

&lt;p&gt;&lt;code&gt;www.example.com/posts/my-first-post&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So, the slug is now my-first-post. All letters are down cased and spaces are replaced by hyphens -. These URLs are extremely SEO Friendly.&lt;/p&gt;

&lt;p&gt;Assume that our Blog Post models look similar to this.&lt;br&gt;
Adding Slugify to our project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)class Post(models.Model):
title = models.CharField(max_length = 250)
slug = models.SlugField(max_length = 250, null = True, blank = True)
text = models.TextField()
published_at = models.DateTimeField(auto_now_add = True)
updated = models.DateTimeField(auto_now = True)status = models.CharField(max_length = 10, choices = STATUS_CHOICES,
             default ='draft')class Meta:
 ordering = ('-published_at', )def __str__(self):
 return self.title
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to find a way to convert the title into a slug automatically. We want this script to be triggered every time a new instance of Post model is created. For this purpose, we will use signals.&lt;/p&gt;

&lt;p&gt;Note: Add new file util.py in the same directory where settings.py file is saved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import string, random
from django.db.models.signals import pre_save
from django.dispatch import receiver
from django.utils.text import slugify
def random_string_generator(size = 10, chars = string.ascii_lowercase + string.digits):
 return ''.join(random.choice(chars) for _ in range(size))def unique_slug_generator(instance, new_slug = None):
 if new_slug is not None:
  slug = new_slug
 else:
  slug = slugify(instance.title)
 Klass = instance.__class__
 max_length = Klass._meta.get_field('slug').max_length
 slug = slug[:max_length]
 qs_exists = Klass.objects.filter(slug = slug).exists()

 if qs_exists:
  new_slug = "{slug}-{randstr}".format(
   slug = slug[:max_length-5], randstr = random_string_generator(size = 4))

  return unique_slug_generator(instance, new_slug = new_slug)
 return slug
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  &lt;strong&gt;Signals in Django:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In many cases when there is a modification in a model’s instance we need to execute some action. Django provides us with an elegant way to handle these situations. The signals are utilities that allow associating events with actions. We can develop a function that will run when a signal calls it.&lt;br&gt;
In models.py file of posts app where Post Model was defined, add this in the same file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@receiver(pre_save, sender=Post)
def pre_save_receiver(sender, instance, *args, **kwargs):
if not instance.slug:
 instance.slug = unique_slug_generator(instance)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pre_save_receiver function should be placed separately outside the Post model.&lt;/p&gt;

&lt;p&gt;Note: In urls.py edit detail path with path(‘posts/’, detail). In views.py edit the detail function with&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def detail(request, slug):
 q = Post.objects.filter(slug__iexact = slug)
if q.exists():
 q = q.first()
else:
 return HttpResponse('&amp;lt;h1&amp;gt;Post Not Found&amp;lt;/h1&amp;gt;')
context = {'post': q
}
return render(request, 'posts/details.html', context)

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

&lt;/div&gt;



&lt;p&gt;The last step is to add the link in HTML file &lt;a href="%E2%80%9D/posts/%7B%7B"&gt;View&lt;/a&gt;. Now we are ready to go to 127.0.0.1:8000/posts/title-you-have-added and it will show you the page details.html.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Note: You Might have to import the modules into your models.py too.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Thats It!&lt;/p&gt;




&lt;p&gt;Now your Blog will automatically convert title into slug.&lt;/p&gt;




</description>
      <category>django</category>
      <category>python</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
