Design a site like this with WordPress.com
Get started

Django – Templates Basic

Lets see how to respond with an html in django. Till now we have seen how to respond to a rest call with a json response. But we will also need to respond to a request with an html/css/javascript. Lets create a small django app to demonstrate the same.

GIT URL – https://github.com/letsblogcontent/Python/tree/master/TemplateDjangoProject

Step 1

Open command terminal and create a directory “TemplateDjangoProject” and navigate into that project

Step 2

First install Django with command “pip install django”

Step 3

Create a project with command “django-admin startporject testproject . “

Step 4

Now create app with command “python manage.py testapp”

Step 5

Add the newly created app in the settings.py file of the testproject folder under the installed_apps array as below to register our new app

Step 6

Now, create a new folder templates under the app testapp folder and create index.html file with content as below

Step 7

Create a function in the views.py file of the app testapp as below

Step 8

update the urls.py(create if not present) file as below. Here we are mapping our url to the function we created in the views.py file in the previous step

Step 9

Now update the urls.py of the our testproject to include the urls created in our app.

Step 10

We are done. Now run the server executing the command “python manage.py runserver” and then navigate to the deploy url to see below response from the server

Advertisement

Django Rest Framework – Part2

In the previous tutorial we have created a simple rest framework and configured a “GET” request and successfully displayed data from the server through Django Rest framework. Now lets learn quickly how to add a POST request so that we can add new products through a rest call and then view it later.

Step1

Lets first define a new function in the views.py file in the products app and decorate it with POST attribute of the api_view annotation. Lets call this function as add_product. We fetch the data into the serializer instance of ProductSerializer and then check if the object is a valid Product object. This will confirm if we have received correct types in the fields as defined in our Product class. Once it is termed valid by the framework, we save it into the database.

Step 2

Now that we have defined the function we need to call, we need to configure the url that it should map to. So lets go the url.py in the products app and configure “product” to map to our newly defined function.

Step 3

And we are done with our project configuration. Now lets send a rest call to our server with the product object and check if the save was successful. I am using ARC(Advanced Rest Client) to send the rest request, you can use any one you like.

As you can see I have received a successful response back from the server. I have save a new product having name “Samsung X2” Make sure to choose a correct content type and POST method as shown else it will result into an error.

Step 4

Now lets retrieve the data of all the products and see if the data is saved.

P2 – Django Rest – Post Request

In the previous tutorial we have created a simple rest framework and configured a “GET” request and successfully displayed data from the server through Django Rest framework. Now lets learn quickly how to add a POST request so that we can add new products through a rest call and then view it later.

Step1

Lets first define a new function in the views.py file in the products app and decorate it with POST attribute of the api_view annotation. Lets call this function as add_product. We fetch the data into the serializer instance of ProductSerializer and then check if the object is a valid Product object. This will confirm if we have received correct types in the fields as defined in our Product class. Once it is termed valid by the framework, we save it into the database.

Step 2

Now that we have defined the function we need to call, we need to configure the url that it should map to. So lets go the url.py in the products app and configure “product” to map to our newly defined function.

Step 3

And we are done with our project configuration. Now lets send a rest call to our server with the product object and check if the save was successful. I am using ARC(Advanced Rest Client) to send the rest request, you can use any one you like.

As you can see I have received a successful response back from the server. I have save a new product having name “Samsung X2” Make sure to choose a correct content type and POST method as shown else it will result into an error.

Step 4

Now lets retrieve the data of all the products and see if the data is saved.

Django Rest Framework -Part1

We will quickly go through a simple tutorial to create django rest project. It is extremely easy to configure a rest project work with Djange rest framework. Lets go through the steps. I am using PyCharm but it is not required but it will make development much easier with PyCharm. So lets begin.

Step1

Create a new project in PyCharm. Those not using PyCharm can skip this step. Create a directory otherwise.

Step 2

Install django using command “pip install django==3.1” on the terminal. You should see a successful installation of the django library.

Step 3

Now create a django project using command on terminal “django-admin startproject restproject .” “.” is used to create project in the current directory

Step 4

Now that we have the project ready, we will create a django app. The project consist only the configuration information. The app contains the application code. Execute the command “python manage.py startapp products

Step 5

Lets now create a model to store and retrieve. Lets create a model with name “Product” with fields “name”, “price”, “desc” as per the below screenshot.

Now, execute the command “python manage.py makemigrations” and “python manage.py migrate” . You will see following output. This will create a table in sqlite3 db being used in django.

Step 6

Install the django rest framework executing the command “pip install djangorestframework”

Step 7

Create a new file with name “serializers.py” under the products app as shown in the snapshot below.
The “serializers” are used to transform between various formats between the rest client and the server so that communication can happen seamlessly. Here we create a class ProductSerializer class which extends from the ModelSerializer class. The ModelSerializer class maps the fields between the model and serializer and performs validation based on the model defined under the meta class. We don’t need to think much here, just we need to understand that this is required for transofmation between an object to json/xml or vice versa.

Step 8

Now update the views.py file under the products app as below. Here we create a new function “product_list” which takes request as input parameter. We retrieve all the elements in the products table and then use the ProductSerializer created in the previous step to create a json response. Please see 1,2,3 points in the snapshot. We annotate the function to process “GET” request with api_view

Step 9

Now create a urls.py file in the products app if not present already and map “products” url to “product_list” function we created in the views.py file

Now update the urls.py file in the restproject as below to map urls starting with api to the products app

