loading...

How I ran Django on my android phone βŒπŸ’»

coledrain profile image ColeDrain ・2 min read

How I ran Django on my android phone...

Yeah, it seems weird coding with your phone, but right now I don't have an option, so if you are like me or you just fancy the idea, you can also do this. This is not an introduction to Django tutorial but how to work on Django with your android.


First we need something like a Linux terminal(or CLI) for android. Lucky for us, there is a terminal emulator on Google play store called Termux, so head to playstore and install it.
Link:termux


Okay, so you have installed it, what next?
So basically you can do whatever you could do on a Linux terminal now with this app(create directories, move files, change directory). This is how Termux looks like.

Alt Text

So let's roll in. Enter this command to view where Termux saves your files.

$ pwd

Alt Text
So that's where Termux saves your files, but the problem is you can't access that folder because of safety issues.

So to be able to access your files..
run this command

$ termux-setup-storage

And allow permission to storage in the popup shown.
Alt Text
Now change directory to your internal shared storage by running this command.

$ cd /sdcard

N:B this command doesn't point to your external SD card but your internal SD card(storage).


Then let's create a folder to store our Django projects, let's call it django_projects.
So run this command.

$ mkdir django_projects

$ cd django_projects


Now let's install python
To install python, run this

$ pkg install python

To check if python is installed, run the python command to enter python console.

$ python

N:B Use ctrl + z to exit the terminal. Termux app comes with a ctrl key.


Let's create a project called mysite
So still in that django_projects, run this commands.

$ mkdir mysite

$ cd mysite

We need a virtual environment to avoid dependencies clash. So let's install a virtual environment, run this command.

$ pip install virtualenv

After installing it, run

$ virtualenv venv

to create a virtual environment.. Then activate the virtual environment by running (note the full stop)

$ . venv/bin/activate


Now we have a virtual environment, let's install Django. run..

$ pip install Django

Okay, so Django is installed in our venv, let's create a Django project. run..

$ django-admin startproject hello .

(note the full stop)

Then let's run migrations..

$ python manage.py migrate

You can then run the server..

$ python manage.py runserver

voilΓ , go to your phone browser and open http://127.0.0.1:8000 and you should see this.
Alt Text

You can always view your files by going to your file manager and searching for django_projects folder.

Feel free to indulge me, if any issues.

Discussion

pic
Editor guide
Collapse
robert197 profile image
Robert

When i started my studies back in 2013 they told me: "Java is so great and popular because it is running on every machine." And we started to learn java. πŸ˜‚
Now i see people running Django server on a phone in few simple steps.

Django Unchained

Collapse
coledrain profile image
ColeDrain Author

πŸ˜πŸ˜πŸ˜…

Collapse
chuckis profile image
Ruslan Ishchuk

after trying to install virtualenv I got this:

WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping

Collapse
coledrain profile image
ColeDrain Author

Try running this command:

pkg upgrade

Collapse
chuckis profile image
Ruslan Ishchuk

suspecting: my phone must be rooted?

Thread Thread
coledrain profile image
ColeDrain Author

no it isn't compulsory.

Thread Thread
chuckis profile image
Ruslan Ishchuk

after trying to create
virtual environment I got a 'permission error'
dev-to-uploads.s3.amazonaws.com/i/...

Thread Thread
coledrain profile image
ColeDrain Author

I guess you have ran this command before:

termux-setup-storage

run it again then try creating the virtualenv, sorry for the issues so far. 😬

Collapse
codeperfectplus profile image
CodePerfectPlus

It's hard to manage files and code on the phone. Not recommended.

Collapse
coledrain profile image
ColeDrain Author

ikr, just if you have no option. It's not that bad tho. Some can't afford a laptop, so you can do with what you have.

Collapse
pomfrit123 profile image
***

A laptop from the previous century is better than a phone :D

Thread Thread
coledrain profile image
ColeDrain Author

😁😁

Collapse
tombohub profile image
tombohub

I found my self many times waiting for something somewhere and then wished I can work on my project while waiting. Can we use git too?

Thread Thread
coledrain profile image
ColeDrain Author

yeah.. Sure

Collapse
faraixyz profile image
Farai Gandiya

That’s cool. I think it might work on iOS too with Pythonista.

Collapse
coledrain profile image
ColeDrain Author

That's cool, thought IOS was kinda strict.

Collapse
faraixyz profile image
Farai Gandiya

They’ve loosened up a bit with interpreted languages though this app was around way before then. Dependency management with it is still annoying and I’m not sure what the developer is working on now. There’s another app called play.js which does a good job with JavaScript.

Thread Thread
coledrain profile image
ColeDrain Author

Wow, that's really interesting to know.

Collapse
hukuta profile image
Nikita

Interesting what else we can do with termux πŸ€”. It has api to use some native phone features, like get location, send notifications and more. But I don't know really where it will be useful

Collapse
coledrain profile image
ColeDrain Author

From their wiki.
There are a number of common use-cases for the Termux application:

Data processing with Python.
Programming in a development environment.
Downloading and managing files and pages using time-established tools.
Learning the basics of the Linux command line environment.
Running an SSH client.
Synchronizing and backing up your files.
Of course, usage is not limited to the topics listed above. There are more than 1000 packages in our repositories. If these packages don't have what you're looking for, you can compile your own - we have a variety of build tools, including compilers for languages like C, C++, Go, Rust. Interpreters for common languages like NodeJS, Python, Ruby are available too.

Collapse
hukuta profile image
Nikita

for data processing, it would be more suitable to buy a minimal VPS or even free. For example google cloud (free) is enough to run vs code in browser mode, and much more. I suspect that bash with android integration is more interesting related with the features of the device. For everything else you can connect to the server and achieve full opportunities

Collapse
pratham82 profile image
Prathamesh Mali

Just tried it πŸ˜‚ my friends loosing their mind.

Collapse
coledrain profile image
ColeDrain Author

πŸ˜†
Ha, it's very cool.

Collapse
gatsbysex profile image
Matz

$ virtualenv env
PermissionError: [Errno 1] Operation not permitted: '/data/data/com.termux/files/usr/bin/python3' -> '/storage/emulated/0/aamydjango/Django/env/bin/python'