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.
So let's roll in. Enter this command to view where Termux saves your files.
$ pwd
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.
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.
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.
Top comments (32)
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
πππ
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
Try running this command:
pkg upgrade
suspecting: my phone must be rooted?
no it isn't compulsory.
after trying to create
virtual environment I got a 'permission error'
dev-to-uploads.s3.amazonaws.com/i/...
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. π¬
It's hard to manage files and code on the phone. Not recommended.
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.
A laptop from the previous century is better than a phone :D
ππ
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?
yeah.. Sure
.../django1/mysite $ virtualenv venv
PermissionError: [Errno 1] Operation not permitted: '/data/data/com.termux/files/usr/bin/python3' -> '/storage/emulated/0/django1/mysite/venv/bin/python'
Any one help
Just tried it π my friends loosing their mind.
π
Ha, it's very cool.
Thatβs cool. I think it might work on iOS too with Pythonista.
That's cool, thought IOS was kinda strict.
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.
Wow, that's really interesting to know.
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
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.
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
hi