Generate random numbers and background color in Django


On this article, we’ll 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 internet framework referred to as Django allows you to simply construct efficient on-line apps. As a result of Django has built-in options for all the pieces, together with the Django Admin Interface and the default database, SQLlite3, it’s usually referred to as a batteries-included framework.

File Construction

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

 

Stepwise Implementation

Step 1: Create a Digital Atmosphere, You possibly can skip this step if you would like.

Step 2: Step up Django Set up.

Open Command Immediate or Terminal and Set up Django

pip set up Django

Step 3: Begin a venture by the next command, and Change the listing to Check 

django-admin startproject Check
cd Check

Step 4:  Create an app in Django

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

python handle.py startapp GenerateRandom

Step 5: Add assets 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 Check/settings.py.

Add the next code to the top of the file. 

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

STATICFILES_DIRS = [
   os.path.join(BASE_DIR,"static")
]

 

Step 7: Now discover Templates and add the next code to DIRS.

os.path.be 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 urls.py  for the corresponding view, now edit the urls.py of the app and add a brand new path to it.

HTML

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

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

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

    <title>Generate Random Quantity</title>

    <fashion>

        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

        }

    </fashion>

</head>

<physique>

    <middle>

        <h1>Generate Random Quantity</h1>

        <button onclick="location.href="https://www.geeksforgeeks.org/generate-random-numbers-and-background-colour-in-django/{% url"script' %}'">Generate</button>

    </middle>

    {% if quantity %}

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

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

    {% endif %} 

</physique>

</html>

Step 9: Configure the Check/urls.py file.

Python3

from django.contrib import admin

from django.urls import path, embrace

  

urlpatterns = [

    path('admin/', admin.site.urls),

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

]

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

Python3

from django.contrib import admin

from django.urls import path, embrace

from GenerateRandom import views

  

urlpatterns = [

    path('admin/', admin.site.urls),

    path('', views.index),

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

]

Step 11: Open GenerateRandom/view.py 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.

Python3

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 Every thing is ready let’s run the server and preview our utility. To run the server enter the next command:

python handle.py runserver

Output:

 

For supply code checkout this GitHub Repository.

Leave a Reply