Te encuentras en la páginas de Blogsperu, los resultados son los ultimos contenidos del blog. Este es un archivo temporal y puede no representar el contenido actual del mismo.

Comparte esta página:

Etiquetas: [API]  [Django]  [Framework]  [REST]  [tutorial]  
Fecha Publicación: Sat, 23 Jan 2021 21:45:00 +0000

 Tutorial on how to create a REST API using Django Rest Framework (DRF)

Create a REST API with Django Rest Framework
Create a REST API with Django Rest Framework

DRF is a library that allows us to build a REST API on Django in a simple way. Offering a high range of methods and functions for the management, definition and control of our resources (endpoints).

Let's start our installation

Creating our base directory
We must create the directory where our code will live

mkdir tutorial
cd tutorial

Setting the environment

Before we begin, we must first create our virtual environment. This will allow us to completely isolate the dependencies of the project, in such a way, there will be no conflict with the dependencies and local libraries existing in our system. To learn more about the installation of virtual environments.

virtualenv tutorial
source tutorial/bin/activate # On Windows use `env\Scripts\activate`
Once our virtual environment has been created and activated, we proceed to install the necessary packages.

pip install django
pip install djangorestframework

Creating our project and initial configuration

Once DRF is installed in our virtual environment, we proceed to create our project in Django and establish the initial configuration. For the purposes of this tutorial, we are going to create an API to obtain information about movies and series, it will be called webflix.

django-admin.py startproject webflix
cd webflix
django-admin.py startapp series

Once our first application has been created, we proceed to incorporate it into the installed Django modules as we will do with DRF. To do this, we edit our /tutorial/settings.py file and add the following


Now we start to play with the serializers. 

Serializers and Model Serializers

Before creating our first serializer, we need to create a model which we are going to use to work with in this tutorial. We will create one called Series, within our series app, which will represent all the information that a TV series has.

from django.db import models

xclass Serie(models.Model):

HORROR = 'horror'
COMEDY = 'comedy'
ACTION = 'action'
DRAMA = 'drama'

(HORROR, 'Horror'),
(COMEDY, 'Comedy'),
(ACTION, 'Action'),
(DRAMA, 'Drama'),

name = models.CharField(max_length=100)
release_date = models.DateField()
rating = models.IntegerField(default=0)
category = models.CharField(max_length=10, choices=CATEGORIES_CHOICES)

Then we need to create the Django migrations

./manage.py makemigrations series
./manage.py migrate

A vital part of the Django Rest Framework is the ability to be able to serialize and deserialize our instances or resources in some type of representation that is easier to handle and transmit. To learn a little more about this process, we are going to create a serializer that will help us transform and handle our string instances in JSON format. To do this, we are going to create a file inside our app series called serializers.py. Within it the declarations of our serializers related to our newly created app will recur.

from rest_framework import serializers
from .models import Serie

xclass SerieSerializer(serializers.Serializer):
pk = serializers.IntegerField(read_only=True)
name = serializers.CharField()
release_date = serializers.DateField()
rating = serializers.IntegerField()
category = serializers.ChoiceField(choices=Serie.CATEGORIES_CHOICES)

def create(self, validated_data):
Create and return a new `Serie` instance, given the validated data.
return Serie.objects.create(**validated_data)

def update(self, instance, validated_data):
Update and return an existing `Serie` instance, given the validated data.
instance.name = validated_data.get('name', instance.name)
instance.release_date = validated_data.get('release_date', instance.release_date)
instance.rating = validated_data.get('rating', instance.rating)
instance.category = validated_data.get('category', instance.category)
return instance

Basically in the first part of the serializer we define the attributes that we want to represent, each attribute is accompanied by a type, much like the forms in Django. Serializers in DRF need two fundamental methods, create () and update (), both of which work to create and update the instance using the serializer. Now it's time to see how to initialize our serializer. 

Working with Serializers

To start testing our serializer quickly, let's open our Django console

./manage.py shell

Now, we import our model, serializer, and other utilities to create multiple instances of the Series model

from series.models import Series
from series.serializers import SerieSerializer
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from datetime import datetime

release_date = datetime.strptime ('17 -04-2011 ','% d-% m-% Y '). date ()
series = Series (name = 'Game of Thrones', category = 'drama', release_date = release_date)
series.save ()

release_date = datetime.strptime ('24 -06-2015 ','% d-% m-% Y '). date ()
series = Series (name = 'Mr. Robot', category = 'drama', release_date = release_date)
series.save ()