Step 10

We are done with the rest framework basic setup. To demonstrate its working, lets add some values to the products object/table using the admin functionality provided by django.

First register your “product” object to the admin. For this create a file admin.py file under products app and update with below content.

Lets create a superuser to manage admin functionality provided by django. Execute below commands. Provide details of username and password to create a super user

Now lets run our server with command “python manage.py runserver“. This will run the server at http://localhost/8000

Now navigate to url http://127.0.01:8000/admin. you will see below below screenshot and provide username and password created in the step above. for me it is admin/admin. Provide the details and press login.

Now you should see below screenshot

Now lets add a few products to the list, click on add and fill the details

This should add to the products table. This is really easy and fast way to populate some data. The admin functionality can do a lot of this, but will see more of it in future tutorials.

Step 11

Now we are all set to retrieve the list of the products using django rest framework. Hit the url http://127.0.0.1:8000/api/products to see the products we have created in previous step. You should response in json for the products you have created.


We have just now successfully created a basic setup of django restframework. We will look at some advanced features in the next tutorial. The git link for this project is “https://github.com/letsblogcontent/SampleRestProject


P1 – Django Rest – Project Creation

Learn Django project creation. Its is extremely easy and fast to develop with django

We will quickly go through a simple tutorial to create django rest project. It is extremely easy to configure a rest project work with Djange rest framework. Lets go through the steps. I am using PyCharm but it is not required but it will make development much easier with PyCharm. So lets begin.

Step1

Create a new project in PyCharm. Those not using PyCharm can skip this step. Create a directory otherwise.

Step 2

Install django using command “pip install django==3.1” on the terminal. You should see a successful installation of the django library.

Step 3

Now create a django project using command on terminal “django-admin startproject restproject .” “.” is used to create project in the current directory

Step 4

Now that we have the project ready, we will create a django app. The project consist only the configuration information. The app contains the application code. Execute the command “python manage.py startapp products

Add the newly created app to the settings.py file of the project, in our case it is our restproject.

Step 5

Lets now create a model to store and retrieve. Lets create a model with name “Product” with fields “name”, “price”, “desc” as per the below screenshot.

Now, execute the command “python manage.py makemigrations” and “python manage.py migrate” . You will see following output. This will create a table in sqlite3 db being used in django.

Step 6

Install the django rest framework executing the command “pip install djangorestframework”

Import this into the installed apps of the project’s settings.py

Step 7

Create a new file with name “serializers.py” under the products app as shown in the snapshot below.
The “serializers” are used to transform between various formats between the rest client and the server so that communication can happen seamlessly. Here we create a class ProductSerializer class which extends from the ModelSerializer class. The ModelSerializer class maps the fields between the model and serializer and performs validation based on the model defined under the meta class. We don’t need to think much here, just we need to understand that this is required for transofmation between an object to json/xml or vice versa.

Step 8

Now update the views.py file under the products app as below. Here we create a new function “product_list” which takes request as input parameter. We retrieve all the elements in the products table and then use the ProductSerializer created in the previous step to create a json response. Please see 1,2,3 points in the snapshot. We annotate the function to process “GET” request with api_view

Step 9

Now create a urls.py file in the products app if not present already and map “products” url to “product_list” function we created in the views.py file

Now update the urls.py file in the restproject as below to map urls starting with api to the products app

Step 10

We are done with the rest framework basic setup. To demonstrate its working, lets add some values to the products object/table using the admin functionality provided by django.

First register your “product” object to the admin. For this create a file admin.py file under products app and update with below content.

Lets create a superuser to manage admin functionality provided by django. Execute below commands. Provide details of username and password to create a super user

Now lets run our server with command “python manage.py runserver“. This will run the server at http://localhost/8000

Now navigate to url http://127.0.01:8000/admin. you will see below below screenshot and provide username and password created in the step above. for me it is admin/admin. Provide the details and press login.

Now you should see below screenshot

Now lets add a few products to the list, click on add and fill the details

This should add to the products table. This is really easy and fast way to populate some data. The admin functionality can do a lot of this, but will see more of it in future tutorials.

Step 11

Now we are all set to retrieve the list of the products using django rest framework. Hit the url http://127.0.0.1:8000/api/products to see the products we have created in previous step. You should response in json for the products you have created.


We have just now successfully created a basic setup of django restframework. We will look at some advanced features in the next tutorial. The git link for this project is “https://github.com/letsblogcontent/SampleRestProject


Simple Django Project

Steps to create a simple Django project in Python

Git Url – https://github.com/letsblogcontent/MyDjangoProject

Prerequisites –
-Python(>3.8) installed on your machine
-PyCharm(Optional)

I am using PyCharm as my IDE for developing this application. You can use PyCharm or anyother suitable IDE of your choice.

Steps

  1. Open PyCharm and select File -> New Project
  2. Provide a suitable name. In my case its MyDjangoProject

3. Now Install Django library. For this open the command terminal in PyCharm or on a command prompt and run “pip install DJango==3.1” Once it is done, you should see the below message.

4. Once it is installed, create a new Django project by running the command “django-admin startproject MyDjangoApp .”

Once completed, the IDE will refresh with a directory with name MyDjangoApp with files shown below

5. Now you can run this application with command. This command will run your application and delpoy listening on the port 8000

6. Your first Django application is up and running navigate to the url http://127.0.0.1:8000/ to view a default front page as below. To run the server on a different port use the following command

python manage.py runserver 8080