DEV Community

Cover image for Getting Started with Django
Raghu Kapur
Raghu Kapur

Posted on

Getting Started with Django

Takeaways from this article:

  1. Setting up python in Ubuntu system.
  2. Creating a virtual environment in python.
  3. Creating a sample application with Django Framework.

1. Setting up python in Ubuntu system.

1.1 Check if python exists in your system.

python3 --version
Enter fullscreen mode Exit fullscreen mode

If Python is not installed, continue to the next step, else skip to point 2.

1.2 Update and refresh repository list.

sudo apt update
Enter fullscreen mode Exit fullscreen mode

1.3 Install supporting softwares.

sudo apt install software-properties-common
Enter fullscreen mode Exit fullscreen mode

1.4 Install python 3.8

sudo apt install python3.8
Enter fullscreen mode Exit fullscreen mode

1.5 Check installed python version

python3 --version
Enter fullscreen mode Exit fullscreen mode

2. Creating virtual environment in python.

2.1 Install python-pip

sudo apt install -y python3-pip
Enter fullscreen mode Exit fullscreen mode

2.2 Install python virtual-environment

sudo apt install -y python3-venv
Enter fullscreen mode Exit fullscreen mode

2.3 Create virtual-environment

python3 -m venv <virtual_enviornment_name>
Enter fullscreen mode Exit fullscreen mode

2.4 Activate virtual-environment

source <virtual_enviornment_name>/bin/activate
Enter fullscreen mode Exit fullscreen mode

Your command prompt will now be prefixed with the name of your environment:

(virtual_enviornment_name) raghu:~/Projects
Enter fullscreen mode Exit fullscreen mode

2.5 Test your virtual-environment

python3
Enter fullscreen mode Exit fullscreen mode

This will open a python shell for you.

Congratulations your virtual environment has been created, let's create our first Django app.


3. Creating a sample application with Django Framework.

3.1 Check if Django exists.

django-admin --version
Enter fullscreen mode Exit fullscreen mode

If Django is not installed, continue to the next step, else skip to step 3.3.

3.2 Install Django

sudo apt install python3-django
Enter fullscreen mode Exit fullscreen mode

3.3 Setting up Django and starting your server.

django-admin startproject <project_name>
Enter fullscreen mode Exit fullscreen mode

This command would have created a folder with your project name. Navigate to the folder using the "cd" command.

Inside the folder you will see the file "manage.py", a key file in Django projects.
Run the following command to migrate the database(Django uses SQLite by default) and sync your changes.

python3 manage.py migrate
Enter fullscreen mode Exit fullscreen mode

This will create some migrations and extra files, which we will discuss in the upcoming blogs.

For now you can start your webserver using the following command:

python3 manage.py runserver
Enter fullscreen mode Exit fullscreen mode

In the terminal you will see the development server starting at http://127.0.0.1:8000/

You can open the same URL in the browser to see your project
Alt Text


4. Conclusion

In this blog we learned about how to setup a Django Application.
In the upcoming blogs we will learn to create a fully functional application.

Top comments (2)

Collapse
 
utsavtulsyan profile image
Utsav Tulsyan

Great Stuff! Quite easy to follow.

Collapse
 
kapur_raghu profile image
Raghu Kapur

Thanks!!