DEV Community

Cover image for Django Rest API x Flutter
Showmik
Showmik

Posted on

Django Rest API x Flutter

It is amazing that two different technologies can work together.
In this article we will find out how can we use django rest API in a flutter app. sound exiting? let's find out how...

  • At first we will create rest api with django rest framework.
  • with this two line we have created a django project along with an django app
Django-admin startproject flutterApi .
Django-admin startapp api
Enter fullscreen mode Exit fullscreen mode
  • Now we have to install django rest framework
pip install djangorestframework
Enter fullscreen mode Exit fullscreen mode

after that add

'rest_framework' in

flutterApi/settings.py
in your codebase

in settings.py

  • Now we have to create a model for our django database. This is a simple data model.

api/models.py

Django model

Now we will migrate the model by using these two lines

py manage.py makemigrations
py manage.py migrate
Enter fullscreen mode Exit fullscreen mode

now the model is migrated.

We will set the url setting for our api.

flutterapi/urls.py we create the api designation

Image description

after that we crate a urls.py file in 'api' folder in your project structure

Image description

  • In this file we will create the api get, post, update, delete urls.

url patterns

After this we have to make the serializers for the api.

  • crate serializers.py file like api/serializers.py

then write the code like this.

serializers

Now let's write the actual code for developing the api
first go to api/views.py

In here we will be writing the code.
first import the models and serializers.

import

The code..
view

view

view

  • Now register your model in api/admin.py file

admin.py

So we are done building the API! let's test it now
first run the django server by writing below code in console

py manage.py runserver
Enter fullscreen mode Exit fullscreen mode

you should see something like this

server

the server is at http://127.0.0.1:8000/.hit it on your browser.

at first you will see this error but do not panic.

error

just hit http://127.0.0.1:8000/api to see all possible urls.
you should see something like this.

api

before see the api data we need to add data in our database. which is the admin panel in django. one of the best things in djnago.

py manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

use this for createing you user admin. Fill with name username, password and the hit enter. after this run the server again

http://127.0.0.1:8000/admin
Enter fullscreen mode Exit fullscreen mode

login with your user and password which you just created.

after login you will see

Image description

click products and then click add.

Image description
fill this with some values. ad hit save.
example:

Product name: OnePlus Nord 2 5G 12GB/256GB

Price: 40000

Prdoduct link = https://www.pickaboo.com/oneplus-nord-2-5g-12gb-256gb.html

Image: https://azse77seaprodsa.blob.core.windows.net/b2b-dr-
pickaboocdn/media/catalog/product/cache/ad6eca43ef1a34982ffeb01940d91d5b/n/o/nord-2-5g-12gb-256gb-7-16-2022.jpg
Enter fullscreen mode Exit fullscreen mode

after saving the data follow the api links.

Image description

if we go to http://127.0.0.1:8000/api/product-list/ like the urls we should see the data as json format like this.

Image description

So our rest api creating is done! Now we have to prepare the flutter part!

Flutter app

For creating a flutter app we use

flutter create app_name
ex: flutter create django_rest_api_x_flutter 
Enter fullscreen mode Exit fullscreen mode

in your flutter project pubspec.ymal file add these dependencies

Image description

  • I will use GetX state management to manage your data in more efficient way.

  • I also used flutter_staggered_grid_view to use in my project. it is very efficient for making grids in flutter.

I am not going to explain the designing part of flutter. I will be explaining the modeling and http part in this blog.
you can download or fork the project from here.

https://github.com/Miskat-UL/GetX-rest-api-flutter

don't forget to give it a star!

Now in your flutter project structure your files like this

Image description

In remote_service.dart file we will be importing the http packages.

Image description

we have to use the http module as follows.
for cors we used the django cors.but it is not a matter now.
just use the origin as headers so that the browser can access xml requests.
the code gose here.

Image description

  • in product_controller.dart we make the getx controller

Image description

  • we are almost done. fork the github project to learn more with the code.

  • after this run the flutter project from your ide or cmd.

  • make sure to run your django server

the app should run properly and the final output is kind of this

Image description

Thanks for reading the blog. I hope you learned something.
give a like and share with your friends!

Top comments (2)

Collapse
 
miskat profile image
Showmik

Make sure to add your django app(in this case the django app name is 'api' ) in the settings.py file

Collapse
 
yel59 profile image
Md. Tofayel Islam

owao that is cool!!! it's good to see you in dev.to. carry on!