Django Configuration with Odinub

Hello Guys! For today’s post I will explain how to configure Django framework in python using Odinub. But before starting, there are a few basic things we need to know about the software. 

What is Django?

Django is a high-level Python Web framework that encourages rapid development and clean pragmatic design. Django’s primary goal is to ease the creation of complex database-driven websites. Some well-known sites that use Django include PBS, Instagram, Disqus, Washington Times, Bitbucket and Mozilla.

Django Configuration Using Python 

Django supports the model template view(MTV) software architectural pattern.
Django’s core principles are scalability, re-usability and rapid development. It is also known for its framework-level consistency and loose coupling, allowing for individual components to be independent of one another.
 

 How to install Django on Odinub?

Here we will use a python web framework called Django to create a dynamic web server in odinub. Django provides you with so many libraries, tools and technologies that allow you to build a web application it can be a webpage, a web server, a blog or a commercial website.
Step 0: WiFi Setup
For setup, the WiFi on your odinub board follows our another article.

Step: 1 Install python and pip
To install python on your odinub board follow below command

sudo apt-get install python

To verify the successful installation of python, run a version check with the python command :

python --V

now install pip via this command :

sudo apt-get install –y python-pip 

to verify that pip was successfully installed, run the following command:

pip --V 

Now that we have pip installed on our odinub board, we have the ability to quickly install other necessary packages for a python environment.
Step: 2 Install virtualenv
Virtualenv is a virtual environment where you can install software and python packages in a contained development space.
To install virtualenv, we will use the pip command as shown below:

pip install virtualenv

Once it is installed, run a version check to verify that the installation has completed successfully :

virtualenv --version 

The output will be like this : 

 

You have successfully installed virtualenv.
Step : 3 Install Django
We’ll be installing Django using pip within a virtual environment.
For that make a directory in the server’s home directory. Run the following command to create a directory called django-apps, or another name of your choice. Then navigate to the directory.

mkdir django-apps
cd django-apps

 Inside the django-apps directory, create your virtual environment. Let’s call it env.

virtualenv  env 

Now, activate the virtual environment with the following command :

. env/bin/activate 

You’ll know it’s activated once the prefix is changed to (env), which will look like this :

Within the environment, install the Django package using pip command.

pip install django 

Once installed, verify your Django installation by running a version check.

django-admin --version 

The output will be like this : 

 

Step: 4 Creating a Django Test Project
Starting the project
Now we can generate an application using django-admin, a command line utility for administration tasks in python.
Then we can use the startproject command to create the project directory structure for our test website.
In the django-apps directory, run the following command :

django-admin startproject testsite

Now we can look to see what project files were created. Navigate to the testsite directory then list the contents of that directory.

cd testsite

ls

 Output :

Here the manage.py file is similar to Django-admin and puts the project’s package on sys.path.
This also sets the DJANGO_SETTINGS_MODULE environment variable to point to your project’s settings.py file.
You can view the manage.py script in the terminal by running the less command :

less manage.py

When you are finished reading the script, press q, to quit viewing the file.
Now navigate to the testsite directory to view the other files that were created

cd testsites/

Then run the following command to list the contents of the directory.

 ls

You  will see four files :

 

Let’s brief that files what they actually are
•    __init__.py: acts as the entry point for your python project.
•    Setings.py describes the configuration of your Django installation and lets Django know which settings are available.
•    Urls.py contains a URL patterns list, that routes and maps URLs to their views.
•    Wsgi.py contains the configuration for the Web Server Gateway Interface is the python platform standard for the deployment of web servers and applications.
Start and View your Website
Now we can start the server and view the website on a designated host and port by running the runserver command.
We’ll need to add your server IP address to the list of ALLOWED_HOSTS in the settings.py file located in ~/test_django_app/testsite/testsite/.
You can use nano command to run settings.py script.

nano settings.py

Once you run the command you will have to navigate to allowed hosts section of the document and add your server’s IP address inside the square brackets within single or double-quotes.
You can save the changes and exit nano by holding down the CTRL + X and then pressing y key.
When you complete this make sure to navigate back to the directory where manage.py is located:

cd ~/django-apps/testsite/

 Now run the following command replacing the your-server-IP text with the IP of your server :

python manage.py runserver your-server-ip:8000

Finally, you can navigate to the below link to see what your Django website looks like, again replacing the highlighted text with your server’s actual IP:
http://your-server-ip:8000/
Once the page loads, you’ll see the following:

That were few steps to configure Django with Odinub. As a developer, having the knowledge to use and configure Django is very important. Odinub ensures that this is done in the most, user-friendly way possible. 
Do let me know about your projects with Odinub. Till then, keep innovating!
 

0 Commenting Overall Comment