Vishwa
Vishwa

Vishwa

Django: Getting Started

Django: Getting Started

Subscribe to my newsletter and never miss my upcoming articles

In this series, we are going to create a random motivational quotes generator using Django. This is a beginner-friendly series, so you don't have to have any prior knowledge of Django. Oh, by the way, it's pronounced JANG-OH, you're welcome. ;)

Disclaimer: I am not a Django expert. I am learning Django by doing & sharing articles like this. The code I write here might not be great, but I welcome your suggestions/feedback.

What is Django

Django is a web framework written in python with batteries included. It gives us several functionalities out of the box, such as user authentication, admin panel, etc.

Setup

Once you have python3 and pip installed, fire up the Terminal (on Mac/Linux) or command prompt (on windows) in a fresh new directory, say, quotesgen and let's get started.

  • virtualenv is a very helpful python module to build isolated environments. Let's begin by creating our virtualenv called venv for this project.

    python3 -m venv venv
    
  • Let's activate it now.

    source venv/bin/activate
    
  • The terminal/command prompt should look something like this once the virtualenv is activated. image.png

  • Install Django

    pip install -U django
    

My first Django app

Now that we have everything set up, we can start implementing our first ever Django application.

Let's start by creating a new Django project using django-admin. django-admin is a command-line utility that comes packaged with Django, it has several utilities to simplify the development; we'll learn about each of them as we progress into the course.

Just run the following command to create a django project called quotesgen.

django-admin startproject quotesgen .

This creates a manage.py file and a directory quotesgen with a bunch of files inside it. Overall, the directory structure should look something like this:

quotesgen
├── manage.py
└── quotesgen
    ├── __init__.py
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

Now let's go through each file generated.

  • manage.py: this file allows us to run commands such as making migrations, running the server, etc. We'll learn more about it soon.
  • quotesgen/__init__.py: Django generates a directory with the same name as our project name & a empty __init__.py file is created in it to indicate that it's a python package. quotesgen/settings.py: settings.py holds the entire project's configuration, including DB credentials, security keys, etc.
  • quotesgen/urls.py: urls.py file contains URL routes & mapping to appropriate packages.
  • quotesgen/wsgi.py & quotesgen/asgi.py: Both wsgi.py & asgi.py act as an interface for our web server. A default configuration is added to help us get started quickly.

Testing it out

Unlike other frameworks, we don't need to write any code to run the Django application. Django comes with a basic webpage built-in; we, simply need to run a command to view it.

To start the server, we need run:

python manage.py runserver

This will start the webserver & we should see a message saying Starting development server at http://127.0.0.1:8000/

Now, go ahead & open the URL http://127.0.0.1:8000 on your browser, and we should see a standard django template like this

image.png

Congratulations on setting up your first django app. 🎉 That's it for today, and in the second part of this series, we'll learn to create a new app, setting up custom URL routes, writing & reading from the database.

Thanks for reading. .

PS: This is my first time writing a blog, any feedback/suggestions towards improvement is really appreciated.

 
Share this