Once we have created two instances, we can start playing with our serializers. We are going to serialize one of them.

serializer = SerieSerializer(serie)
>{'category': 'drama',
'name': u'Mr. Robot',
'pk': 2,
'rating': 0,
'release_date': '2015-06-24'}

In this step, we have transformed our instance into a native Python data type, in this case a dictionary (dict for future reference). To finish the serialization process we must transform our dict into JSON, for this, we will use the JSONRenderer xclass that DRF brings and its render () method.

content = JSONRenderer().render(serializer.data)
> '{"pk":2,"name":"Mr. Robot","release_date":"2015-06-24","rating":0,"category":"drama"}'

If we want to reverse the process, we must transform our JSON into a native Python data type

from django.utils.six import BytesIO

stream = BytesIO(content)
data = JSONParser().parse(stream)
> {u'category': u'drama',
u'name': u'Mr. Robot',
u'pk': 2,
u'rating': 0,
u'release_date': u'2015-06-24'}

Next, we convert our native data type, in this case a dict, to an instance of the Series model with all its attributes set.

serializer = SerieSerializer(data=data)
> True

> OrderedDict([(u'name', u'Mr. Robot'), (u'release_date', datetime.date(2015, 6, 24)), (u'rating', 0), (u'category', 'drama')])

serie = serializer.save()
> <Serie: Serie object>

# Borramos la serie creada para continuar con nuestro ejemplo

We just instantiated our model from a dict. We can notice a similarity between working with serializers and Django forms. The process to create an instance is similar: We initialize our serializer with the required data We validate that the data is correct If there is no error, we can inspect that data to manipulate it We execute the .save () method to create the instance. Not only can we serialize an instance, our serializer allows us to transform several instances or a queryset. To do this, we only need to pass the queryset and the flag many = True which indicates that the object to be serialized is a set of instances.

serializer = SerieSerializer(Serie.objects.all(), many=True)
OrderedDict([('pk', 1), ('name', u'Game of Thrones'), ('release_date', '2011-04-17'), ('rating', 0), ('category', u'Drama')]), OrderedDict([('pk', 2), ('name', u'Mr. Robot'), ('release_date', '2015-06-24'), ('rating', 0), ('category', u'Drama')]),
OrderedDict([('pk', 3), ('name', u'Mr. Robot'), ('release_date', '2015-06-24'), ('rating', 0), ('category', 'drama')]), OrderedDict([('pk', 4), ('name', u'Mr. Robot'), ('release_date', '2015-06-24'), ('rating', 0), ('category', 'drama')])

Using Model Serializers
As we can see, our SerieSerializer xclass is replicating much of the information that is contained in the Serie model. DRF offers the possibility of creating a serializer based on a previously defined model to avoid duplication of information. Just like forms do in Django, defining a ModelForms based on an existing model. To do this, we must import the ModelSerializer xclass and redefine our serializer. We open the file series / serializer.py and modify it.

xclass SerieSerializer(serializers.ModelSerializer):
xclass Meta:
model = Serie
fields = ('id', 'name', 'release_date', 'rating', 'category')

Redefined our serializer, we return to the console and proceed to test it.

from series.serializers import SerieSerializer

serializer = SerieSerializer()
> SerieSerializer():
id = IntegerField(label='ID', read_only=True)
name = CharField(max_length=100)
release_date = DateField()
rating = IntegerField(required=False)
category = ChoiceField(choices=(('horror', 'Horror'), ('comedy', 'Comedy'), ('action', 'Action'), ('drama', 'Drama')))

Like our old serializer, the new one has the same attributes. Furthermore, it already contains the .create () and .update () methods implemented. They are given by default.
Writing normal views in Django using a serializer
After defining our serializer, we are going to create our first API using functional views or simple views in Django. It should be noted that I am in favor of using Class-Based Views but for the moment we will do it this way for practical purposes. But first, we will create a subxclass of HttpResponse to allow our views to return the content in JSON format. We open the series / views.py file and add our subxclass.

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from series.models import Serie
from series.serializers import SerieSerializer

xclass JSONResponse(HttpResponse):
An HttpResponse that renders its content into JSON.
def __init__(self, data, **kwargs):
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json'
super(JSONResponse, self).__init__(content, **kwargs)

Now, we are going to create our first API services. We are going to allow the user to list the series and create new records for them.

def serie_list(request):
List all code serie, or create a new serie.
if request.method == 'GET':
series = Serie.objects.all()
serializer = SerieSerializer(series, many=True)
return JSONResponse(serializer.data)

