Wednesday, February 8, 2023
HomeSoftware DevelopmentGenerate random numbers and background color in Django

Generate random numbers and background color in Django

On this article, we are going to learn to generate random numbers and background colours in Python Django. And we will even see completely different steps associated to a random quantity in Django. These are the next steps that we’re going to focus on on this tutorial:

  • What’s the Django?
  • Features to generate a random quantity
  • Features to generate random background shade
  • Django random quantity in template

What’s Django?

A Python-based net framework referred to as Django allows you to simply construct efficient on-line apps. As a result of Django has built-in options for the whole lot, together with the Django Admin Interface and the default database, SQLlite3, it’s usually often called a batteries-included framework.

File Construction

That is the ultimate file construction after we did our all stuff.


Stepwise Implementation

Step 1: Create a Digital Surroundings, You may skip this step in order for you.

Step 2: Step up Django Set up.

Open Command Immediate or Terminal and Set up Django

pip set up Django

Step 3: Begin a mission by the next command, and Change the listing to Take a look at 

django-admin startproject Take a look at
cd Take a look at

Step 4:  Create an app in Django

Word:  ‘GenerateRandom’ is the identify of the app you’ll be able to change it accordingly.

python startapp GenerateRandom

Step 5: Add sources to your Django app. Create a templates listing to carry HTML pages. Open any code editor and add each directories to the trail. To your reference, you’ll be able to see the file construction given above. 

Step 6: Edit Take a look at/

Add the next code to the top of the file. 

Word:  Keep in mind to Import the os module into the code.



Step 7: Now discover Templates and add the next code to DIRS. a part of(BASE_DIR,"templates")


Step 8: Add GenerateRandom/index.html information to the template listing. Right here, the script is the identify that’s outlined within the  for the corresponding view, now edit the of the app and add a brand new path to it.


<!DOCTYPE html>

<html lang="en">


    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Suitable" content material="IE=edge">

    <meta identify="viewport" content material="width=device-width, initial-scale=1.0">

    <title>Generate Random Quantity</title>


        physique {

            background-color: rgb(249, 255, 195);


        button {

            margin-bottom: 15px;

            border-radius: 15px;

            background-color: rgb(164, 211, 252);

            padding: 5px;

            padding-left: 10px;

            padding-right: 10px;

            font-size: 15px;

            font-family: 'Courier New', Courier, monospace;


        h1 {

            font-family:'Courier New', Courier, monospace






        <h1>Generate Random Quantity</h1>

        <button onclick="location.href="{% url"script' %}'">Generate</button>


    {% if quantity %}

    <heart>Random Quantity is: {{quantity}}</heart>

    <script>doc.physique.type.backgroundColor= "#{{quantity}}"</script>

    {% endif %} 



Step 9: Configure the Take a look at/ file.


from django.contrib import admin

from django.urls import path, embody


urlpatterns = [


    path('', include('GenerateRandom.urls'))


Step 10: Open the newly created GenerateRandom/ within the app and add the next code it.


from django.contrib import admin

from django.urls import path, embody

from GenerateRandom import views


urlpatterns = [


    path('', views.index),

    path('output', views.output, name='script')


Step 11: Open GenerateRandom/ within the app.

  • Create an index operate to render index.html.file
  • The generate operate generates a random quantity by Python and shows the quantity and modifications the background shade randomly, so making a generate operate that generates a random quantity and shade.
  • Create a brand new output operate after which name the beforehand created generate() operate in it.


from django.shortcuts import render, HttpResponse


def index(request):

    return render(request, 'index.html')


def generate():

    from random import randrange

    num = randrange(100000, 1000000)

    return num


def output(request):

    context = {

        'quantity': generate()


    return render(request, 'index.html', context)

Step 12: Deploy the Mission.

Now The whole lot is ready let’s run the server and preview our utility. To run the server enter the next command:

python runserver



For supply code checkout this GitHub Repository.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments