<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Django REST Framework</title>
	<atom:link href="https://renanmf.com/category/django-rest-framework/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Software development, machine learning</description>
	<lastBuildDate>Thu, 10 Jun 2021 14:00:00 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://renanmf.com/wp-content/uploads/2020/03/cropped-android-chrome-512x512-2-32x32.png</url>
	<title>Django REST Framework</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>CORS on Django REST Framework</title>
		<link>https://renanmf.com/cors-django-rest-framework/</link>
		
		<dc:creator><![CDATA[Renan Moura]]></dc:creator>
		<pubDate>Thu, 20 Feb 2020 21:53:58 +0000</pubDate>
				<category><![CDATA[Django REST Framework]]></category>
		<category><![CDATA[django rest framework]]></category>
		<guid isPermaLink="false">https://renanmf.com/?p=696</guid>

					<description><![CDATA[<p>In the post Django REST Framework: API in a few minutes in Python, I showed you how to build your very first API in Python. But there is one minor problem, if you test your API with another application, like an Angular or React frontend, you are going to face CORS problems. To solve this [&#8230;]</p>
<p>The content <a href="https://renanmf.com/cors-django-rest-framework/">CORS on Django REST Framework</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In the post <a href="https://renanmf.com/django-rest-framework-api-few-minutes/">Django REST Framework: API in a few minutes in Python</a>, I showed you how to build your very first API in Python.</p>
<p>But there is one minor problem, if you test your API with another application, like an Angular or React frontend, you are going to face CORS problems.</p>
<p>To solve this you can install the <a href="https://github.com/adamchainz/django-cors-headers">django-cors-headers</a> in your app using <code>pip</code>.</p>
<pre><code>pip install django-cors-headers</code></pre>
<p>After installing the <code>django-cors-headers</code> in your project, you have to configure it in the <code>settings.py</code> file, including the <code>corsheaders</code> in the <code>INSTALLED_APPS</code>.</p>
<pre><code class="language-python">INSTALLED_APPS = [
    ...
    &#039;corsheaders&#039;,
    ...
]</code></pre>
<p>The end result will look like this.</p>
<pre><code class="language-python">INSTALLED_APPS = [
    &#039;django.contrib.admin&#039;,
    &#039;django.contrib.auth&#039;,
    &#039;django.contrib.contenttypes&#039;,
    &#039;django.contrib.sessions&#039;,
    &#039;django.contrib.messages&#039;,
    &#039;django.contrib.staticfiles&#039;,
    &#039;rest_framework&#039;,
    &#039;corsheaders&#039;,
]</code></pre>
<p>After that, still in <code>settings.py</code>, add the <code>corsheaders.middleware.CorsMiddleware</code> in <code>MIDDLEWARE</code> before any middleware that can generate responses like CommonMiddleware.</p>
<pre><code class="language-python">MIDDLEWARE = [
    ...
    &#039;corsheaders.middleware.CorsMiddleware&#039;,
    &#039;django.middleware.common.CommonMiddleware&#039;,
    ...
]</code></pre>
<p>The end result will look like this.</p>
<pre><code class="language-python">MIDDLEWARE = [
    &#039;django.middleware.security.SecurityMiddleware&#039;,
    &#039;django.contrib.sessions.middleware.SessionMiddleware&#039;,
    &#039;django.middleware.common.CommonMiddleware&#039;,
    &#039;django.middleware.csrf.CsrfViewMiddleware&#039;,
    &#039;django.contrib.auth.middleware.AuthenticationMiddleware&#039;,
    &#039;django.contrib.messages.middleware.MessageMiddleware&#039;,
    &#039;django.middleware.clickjacking.XFrameOptionsMiddleware&#039;,
    &#039;corsheaders.middleware.CorsMiddleware&#039;,
    &#039;django.middleware.common.CommonMiddleware&#039;,
]</code></pre>
<p>Finally, you have to add a new block in the <code>settings.py</code> file that contains the list of allowed origins, that is, those who can consume the API.</p>
<p>In the example below I am including <code>http://localhost:4200</code> which is Angular&#8217;s default localhost.</p>
<pre><code class="language-python">CORS_ORIGIN_WHITELIST = [
    &quot;http://localhost:4200&quot;,
]</code></pre>
<p>The content <a href="https://renanmf.com/cors-django-rest-framework/">CORS on Django REST Framework</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Django REST Framework: API in a few minutes in Python</title>
		<link>https://renanmf.com/django-rest-framework-api-few-minutes/</link>
					<comments>https://renanmf.com/django-rest-framework-api-few-minutes/#comments</comments>
		
		<dc:creator><![CDATA[Renan Moura]]></dc:creator>
		<pubDate>Sun, 16 Feb 2020 20:41:47 +0000</pubDate>
				<category><![CDATA[Django REST Framework]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[django rest framework]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">https://renanmf.com/?p=648</guid>

					<description><![CDATA[<p>In this tutorial, I will show you a quickstart to have your first API up and running in minutes. The Django REST Framework is a powerful toolkit for building APIs on top of the Django Framework. I&#8217;m going to use Django Framework 3 and assume you have the latest version of Python 3 installed. I [&#8230;]</p>
<p>The content <a href="https://renanmf.com/django-rest-framework-api-few-minutes/">Django REST Framework: API in a few minutes in Python</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this tutorial, I will show you a quickstart to have your first API up and running in minutes.</p>
<p>The <a href="https://www.django-rest-framework.org/">Django REST Framework</a> is a powerful toolkit for building APIs on top of the <a href="https://www.djangoproject.com/">Django Framework</a>.</p>
<p>I&#8217;m going to use Django Framework 3 and assume you have the latest version of Python 3 installed. I did this tutorial with Python 3.8.1.</p>
<h2>Environment</h2>
<p>On a terminal, create a folder with your project name enter in it.</p>
<pre><code>mkdir myproject
cd myproject</code></pre>
<p>Create a virtual environment to work on.</p>
<pre><code>python3 -m venv venv</code></pre>
<p>Activate the virtual environment.</p>
<pre><code>. venv/bin/activate</code></pre>
<p>Check for the <code>(venv)</code> at the beginning of the command line, it indicates the virtual environment started successfully.</p>
<h2>Install Django Framework</h2>
<p>Use pip to install the Django Framework.</p>
<pre><code>pip install django</code></pre>
<p>Your output should be similar to this. <img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/1-e1581806194775.png" alt="Install" /></p>
<p>Start your first project with the django-admin cli.</p>
<p>Mind the <code>.</code> to create the structure in the current directory.</p>
<pre><code>django-admin startproject myfirstapi .</code></pre>
<p>Then use the <code>manage.py</code> to create the database with the <code>migrate</code> parameter.</p>
<pre><code>python manage.py migrate</code></pre>
<p>Your output should be similar to this. <img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/2.png" alt="Initial" /></p>
<p>Create a super user to use the admin panel.</p>
<pre><code>python manage.py createsuperuser</code></pre>
<p>User <code>admin</code> and password <code>123</code>, it\&#8217;s going to complain about the password being too simple, just agree. <img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/3.png" alt="Creatin" /></p>
<p>Finally, run the server.</p>
<pre><code>python manage.py runserver</code></pre>
<p>If everything was alright, your output should be similar to this.</p>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/4.png" alt="RUn" /></p>
<p>Check <code>http://localhost:8000</code> in a browser.</p>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/5.png" alt="Project" /></p>
<p>Enter <code>http://localhost:8000/admin</code> in a browser.</p>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/6.png" alt="Django" /></p>
<p>Login with <code>admin</code> and <code>123</code> those were the credentials chosen earlier.</p>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/7.png" alt="Django" /></p>
<p>Great! Now you have Django up and running.</p>
<h2>Install Django Rest Framework</h2>
<p>CTRL-C on the terminal to shutdown the server.</p>
<p>Use pip to install the Django Rest Framework</p>
<pre><code>pip install djangorestframework</code></pre>
<p>The output of the installation should be similar to this.</p>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/8.png" alt="Installing" />## Create the app</p>
<p>Create your first app named person.</p>
<pre><code>manage.py startapp person</code></pre>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/9.png" alt="Create" /></p>
<p>Open <code>myproject</code> folder in some editor, I use VSCode. </p>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/10.png" alt="Open" /></p>
<h3>settings.py</h3>
<p>In <code>settings.py</code>, at the end of <code>INSTALLED_APPS</code>, add:</p>
<pre><code class="language-python">    &#039;person&#039;,
    &#039;rest_framework&#039;,</code></pre>
<p>Like so: </p>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/11.png" alt="Create" /></p>
<h3>models.py</h3>
<p>Inside the person folder, open<code>models.py</code> and create a <code>Person</code> class:</p>
<pre><code class="language-python">from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=100)
    surname = models.CharField(max_length=100)
    address = models.CharField(max_length=200)

    def __str__(self):
        return self.name</code></pre>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/12.png" alt="models.py" /></p>
<h3>serializers.py</h3>
<p>In the same level of <code>models.py</code> create a <code>serializers.py</code> with the following content:</p>
<pre><code class="language-python">from rest_framework import serializers
from .models import Person

class PersonSerializer(serializers.ModelSerializer):
    class Meta:
        model = Person
        fields = [&#039;id&#039;, &#039;name&#039;, &#039;surname&#039;, &#039;address&#039;]</code></pre>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/13.png" alt="serializers.py" /></p>
<h3>views.py</h3>
<p>In <code>views.py</code>, write the following code:</p>
<pre><code class="language-python">from rest_framework import viewsets
from .models import Person
from .serializers import PersonSerializer

class PersonViewSet(viewsets.ModelViewSet):
    queryset = Person.objects.all()
    serializer_class = PersonSerializer</code></pre>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/14.png" alt="views.py" /></p>
<h3>admin.py</h3>
<p>In <code>admin.py</code>, register our class to appear in the admin panel.</p>
<pre><code class="language-python">from django.contrib import admin
from .models import Person

admin.site.register(Person)</code></pre>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/15.png" alt="admin.py" /></p>
<h2>urls.py</h2>
<p>In <code>urls.py</code>, create the routing for our API in the <code>/persons</code>:</p>
<pre><code class="language-python">from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from person.views import PersonViewSet

router = routers.DefaultRouter()
router.register(r&#039;persons&#039;, PersonViewSet)

urlpatterns = [
    path(&#039;&#039;, include(router.urls)),
    path(&#039;api-auth/&#039;, include(&#039;rest_framework.urls&#039;, namespace=&#039;rest_framework&#039;)),
    path(&#039;admin/&#039;, admin.site.urls),
]</code></pre>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/16.png" alt="urls.py" /></p>
<h2>Update the database</h2>
<p>Finally, <code>makemigrations</code> to create the scripts according to the current structure:</p>
<pre><code>python manage.py makemigrations</code></pre>
<p>And update the database with the scripts created earlier.</p>
<pre><code>python manage.py migrate</code></pre>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/17.png" alt="Migrations" /></p>
<h2>Checking the API</h2>
<p>Start the server.</p>
<pre><code>python manage.py runserver</code></pre>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/18.png" alt="Run" /></p>
<p>Check <code>http://localhost:8000/admin</code> and log in with <code>admin</code> and <code>123</code> and add a new person.</p>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/19.png" alt="Django" /></p>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/20.png" alt="Showing" /></p>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/21.png" alt="Inputing" /></p>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/22.png" alt="New" /></p>
<p>In a browser, access <code>http://localhost:8000/persons/</code>, you should see the record corresponding to the one you created in the admin panel. </p>
<p><img decoding="async" src="https://renanmf.com/wp-content/uploads/2020/02/23.png" alt="API" /></p>
<p>That&#8217;s it! A simple endpoint almost effortlessly.</p>
<p>Notice that you have all the basic operations in a CRUD: Create, Read, Update and Delete.</p>
<p>The content <a href="https://renanmf.com/django-rest-framework-api-few-minutes/">Django REST Framework: API in a few minutes in Python</a> is from <a href="https://renanmf.com">Renan Moura - Software Engineering</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://renanmf.com/django-rest-framework-api-few-minutes/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
