The steps are:

  1. Create a directory. mkdir '$MY_DIR_NAME'.

  2. Navigate to that directory.
    cd $MY_DIR_NAME.

  3. Setup a virtual environment inside the directory. python -m venv .env Here, .env is name of virtual environment. You can name it as you like. But I started with period . so it gets listed at top in explorer view of IDEs and makes easier to explore through project files.

  4. Activate the virtual environment. .env\Scripts\activate

  5. Install django library. pip install django

  6. Create a django project. django-admin startproject my_project . Here my_project is the name of the django project and . makes project files accessible in current working directory.

  7. Test the installation. python runserver

  8. Now you’ve django setup on your system. Let’s start making modular apps. Break the running server with Ctrl + C and type the following command: python startapp luck Here luck is name of the project. The project structure looks like following inside the luck application.

    	├── migrations
    	│   └──


    Don’t worry, pal. I will be guiding you through each of the files.

    Each app has a file identifying it as a Python package. There are 6 new
    files created: is a configuration file for the built-in Django Admin app. is a configuration file for the app itself.
    • The migrations directory stores migrations files for database changes. is where we define our database models. is for our app-specific tests. is where we handle the request/response logic for our web app.
    Typically developers will also create an file within each app too for routing.

  9. Let’s register the newly created app into the INSTALLED_APPS inside the

    # my_project/
    # ======================

    ` Please take care of the indentation and don’t forget to provide comma as writing comma will help to ignore error at near future.

  10. After adding the app to INSTALLED_APPS, let’s start to code some logic into the luck app we created earlier.

    # luck/
    # ==============
    from django.shortcuts import render
    import random
    def  guess_luck(request):
    	guess = random.randint(3,6)
    	user_guess =  4
    	luck =  False
    	if user_guess == guess:
    	luck =  True
    	context = {
    		'result': luck
    	return render(

    We’ve created a logic where an arbitary number 4 tries to match to with the random number from 3 to 6 and if they got matched, the luck variables is assigned to True else False.

  11. Notice the 'luck/index.html' inside the render function, which refers to the template file location where this reponse will be displayed. Till now, we’ve not created any temlate directory nor template (HTML) file to display the response. But, first we need to tell Django where to look for our template files. For doing so,

# my_project/

		'BACKEND': 'django.template.backends.django.DjangoTemplates',
		'DIRS': [
			'templates', # Modification here
		'APP_DIRS': True,
		'OPTIONS': {
			'context_processors': [
By mentioning the folder inside `DIRS` list, we're telling django to look for templates inside `templates` directory. By default, Django looks for templates inside all the path mentioned inside `DIRS` list. So, be careful while modifying the list.
  1. Now, create a templates directory inside luck directory. cd luck && mkdir templates

  2. Again, create a directory named luck inside templates directory we recently created and create a file named index.html inside the luck directory where we will write HTML. mkdir luck

  3. It’s time to write some HTML, pal.

    # luck/templates/luck/index.html
    <!DOCTYPE  html>
    <html  lang="en">
    	<meta  charset="UTF-8">
    	<meta  name="viewport"  content="width=device-width, initial-scale=1.0">
    	<title>Guess luck</title>
    	Your luck is {{result}} today.

    Notice the {{result}} inside <body> tag. It is the context value passed from luck/ through render function.

  4. Now, let’s setup URL configuration for our project.

    # my_project/
    from django.contrib import admin
    from django.urls import path, include
    urlpatterns = [
    	path('luck/', include('luck.urls')),

    For routing url routes to our app luck, let’s include all the URL routes starting from luck/ to the of luck app. Notice the quotes sourrounding 'luck.urls' inside include function. But, something seems fishy. There is no inside luck app. Alright, let’s create one. After creating inside luck app. The URL request get routed to our app. Now, let’s make our app handle the URL routes with the function and templates we created recently.

    # luck/
    from django.urls import path
    from .views import guess_luck
    urlpatterns = [
    	path('', guess_luck)

    This will route /luck/ request from browser to guess_luck view and that will render luck/index.html. So easy, right ?

    Let’s run the server if it works. python runserver


    That’s what we expected. The mighty luck checker, that may save the planet one day.

DISCLAIMER : Try using this app only once. Results may vary upon reloading.