elif request.method == 'POST':
data = JSONParser().parse(request)
serializer = SerieSerializer(data=data)
if serializer.is_valid():
return JSONResponse(serializer.data, status=201)
return JSONResponse(serializer.errors, status=400)

Since we are allowing a user to create a new record of a series, using the POST method, we must tell Django that this view should not request the csrf token that is usually handled, because our service will be public and the user will not you necessarily own that token, this for our practical purposes. To do this, we use the @csrf_exempt decorator. Usually this is not what should be done, DRF offers mechanisms to better handle these cases. Now, we are going to allow the user to get, update or delete a series

def serie_detail(request, pk):
Retrieve, update or delete a serie.
serie = Serie.objects.get(pk=pk)
except Serie.DoesNotExist:
return HttpResponse(status=404)

if request.method == 'GET':
serializer = SerieSerializer(serie)
return JSONResponse(serializer.data)

elif request.method == 'PUT':
data = JSONParser().parse(request)
serializer = SerieSerializer(serie, data=data)
if serializer.is_valid():
return JSONResponse(serializer.data)
return JSONResponse(serializer.errors, status=400)

elif request.method == 'DELETE':
return HttpResponse(status=204)

Finally, we add our urls to expose the services. We create a series / urls.py file within our series app.

from django.conf.urls import url
from series import views

