How to Access Django Server from Flutter App on Android

Written by hackerclrlzwhff00003b6s47atig9h | Published 2024/01/22
Tech Story Tags: backend-development | django-applications | flutter-django-integration | cross-device-api-calls | android-app-development | mobile-app-development | flutter-app-development | backend-configuration

TLDRDiscover a hassle-free way to call Django APIs from a Flutter app on different devices. Ensure your computer and phone are on the same network, find your desktop's IP address, and configure your Django server to run on that address. Access your APIs seamlessly and improve your development workflow across platforms.via the TL;DR App


Recently I was building aflutter app and was using Django to build its backend. I wanted to call by APIs from my app which was running on my phone and it was proving to be a hassle. However, after searching the net I finally found the way!!.

Let’s get started.

Step 1

First, you need to make sure that your computer and phone are connected to the same Wifi network. Alternatively, you can connect your computer to your mobile HotSpot.

Step 2

Find the IP address of your Desktop.

You can Type the Windows ComandLine

ipconfig

If you’re using a Linux or Mac operating system, type the following command in the terminal

ifconfig

The command will show you your IP Address (Example = 192.168.20.22)

Step 3

Naturally, when we run our Django server using Python:

python manage.py runserver

It runs on localHost( 127.0.0.1) which we cannot access from another device. So, instead, we will run our server on our computer’s IP Address.

In Django, we do that by running the following command:

python manage.py runserver 0.0.0.0:8000

This command will run our server on our computer so that other devices on the same network can also access the server. Also, don't forget to add your IP address to the ALLOWED_HOSTS in the setting.py file of your Django project.

Step 4

Now open any browser and open http://YOUR_IP:8000/api_endpoint

For example:= http://192.168.20.22/my_site

You will be able to see the expected result displayed.

Conclusion

The Above example is tailored to Django but it can be done on any other framework. All you have to do is figure out how to run your server on your machine's IP address and connect to the same network. You can use the URL with the IP address and call from any application you are building.



Written by hackerclrlzwhff00003b6s47atig9h | I like code
Published by HackerNoon on 2024/01/22