Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Django Development on Android using Pydroid

5.00/5 (1 vote)
5 May 2020CPOL2 min read 35.1K   267  
Creating a Django app using Pydroid Android app
This is a demonstration of how Pydroid can be used to develop and test a Django application.

Image 1

Background

Django is a web application framework written in Python that helps web developers to build websites faster and without much hassle. It can be of particular use while developing complex database driven websites.

Pydroid is an easy to use and powerful Python IDE for Android. It has a lot of features to develop sophisticated python projects with ease. It also supports various libraries like PyQt5, Kivy, Jupyter, etc. It can be easily used to develop Django applications as well.

In Django, whenever the webserver receives a request for a resource, the urlresolver is used to match the URL with a list of patterns. When it finds the matching pattern, it sends the request to the related function called view. The view performs the required business logic and generates a response, which Django sends to the web browser.

Using the Code

Terminal can be started by choosing the Terminal option in the menu in Pydroid as follows:

Image 2

The following command can be used to install Django after starting terminal in Pydroid:

Image 3

To create a project, you can enter the following command in the directory where you want to create the project:

Image 4

The following directories/files are created:

helloproj/
    manage.py
    helloproj/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

The outer helloproj/ directory is the main container for the project, while the inner helloproj/ directory is the Python package for the project. The helloproj/urls contains the URLs for the project.

To create an application, you can enter the following command after changing to the directory containing the manage.py file:

Image 5

This will create the following directories/files:

helloapp/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

Edit the helloapp/views.py file as follows:

Image 6

The above code is for a simple index view, which returns an HttpResponse.

The next step is to map this view to a URL. This can be done by creating the urls.py file in the helloapp directory as follows:

Image 7

After creating the urls.py file in the helloapp directory, the app directory will be as follows:

helloapp/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    urls.py
    views.py

The next step is to edit the root urls.py file (helloproj/urls.py) as follows:

Image 8

In the above code, the include() function is used to refer to the helloapp.urls file.

Now you can run the server by the $ python manage.py runserver command as follows:

Image 9

After starting the browser and entering the url http://127.0.0.1:8000/helloapp/, it will show the following output:

Image 10

Points of Interest

I hope readers find this article useful and interesting.

History

  • 5th May, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)