urlpatterns = [
url (r '^ series / $', views.serie_list),
url (r '^ series / (? P <pk> [0-9] +) / $', views.serie_detail),

We proceed to open our main urls file webflix / urls.py to import the urls established in our app series.
from django.conf.urls import url, include

urlpatterns = [
url(r'^', include('series.urls')),

It should be noted that due to the practicality of the example, there are cases, which are not being taken into account. For example, what happens if the user sends a malformed request, which can cause a 500 error. There are several ways to handle certain exceptions. They are not included in the scope of this tutorial.
Testing our API
First, we must raise our server

./manage.py runserver

Performing system checks...

System check identified no issues (0 silenced).
August 03, 2016 - 08:23:45
Django version 1.9.7, using settings 'webflix.settings'
Starting development server at
Quit the server with CONTROL-C.

Now, we open another terminal to test our API We can request the API services using curl or httpie. Httpie is a lightweight http server, very easy to use. We install Httpie

pip install httpie

Now, we are going to list all the series


HTTP/1.0 200 OK
Content-Type: application/json
Date: Wed, 03 Aug 2016 08:29:53 GMT
Server: WSGIServer/0.1 Python/2.7.10
X-Frame-Options: SAMEORIGIN

"category": "drama",
"id": 1,
"name": "Game of Thrones",
"rating": 0,
"release_date": "2011-04-17"
"category": "drama",
"id": 2,
"name": "Mr. Robot",
"rating": 0,
"release_date": "2015-06-24"

Or we can request a series by your id


HTTP/1.0 200 OK
Content-Type: application/json
Date: Wed, 03 Aug 2016 08:29:53 GMT
Server: WSGIServer/0.1 Python/2.7.10
X-Frame-Options: SAMEORIGIN

"category": "Drama",
"id": 1,
"name": "Game of Thrones",
"rating": 0,
"release_date": "2011-04-17"

To create a series, we POST to / series /

$http -j POST category=comedy name=Lost release_date=2004-09-22

HTTP/1.0 201 Created
Content-Type: application/json
Date: Wed, 03 Aug 2016 08:30:50 GMT
Server: WSGIServer/0.1 Python/2.7.10
X-Frame-Options: SAMEORIGIN

"category": "drama",
"id": 4,
"name": "Lost",
"rating": 0,
"release_date": "2004-09-22"

Obviously they will be wondering, what's wrong with him, why the LOST category is comedy, but don't worry, I know, it's drama. To correct my mistake, we are going to update the information of our series by making a PUT to the url / series / 4 / with the new category.

$http -j PUT category=drama name=Lost release_date=2004-09-22

HTTP/1.0 200 OK
Content-Type: application/json
Date: Wed, 03 Aug 2016 08:32:53 GMT
Server: WSGIServer/0.1 Python/2.7.10
X-Frame-Options: SAMEORIGIN

"category": "drama",
"id": 4,
"name": "Lost",
"rating": 0,
"release_date": "2004-09-22"

DRF also offers by default a web interface which allows to interact with the API. To do this, open a browser and visit the same url previously requested. 

Next step 

 At the moment, we know how to create serializers and that they behave very similar to the Django forms. In addition, we managed to create a functional REST API that currently only serves data in JSON format. It is important to clarify that certain factors that must be handled when wanting to implement robust services are not being taken into account. In the next tutorial we will talk a little more in depth about requests and responses. In addition, we will see certain methods to improve what has been previously achieved. For example, using Class Based Views with DRF. To close, you cannot miss the cool gif.

Fecha Publicación: Thu, 15 Oct 2020 13:58:00 +0000

code to create dynamic buttons with java using jframe

code to create dynamic buttons with java using jframe

In some projects that we face in the study of the university, at work or MBA project, we are faced with the need to find a simple and easy code in which to create Components in Java dynamically, just clicking on a button can generate several components of swing and interface so that the client can interact with the program that we develop.

The way we generate set code or simple code is not the best way, since from experience the client for whom we are developing a software wants more changes in the presentation or functionality that is why I present the best way to create panels (Jpanel) , Labels (Jlabel), Texarea (JTextArea) with a button without the need to program them from scratch.

The first thing we should do is create a java Jframe form as in the following image:

create a java Jframe

we will call this form "Aplicacion", inside this form we are going to add a button named add button (Button), we are going to add a scrollpanel (JScrollPane) and inside this JScrollPane we are going to add a panel (JPanel):

add button in java

We create the following variables:

ArrayList<JButton> botones;
private int indice;

and inside the constructor we place the following code:

botones = new ArrayList<>();

the code would look like the following image:

code to create dynamic buttons with java using jframe 2
We proceed to create the action of the button or the component. I remind you that you can place any interface jlabel, Jtexarea, Jpanel etc, within this action we place the following code:


JButton boton= new JButton("https://www.mbajava.com/ "+indice);
the code looks like the following image:

code to create dynamic buttons in java 3

You may be interested in the following entry recursively organize an array

Etiquetas: [google chrome]  [javascript]  [web]  
Fecha Publicación: Fri, 18 Sep 2020 21:39:00 +0000
Many times we want to develop an automation and an automatic program that allows browsing to download files using the google chrome browser, but in this development we need the program to send a script to enable automatic downloads of any file, a drawback with chromium is that we must click on the option "Ask location before downloading" to be able to download .zip, .csv, .jpg, .png, .pdf etc files and that the save as modal window does not appear.
automatic downloads google chrome javascript
automatic downloads google chrome javascript

That is why mbajava brings you a javascript code to enable the option to automatically download all the files you need, with this code you can place the files in the download area copy the code:


I code you can add it in chrome console as in the image:

javascript code automatic downloads chrome

the html of this browser uses settings-ui tags not very mentioned in the programming language but with the power of javascript nothing is left behind.

now if we want to know if the automatic download is true we can do a check and verify if it is available with the following pure javascript code:


Etiquetas: [procedure]  [SQL Server]  
Fecha Publicación: Sun, 24 May 2020 15:53:00 +0000
Today in programming we are going to create a procedure, in which we can insert records in tables without need to wear conditional queries only with a code line we can insert the rows we want in the database.
procedure to insert dynamic table SQL Server
In the procedure, we are going to send the table name by parameters, and therefore the following columns. of the tables in this example we will take the name database [mabajava]

"@PI_NombreTabla" will be the name of the table, it will have a varchar 50
"@PI_IdEstadoProceso" will be the name of a column in table varchar 50
"@PI_IdSecuencia" will be the sequential of the table
"@PI_Duracion" will be an integer
"@PI_Error" will be a varchar of 50. and finally we insert the following code in sql server:
USE [mbajava]
/****** Object: StoredProcedure CREADO BY https://mbajava.com [dbo].[SP_RS_ActualizarCampos] Script Date: 20/05/2020 6:21:14 p. m. ******/
alter procedure [dbo].[SP_RS_ActualizarCampos]
@PI_NombreTabla varchar(50),
@PI_IdEstadoProceso varchar(50),
@PI_IdSecuencia int,
@PI_Duracion int,
@PI_Error varchar(50)

Declare @SQL VARCHAR(500)

set @SQL= 'UPDATE '+@PI_NombreTabla+' set EstadoDelproceso='''+@PI_IdEstadoProceso+''',
Fecha_Procesado=GETDATE(),Duracion='+Convert(varchar,@PI_Duracion)+', marca='''+@PI_Error+''' where Id_Secuencial='+Convert(varchar,@PI_IdSecuencia);


we use the "Convert (varchar" to convert from int to varchar
to execute we just send an exec

exec dbo.SP_RS_ActualizarCampos 'Table_Name', 'processed', 1,23, 'no error'