<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by ArRosid on Medium]]></title>
        <description><![CDATA[Stories by ArRosid on Medium]]></description>
        <link>https://medium.com/@arrosid?source=rss-2bd4b3ed90eb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*VfrqeFWepQ7DT1Nl.</url>
            <title>Stories by ArRosid on Medium</title>
            <link>https://medium.com/@arrosid?source=rss-2bd4b3ed90eb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 00:38:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arrosid/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to git]]></title>
            <link>https://medium.com/@arrosid/introduction-to-git-9bce16e3218?source=rss-2bd4b3ed90eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/9bce16e3218</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[linux]]></category>
            <dc:creator><![CDATA[ArRosid]]></dc:creator>
            <pubDate>Sat, 11 Apr 2020 02:54:15 GMT</pubDate>
            <atom:updated>2020-04-11T02:54:15.870Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/450/0*o3vc9KSa-XagxcvW.png" /></figure><p>I believe that many of you already familiar with git. But may be there are someone who don’t know about git yet. That’s why I will cover about git in this story.</p><p>Git is a version control tool that records changes in files overtime. This allows programmers to know exactly about the history of the project. So when in the future there are a bug in the projects, the programmer can identify it easily.</p><p>Also the power of git is that we can revert to the earlier version of our project. So we can doing coding without worry breaking our project. If there are an error in our project caused by the new script, we can easily revert back the project to the earlier version.</p><p>Okay let’s just start learning about git. To install git, we can go to <a href="https://www.git-scm.com/downloads">https://www.git-scm.com/downloads</a> and click on the OS that you used. In this case, I will show you how to install git on Ubuntu.</p><p>First we need to add PPA repository</p><pre>sudo add-apt-repository ppa:git-core/ppa</pre><p>Now we just need to update and install git</p><pre>sudo apt update<br>sudo apt install git</pre><p>After we have git installed on our computer, we can simply type git — version to check the version of git</p><pre>git --version</pre><p>Okay, Now we have successfully install git on our computer. In the next story, I will show you how to use git. See you on the next story!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bce16e3218" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy Scrapy Project on ScrapingHub]]></title>
            <link>https://medium.com/@arrosid/deploy-scrapy-project-on-scrapinghub-9b29391cddaf?source=rss-2bd4b3ed90eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/9b29391cddaf</guid>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[ArRosid]]></dc:creator>
            <pubDate>Wed, 11 Mar 2020 07:02:20 GMT</pubDate>
            <atom:updated>2020-03-11T07:02:20.523Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uQWY_sHlqwSb6fcL.png" /></figure><p>Hi, How are you doing?</p><p>In this article, We will learn how to deploy our scrapy project to ScrapingHub. Let’s just start!</p><p>First, you need to create free account at <a href="https://scrapinghub.com/">https://scrapinghub.com</a></p><p>After that, create a new project</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zKiXZkJLkNt9InCmckupQw.png" /></figure><p>After create a new project, you will have a step by step how to setup scrapinghub to your computer. Just follow it! (You can find API Key on profile -&gt; Account settings -&gt; API Key)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h9-Y2x3Cu4_z_T_XVVNS7w.png" /></figure><p>Now we have our project deployed on ScrapingHub</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*muTviE1At65q0xzbG3HGhg.png" /></figure><p>Let’s try to run our spider</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jfd-ObK1vRZGQ-BvOICoJQ.png" /></figure><p>Here we have our running jobs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WkbJTmOslhHdnPjvNi2kIA.png" /></figure><p>Here we have our running jobs finished</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*d27VIzOa3B1gC-P_PjS-fw.png" /></figure><p>Here is the statistic</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JrSj6BFRtMapBvIgbw-DFQ.png" /></figure><p>We also can export the result</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GnAmy7hVCG6g7Tuj8WPPXA.png" /></figure><p>Okay we now have our project successfully deployed and running on ScrapingHub. But we have limitations in the free version, we can’t create schedulers to run our scrapy. But don’t worry, We have another option for that. I will show you in the next article, stay tune!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9b29391cddaf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to export scrapy result to SQL]]></title>
            <link>https://medium.com/@arrosid/how-to-export-scrapy-result-to-sql-52cb27ee2b69?source=rss-2bd4b3ed90eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/52cb27ee2b69</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[scrapy]]></category>
            <category><![CDATA[scraping]]></category>
            <category><![CDATA[data-scraping]]></category>
            <dc:creator><![CDATA[ArRosid]]></dc:creator>
            <pubDate>Wed, 11 Mar 2020 00:21:42 GMT</pubDate>
            <atom:updated>2020-03-11T00:21:42.056Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CWn_JM8YhoFT0V2R.png" /></figure><p>Hi, In this article, I will show you how to export scrapy result to SQL.</p><p>Let’s just start! I will use scrapy project that I have created before, You can use yours If you have scrapy project. If no, you can use mine in github <a href="https://github.com/ArRosid/scrapy-tokopedia">https://github.com/ArRosid/scrapy-tokopedia</a></p><p>First, you need to create models.py file inside project directory</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*Us7tx0AqcZZitRsg0DMcmw.png" /></figure><p>This is the script in the models.py file</p><p>&lt;script src=”<a href="https://gist.github.com/ArRosid/83d5a604f6d7d9fd843d400818b7ca16.js">https://gist.github.com/ArRosid/83d5a604f6d7d9fd843d400818b7ca16.js</a>&quot;&gt;&lt;/script&gt;</p><p>Above script is to define the database table we use.</p><p>Next edit pipelines.py file</p><p>&lt;script src=”<a href="https://gist.github.com/ArRosid/500bbe46d39fa99e92d6bfba6c824525.js">https://gist.github.com/ArRosid/500bbe46d39fa99e92d6bfba6c824525.js</a>&quot;&gt;&lt;/script&gt;</p><p>Last, add following script to settings.py</p><pre>ITEM_PIPELINES = {<br>   &#39;tokopedia.pipelines.SaveDiscountsPipeline&#39;: 200,<br>   &#39;tokopedia.pipelines.TokopediaPipeline&#39;: 300,<br>}</pre><pre>CONNECTION_STRING = &quot;sqlite:///scrapy_tokopedia.db&quot;</pre><p>Okay, Now let’s run the spider by this command</p><pre>scrapy crawl promo</pre><p>You will have new sql3 file named scrapy_tokopedia.db. You can open this file using sqlite extension in VSCode</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6VDbtiWYZuDOoxGJuIdBlw.png" /></figure><p>Okay that’s all I have for you today, Hope you can find this useful. Have a nice day!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=52cb27ee2b69" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scrapy Project 1 — Part 1]]></title>
            <link>https://medium.com/@arrosid/scrapy-project-1-part-1-44e634047437?source=rss-2bd4b3ed90eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/44e634047437</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[scraping]]></category>
            <category><![CDATA[scrapy]]></category>
            <dc:creator><![CDATA[ArRosid]]></dc:creator>
            <pubDate>Sat, 16 Nov 2019 09:56:50 GMT</pubDate>
            <atom:updated>2019-11-16T10:13:29.265Z</atom:updated>
            <content:encoded><![CDATA[<h3>Scrapy Project 1 — Part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*_9uYgSmBojfiDap8.png" /></figure><p>Hi guys! in this story we will try to create our first scrapy project. We will doing scraping to <a href="https://www.worldometers.info/">https://www.worldometers.info/</a>. Without any long story, let’s just start our project</p><p>First we need to install scrapy library</p><pre>pip install scrapy</pre><p>Now let’s create our first scrapy project</p><pre>scrapy startproject worldmeters</pre><p>After creating the project, we will get the directory like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*1HNgGYnZTQlvttGQahj0hg.png" /></figure><p>I will not tell you strange term about scrapy for now. I believe you will understand after complete the project.</p><p>Now let start our first spider in this project</p><pre>cd worldmeters</pre><pre>scrapy genspider countries <a href="http://www.worldometers.info/world-population/population-by-country">www.worldometers.info/world-population/population-by-country</a></pre><p>After typing that command, we will get new file named countries like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*mMsICIFqCAVxDVqlECSkrA.png" /></figure><p>The countries.py file will be like this</p><pre># -*- coding: utf-8 -*-<br>import scrapy</pre><pre>class CountriesSpider(scrapy.Spider):<br>    name = &#39;countries&#39;<br>    allowed_domains = [&#39;<a href="http://www.worldometers.info/world-population/population-by-country&#39;">www.worldometers.info/world-population/population-by-country&#39;</a>]<br>    start_urls = [&#39;<a href="http://www.worldometers.info/world-population/population-by-country/&#39;">http://www.worldometers.info/world-population/population-by-country/&#39;</a>]</pre><pre>def parse(self, response):<br>        pass</pre><p>Let’s edit the file like this</p><pre># -*- coding: utf-8 -*-<br>import scrapy</pre><pre>class CountriesSpider(scrapy.Spider):<br>    name = &#39;countries&#39;<br>    allowed_domains = [<strong>&#39;</strong><a href="http://www.worldometers.info&/#39;"><strong>www.worldometers.info&#39;</strong></a>]<br>    start_urls = [&#39;<a href="https://www.worldometers.info/world-population/population-by-country/&#39;"><strong>https</strong>://www.worldometers.info/world-population/population-by-country/&#39;</a>]</pre><pre>def parse(self, response):<br>        pass</pre><p>Now let’s try to go to scrapy shell by typing</p><pre>scrapy shell</pre><p>In this scrapy shell, we can doing some testing.</p><pre>&gt;&gt;&gt; <strong>r = scrapy.Request(url=&quot;</strong><a href="https://www.worldometers.info/world-population/population-by-country/"><strong>https://www.worldometers.info/world-population/population-by-country/</strong></a><strong>&quot;)</strong><br>&gt;&gt;&gt; <strong>fetch(r)</strong><br>2019-11-16 17:04:32 [scrapy.core.engine] DEBUG: Crawled (200) &lt;GET <a href="https://www.worldometers.info/world-population/population-by-country/">https://www.worldometers.info/world-population/population-by-country/</a>&gt; (referer: None)<br>&gt;&gt;&gt;</pre><p>Above we doing request to the worldmeters url. We can see the result of the request by typing</p><pre>response.body</pre><p>We will get output something like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZoSVkkAYSk4YBTyqC5yp1w.png" /></figure><p>Above is the original source of the urls that we request. We can also see the result of our request in the browser by typing</p><pre>view(response)</pre><p>After typing that command, our browser will automatically open and we will get something like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ksLPQ5EyvY4iarbP9CF1RA.png" /></figure><p>Okay this is the end of this story, In the next story we will try to doing basic element selection in scrapy. Se you in the next stories!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44e634047437" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Introduction to Pandas Python]]></title>
            <link>https://medium.com/@arrosid/introduction-to-pandas-python-77d6b0298f31?source=rss-2bd4b3ed90eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/77d6b0298f31</guid>
            <category><![CDATA[pandas]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[ArRosid]]></dc:creator>
            <pubDate>Sat, 02 Nov 2019 23:59:58 GMT</pubDate>
            <atom:updated>2019-11-02T23:59:58.923Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*r_8O6Lk7IWxdPatc" /></figure><p>In this story we will talk about pandas. What is Pandas?</p><p><em>Pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the </em><a href="https://www.python.org/"><em>Python</em></a><em> programming language.</em></p><p>Pandas is widely use for data science. Let’s take an example</p><p>We have data about weather, and we got some question.</p><ul><li>When the event is rain?</li><li>When is the highest temperature?</li><li>When is the lowest temperature?</li></ul><p>Actually we can use excel to get the answer of that questions. But if our data is big, excel is not useful anymore. That’s why we need pandas python.</p><p>Let’s take a look how we can use pandas to get answer from that questions</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YWV-2r7jQIIY9HBYpoh-MA.png" /></figure><p>Above we import pandas library and read the csv file.</p><p>Let’s get the answer of first question “When the event is rain?”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/505/1*jKwQPmJIYaGy5l7q9l6HYA.png" /></figure><p>Very easy right? Let’s get the answer of second question</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*sRJSkziWwEf4goiGVC5qGg.png" /></figure><p>The first code is to get the highest value of temperature, and the second code is to get when the highest temperature happened.</p><p>Let’s get the answer of the last question</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*B9QSOw0wSE7zIj4Dz4kfMA.png" /></figure><p>The code is similar with code to solve second question, we just replace max function to min.</p><p>Very easy right? I will keep this article simple, so you will not think that pandas is hard. Pandas is easy guys!!! I will write many story about pandas and show you how easy pandas is!</p><p>You can download/clone this code &amp; csv file from my github <a href="https://github.com/ArRosid/learn-pandas-python">https://github.com/ArRosid/learn-pandas-python</a></p><p>See you on the next stories!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77d6b0298f31" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pagination in Django Rest Framework]]></title>
            <link>https://medium.com/@arrosid/pagination-in-django-rest-framework-bbc64abb0faa?source=rss-2bd4b3ed90eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbc64abb0faa</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[ArRosid]]></dc:creator>
            <pubDate>Mon, 28 Oct 2019 02:53:34 GMT</pubDate>
            <atom:updated>2019-10-28T02:53:34.435Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*WsMx3NqyRZx1WFrx.png" /></figure><p>In the previous stories, I already write a lot about Django Rest Framework. If you haven’t read it, you can read it first</p><ul><li><a href="https://medium.com/@arrosid/simple-rest-api-using-django-rest-framework-dddbaa895c52?source=your_stories_page---------------------------">Simple Rest API using Django Rest Framework</a></li><li><a href="https://medium.com/@arrosid/crud-in-django-rest-framework-part-1-8dc3d9f6ce6d?source=your_stories_page---------------------------">CRUD in Django Rest Framework — Part 1</a></li><li><a href="https://medium.com/@arrosid/crud-in-django-rest-framework-part-2-b35f9c5ae46?source=your_stories_page---------------------------">CRUD in Django Rest Framework — Part 2</a></li><li><a href="https://medium.com/@arrosid/validation-in-django-rest-framework-serializers-b1acd978367e?source=your_stories_page---------------------------">Validation in Django Rest Framework Serializers</a></li><li><a href="https://medium.com/@arrosid/introduction-to-genericapiview-in-django-rest-framework-eb557bf986b2?source=your_stories_page---------------------------">Introduction to GenericAPIView in Django Rest Framework</a></li><li><a href="https://medium.com/@arrosid/introduction-to-viewset-django-rest-framework-c600e2b81ecf?source=your_stories_page---------------------------">Introduction to Viewset Django Rest Framework</a></li><li><a href="https://medium.com/@arrosid/how-to-create-auth-token-authentication-in-django-rest-framework-part-1-70a03caa36bf?source=your_stories_page---------------------------">How to create Token Based Authentication in Django Rest Framework — Part 1</a></li><li><a href="https://medium.com/@arrosid/how-to-create-token-based-authentication-in-django-rest-framework-part-2-b40c18468fab?source=your_stories_page---------------------------">How to create Token Based Authentication in Django Rest Framework — Part 2</a></li><li><a href="https://medium.com/@arrosid/permissions-in-django-rest-framework-part-1-88cd1831297e?source=your_stories_page---------------------------">Permissions in Django Rest Framework — Part 1</a></li><li><a href="https://medium.com/@arrosid/permissions-in-django-rest-framework-part-2-30c46fd4c3ff?source=your_stories_page---------------------------">Permissions in Django Rest Framework Part — 2</a></li><li><a href="https://medium.com/@arrosid/permissions-in-django-rest-framework-part-3-fb3edcaee269?source=your_stories_page---------------------------">Permissions in Django Rest Framework Part — 3</a></li></ul><p>Now in this story, I will talk about Pagination. To follow this story, at least you need to read CRUD in Django Rest Framework Part 1 &amp; Part 2 first. In that two stories, we create an API like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lNUDiBjllw1b5YmQJCpTVw.png" /></figure><p>If you see picture above, we have 6 result in one request. This is no problem, but how if we has 1000 article and the article loaded in one request? This is will be problem for us.</p><p>That is why we need pagination here. So we can limit the result of request, let’s say to 10.</p><p>Okay now let’s do pagination on our project. You can clone the project from my github <a href="https://github.com/ArRosid/learn-def">https://github.com/ArRosid/learn-def</a></p><p>To set pagination, edit settings.py file</p><pre>REST_FRAMEWORK = {<br>    &#39;DEFAULT_AUTHENTICATION_CLASSES&#39;: [<br>        &#39;rest_framework.authentication.TokenAuthentication&#39;, <br>    ],<br><strong>    &#39;DEFAULT_PAGINATION_CLASS&#39;: &#39;rest_framework.pagination.PageNumberPagination&#39;,<br>    &#39;PAGE_SIZE&#39;: 2</strong><br>}</pre><p>Above we set page size to 2, so in one request, we just get 2 article. Let’s test it</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eK_lkBiadYbomRglJ7WpdQ.png" /></figure><p>Cool now we get 2 result, and we also have link to next page &amp; previous page. but this is the first page, so we don’t have previous page. Let’s move to next page</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*47SK9FdKo-X1seDmNaBbPQ.png" /></figure><p>Great!! Now we has next &amp; previous page. Let’s move to the next page</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Hmnh0bdMkf7m6vN1FfVEbw.png" /></figure><p>For now we just have 6 articles, so this is the last page, that’s why we don’t have next page here.</p><p>Very easy right to doing pagination in Django Rest Framework?</p><p>This is the end of this article. In the next article I will show you another interesting topics! Stay Tune!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbc64abb0faa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Permissions in Django Rest Framework Part — 3]]></title>
            <link>https://medium.com/@arrosid/permissions-in-django-rest-framework-part-3-fb3edcaee269?source=rss-2bd4b3ed90eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb3edcaee269</guid>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[ArRosid]]></dc:creator>
            <pubDate>Thu, 24 Oct 2019 23:30:46 GMT</pubDate>
            <atom:updated>2019-10-24T23:30:46.079Z</atom:updated>
            <content:encoded><![CDATA[<h3>Permissions in Django Rest Framework Part — 3</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*DXSMo2KuxJSGngXp.png" /></figure><p>In the previous story we already talk about permission in Django Rest Framework, but we use permission that defined by django. Now in this story, we will learn how to create custom permission in django rest framework.</p><p>If you haven’t read the previous story about permissions in django rest framework, you can read it first</p><ul><li><a href="https://medium.com/@arrosid/permissions-in-django-rest-framework-part-1-88cd1831297e?source=your_stories_page---------------------------">Permissions in Django Rest Framework — Part 1</a></li><li><a href="https://medium.com/@arrosid/permissions-in-django-rest-framework-part-2-30c46fd4c3ff?source=your_stories_page---------------------------">Permissions in Django Rest Framework Part — 2</a></li></ul><p>We will use project from previous story, you also can clone the project from my github <a href="https://github.com/ArRosid/learn-def">https://github.com/ArRosid/learn-def</a></p><p>Okay let’s start. First, we will edit the model of this project. Edit models.py file inside app directory</p><pre>from django.db import models<br><strong>from django.contrib.auth.models import User</strong></pre><pre>class Article(models.Model):<br><strong>    author = models.ForeignKey(User, on_delete=models.CASCADE)<br></strong>    title = models.CharField(max_length=120)<br>    description = models.CharField(max_length=200)<br>    body = models.TextField()</pre><pre>    def __str__(self):<br>        return f&quot;{ self.author } - { self.title }&quot;</pre><p>Let’s makemigrations &amp; migrate our project</p><pre>python manage.py makemigrations<br>python manage.py migrate</pre><p><em>Tips: If you got an error during migrate, delete db.sqlite3 first and do migrate again. Your data in the database will lose, but I think this is no problem because our data just for test.</em></p><p>If you delete db.sqlite3, you also need to recreate the user.</p><pre>python manage.py createsuperuser</pre><p>Let’s create some user for test. In this case, I create two user, <strong>admin </strong>and <strong>admin1</strong></p><p>Let’s add some article with different user</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Um6RDosiagYqSLoJUOj23Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lhTsiQHWD947sndfc178OA.png" /></figure><p>Ok now we have some articles with different author.</p><p>We have some problem here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jBg4ZUG58oKUdoAouPC7aA.png" /></figure><p>We login with user admin, the id for user admin is 1, but we can update or even delete all article, include the article created by admin1.</p><p>Now we want to create custom permissions, so the user can only update &amp; delete article that created by him/her self.</p><p>Create and edit permissions.py file inside app directory</p><pre>from rest_framework import permissions</pre><pre>class IsOwnArticleOrReadOnly(permissions.BasePermission):<br>    <br>    def has_object_permission(self, request, view, obj):<br>        if request.method in permissions.SAFE_METHODS:<br>            return True<br>        if obj.author == request.user:<br>            return True</pre><p>The permissions above will return True if the method is SAFE_METHODS (GET, HEAD, OPTIONS) and if the user logged in is the autor of the article. So we can’t edit or delete article created by other user, but we can read it. Now let use this permissions. Edit views.py file inside app directory</p><pre><strong>from .permissions import IsOwnArticleOrReadOnly</strong></pre><pre>class ArticleViewSet(viewsets.ModelViewSet):<br>    serializer_class = serializers.ArticleSerializer<br>    queryset = models.Article.objects.all()<br><strong>    permission_classes = [IsAuthenticated, IsOwnArticleOrReadOnly]</strong></pre><p>Let’s test it</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kfHHbj_x5UFyRqd3rSm5Nw.png" /></figure><p>Above, we login using admin user but access the article created by admin1, so we just have read only permissions. we can’t edit or delete this article. Let’s try to login using admin1</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*auZG4uyCOXXg4pijp1khvw.png" /></figure><p>Now we have permissions to edit or delete the article. Very cool right?</p><p>This is the end of this story, In the next story I will show you another amazing topic! Stay tune!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb3edcaee269" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Permissions in Django Rest Framework Part — 2]]></title>
            <link>https://medium.com/@arrosid/permissions-in-django-rest-framework-part-2-30c46fd4c3ff?source=rss-2bd4b3ed90eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/30c46fd4c3ff</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <dc:creator><![CDATA[ArRosid]]></dc:creator>
            <pubDate>Tue, 22 Oct 2019 22:03:16 GMT</pubDate>
            <atom:updated>2019-10-22T22:03:16.849Z</atom:updated>
            <content:encoded><![CDATA[<h3>Permissions in Django Rest Framework Part — 2</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*coTWI42a1DjTBV1y.png" /></figure><p>In the previous story, we already talk about Permission in Django Rest Framework. If you haven’t read it, you can read it first</p><ul><li><a href="https://medium.com/@arrosid/permissions-in-django-rest-framework-part-1-88cd1831297e">Permissions in Django Rest Framework — Part 1</a></li></ul><p>I told you that by default, Django Rest Framework has some permissions. I explain about <em>IsAuthenticatedOrReadOnly </em>in the previous story. Now in this story, we will talk about <em>IsAdminUser </em>permissions</p><p>We will use project from previous story, you can clone it from my github <a href="https://github.com/ArRosid/learn-def">https://github.com/ArRosid/learn-def</a></p><p>Okay let’s start. Edit views.py file inside app directory</p><pre><strong>from rest_framework.permissions import (IsAuthenticated,<br>                                        IsAdminUser,<br>                                        IsAuthenticatedOrReadOnly)</strong></pre><pre>class ArticleViewSet(viewsets.ModelViewSet):<br>    serializer_class = serializers.ArticleSerializer<br>    queryset = models.Article.objects.all()<br><strong>    permission_classes = [IsAdminUser,]</strong></pre><p>We already has admin user in our project. If you don’t have, you can create it first</p><pre>python manage.py createsuperuser</pre><p>After create admin user, let’s create another user without admin level. Go to localhost:8000/admin</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*IGhDe8rtK0V_BdsOHp8AkA.png" /></figure><p>Login using admin user that we have created, and we will get something like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_bbq0DgcnL1AN-RGE9ELgg.png" /></figure><p>Go to Users,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iOZNNEnuJIs54HRxpOh_OA.png" /></figure><p>Above we have admin user, and also we can add new user here. Let’s add new user</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dsR3e3IPjLQZe2yA13J97w.png" /></figure><p>After create the user, we will see something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8E7J_mhLPJiPhSRDwfyO5w.png" /></figure><p>Make sure to not chec <em>staff status &amp; superuser status</em>.</p><p>Okay now let’s test it. First we will login with the new user which don’t have superuser status.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EMZWs-tTNI01lS6_x0NDtQ.png" /></figure><p>Okay we get the token now, let’s use it. But I want to try access the API without providing any credentials first, and this is what we get</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2JiI68LXH2NSUsvCmWuh8w.png" /></figure><p>Okay we got an error <em>Authentication credentials were not provided. </em>Let’s use token from new user</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WJ_Z0PAV67WRS04Y0uUn0A.png" /></figure><p>Okay now we got an error <em>You do not have permission to perform this action. </em>Let’s try login using admin user</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*oDvnP33nMbqEurWgUOPp8w.png" /></figure><p>Let’s use token above</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*osDWAiupdGDJKfPT8zY_sg.png" /></figure><p>Now we able to access API only if we login using admin user. Very cool right?</p><p>Let’s try to make the new user that we have created to have superuser status.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*srGibFe1cS3N2-6wUraSww.png" /></figure><p>Let’s access the API again using new user token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CVsfhwuJAf4tcBQ5KTr_eg.png" /></figure><p>Okay now we also able access the API using new user, because we already add staff status &amp; superuser status to that user.</p><p>This is the end of this story, In the next story I will show you more interesting thing about permissions in django rest framework, we will try to create custom permissions!! Stay tune!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30c46fd4c3ff" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Permissions in Django Rest Framework — Part 1]]></title>
            <link>https://medium.com/@arrosid/permissions-in-django-rest-framework-part-1-88cd1831297e?source=rss-2bd4b3ed90eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/88cd1831297e</guid>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[ArRosid]]></dc:creator>
            <pubDate>Mon, 21 Oct 2019 23:27:40 GMT</pubDate>
            <atom:updated>2019-10-21T23:27:40.943Z</atom:updated>
            <content:encoded><![CDATA[<h3>Permissions in Django Rest Framework — Part 1</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*60La0Vzp_5dgXcTo.png" /></figure><p>In the previous story, I show you how to create Token based authentication. If you haven’t read it, you can read it first</p><ul><li><a href="https://medium.com/@arrosid/how-to-create-auth-token-authentication-in-django-rest-framework-part-1-70a03caa36bf">How to create Token Based Authentication in Django Rest Framework — Part 1</a></li><li><a href="https://medium.com/@arrosid/how-to-create-token-based-authentication-in-django-rest-framework-part-2-b40c18468fab">How to create Token Based Authentication in Django Rest Framework — Part 2</a></li></ul><p>In this story, We will talk about Permissions in Django Rest Framework. Actually in the two previous story, we already use permission, we use <em>IsAuthenticated </em>permissions.</p><pre>class ArticleViewSet(viewsets.ModelViewSet):<br>    serializer_class = serializers.ArticleSerializer<br>    queryset = models.Article.objects.all()<br>    permission_classes = [<strong>IsAuthenticated</strong>,]</pre><p>In this story, we will use project from previous story, So I highly recommend you to read it first and also clone the project from my github <a href="https://github.com/ArRosid/learn-def">https://github.com/ArRosid/learn-def</a></p><p>By default, Django Rest Framework has some permission,</p><ul><li>IsAuthenticated</li><li>IsAdminUser</li><li>IsAuthenticatedOrReadOnly</li></ul><p>Let’s try that one by one. We already know about <em>IsAuthenticated, </em>this is mean that if we not login, we can’t access the API.</p><p>Now Lets try <em>IsAuthenticatedOrReadOnly</em></p><pre><strong>from rest_framework.permissions import (IsAuthenticated,<br>                                        IsAdminUser,<br>                                        IsAuthenticatedOrReadOnly)</strong></pre><pre>class ArticleViewSet(viewsets.ModelViewSet):<br>    serializer_class = serializers.ArticleSerializer<br>    queryset = models.Article.objects.all()<br><strong>    permission_classes = [IsAuthenticatedOrReadOnly,]</strong></pre><p>Let’s test it. We will not login first</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ow1WbU84FdKYXfCLbnBAeg.png" /></figure><p>We success access the API. but if we try to scroll button, we will not see from to create POST request</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ag646M00RkKuSF5mXHTsGg.png" /></figure><p>Let’s try to access the detail of one article</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v654bvE6pdadJxMF257RtA.png" /></figure><p>We also don’t see from to create PUT request and also we don’t see delete button to create DELETE request.</p><p>Now let’s login using Token Authentication.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2VSivRKgmr_iduA0zfKNwA.png" /></figure><p>Now we see from to create POST request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q12CmEBm0kBcghCjRoyDyA.png" /></figure><p>We also has from to create PUT request, also delete button to create DELETE request.</p><p>Very cool right? And this is so easy!</p><p>I will keep this story simple and short, so you can understand easily. In the next story I will show you about IsAdminUser and how to create custom permission. See you in the next story! Stay tune!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88cd1831297e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create Token Based Authentication in Django Rest Framework — Part 2]]></title>
            <link>https://medium.com/@arrosid/how-to-create-token-based-authentication-in-django-rest-framework-part-2-b40c18468fab?source=rss-2bd4b3ed90eb------2</link>
            <guid isPermaLink="false">https://medium.com/p/b40c18468fab</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[python3]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[ArRosid]]></dc:creator>
            <pubDate>Mon, 21 Oct 2019 06:40:26 GMT</pubDate>
            <atom:updated>2019-10-21T06:40:26.312Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to create Token Based Authentication in Django Rest Framework — Part 2</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*y4SNRXYyoa3WwGKJ.png" /></figure><p>In the previous story, I already start talk about token based authentication in Django Rest Framework. If you haven’t read that, you can read it first</p><ul><li><a href="https://medium.com/@arrosid/how-to-create-auth-token-authentication-in-django-rest-framework-part-1-70a03caa36bf">How to create Token Based Authentication in Django Rest Framework — Part 1</a></li></ul><p>In this story, I will show you how to create API for login. Let’s just start. Edit views.py file inside app directory</p><pre>from rest_framework import viewsets<br>from rest_framework.decorators import api_view<br><strong>from rest_framework.response import Response<br>from rest_framework.permissions import IsAuthenticated<br>from rest_framework import status<br>from django.contrib.auth import authenticate<br>from django.views.decorators.csrf import csrf_exempt<br>from rest_framework.authtoken.models import Token</strong></pre><pre>from . import models<br>from . import serializers</pre><pre><a href="http://twitter.com/csrf_exempt"><strong>@csrf_exempt</strong></a><strong><br></strong><a href="http://twitter.com/api_view"><strong>@api_view</strong></a><strong>([&#39;POST&#39;])<br>def login(request):<br>    username = request.data.get(&quot;username&quot;)<br>    password = request.data.get(&quot;password&quot;)<br>    if username is None and password is None:<br>        return Response({&#39;error&#39;:&#39;Please provide user &amp; password&#39;},<br>                        status=status.HTTP_400_BAD_REQUEST)<br>    <br>    user = authenticate(username=username, password=password)</strong></pre><pre><strong>    if not user:<br>        return Response({&#39;error&#39;: &#39;Invalid credentials&#39;},<br>                        status=status.HTTP_404_NOT_FOUND)<br>    <br>    token, _ = Token.objects.get_or_create(user=user)<br>    return Response({&#39;token&#39;:token.key},<br>                    status=status.HTTP_200_OK)</strong></pre><pre>class ArticleViewSet(viewsets.ModelViewSet):<br>    serializer_class = serializers.ArticleSerializer<br>    queryset = models.Article.objects.all()<br>    permission_classes = [IsAuthenticated,]</pre><p>Next, lets create url for this views</p><pre>from django.urls import path, include<br>from rest_framework.routers import DefaultRouter</pre><pre>from . import views</pre><pre>router = DefaultRouter()<br>router.register(r&quot;article&quot;, views.ArticleViewSet)</pre><pre>urlpatterns = [<br><strong>    path(&quot;login/&quot;, views.login),<br></strong>    path(&quot;&quot;, include(router.urls)),<br>]</pre><p>Let’s test it. We can use postman</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1a5QFXCB9E6kS3028Xc8NQ.png" /></figure><p>This is what we get if we successful login</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nsrZUNllNaLic-bX9gjqxQ.png" /></figure><p>Now we are able to use this token to access the protected API as defined in the previous story</p><p>This is what we get if we access the protected API without token</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IK4rMZVa4vlOTch-Q8MPnQ.png" /></figure><p>Let’s use the token</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/660/1*ko_pklqcQ82FMY9n3H0X7g.png" /></figure><p>This is what we will get now</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P9e3nJ7ewim7JpwGOLtCAg.png" /></figure><p>Cool!!! We are success now!</p><p>This is the end of this story. Stay tune! I will cover another interesting topic.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b40c18468fab" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>