Django is a Python framework used to build Dynamic modern day web applications. In this tutorial, I will show you how to install Django and Configure it on your Ubuntu Linux Desktop.
Setting Python3 as Default Python version
On my machine, I am running two versions of Python: version-2.7 the default version and version-3 to check the current version of Python just type the following command in your terminal.
$ python --version
Now we have to change the default version from 2.7 to 3.0, type in the following commands to perform the action(root access needed).
$ update-alternatives --remove python /usr/bin/python2 $ update-alternatives --install /usr/bin/python python /usr/bin/python3 1
Now check again the python version.
$ python --version
Installing Django using pip(Method-1)
pip is a python package management system, now we will install pip using the following apt command.
$ sudo apt-get install python3-pip
after pip installation, use the following command to download and install Django.
$ pip3 install django
After the installation is finished, verify it by using the following command.
$ django-admin --version
Installing the Django package(Method-2)
You can also install Django on your machine by installing the Django package using the following apt command.
$ sudo apt-get install python-django
Starting a new project in Django
Creating a new project in Django is a very simple job you can create a new project by using the following command:
$ django-admin startproject myproject
Now just go to the directory and run “manage.py”.
$ python manage.py runserver
if you run into the following error:
You have unapplied migrations; your app may not work properly until they are applied. Run 'python manage.py migrate' to apply them.
just do the following:
$ python manage.py migrate
Now open your web browser and go to the following URL http://localhost:8000 if it shows the following page then you have followed everything correctly.
Now we need to create a superuser for accessing the project’s admin panel.
$ python manage.py createsuperuser
after typing the following command just fill up the details and remember it won’t show your password in the terminal while you were typing it, so don’t worry.
open your browser and go to http://localhost:8000/admin and log in with your superuser credentials.
Django tutorials for further learning:
- Installing Django and Setting up the Environment
- Creating Models and Connecting to the Database API
- Creating Views and Routeing URL