Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 Radion Button Example in Django to Change Background color on Django?

Radiobutton is used to provide option based UI where we can select only a single option.

Syntax for RadioButton

<input type="radio" name="r"   value= "xyz"  /> XYZ

<input type="radio" name="r"   value= "ABC"  /> ABC

Now I am explaining one example of RadioButton to take three different radio button to change background color

Code of

from django.urls import path

from . import views







Create two method on to load design and write code?

def radiodesign(request):

return render(request,"employee/radiodesign.html")

def radiocode(request):

color = request.POST["color"]

return render(request,"employee/radiodesign.html",{'key':color})


create templates/appname/radiodesign.html

<!DOCTYPE html>





<body bgcolor="{{key}}">

<form action="radiocode" method="post">

    {% csrf_token %}

     <input type="radio" name="color" value="Red" {% if key == 'Red'  %} checked="true" {% endif  %}>Red <br><br>

     <input type="radio" name="color" value="Green" {% if key == 'Green'  %} checked="true" {% endif  %}>Green <br><br>

     <input type="radio" name="color" value="Blue" {% if key == 'Blue'  %} checked="true" {% endif  %}>Blue <br><br>

     <input type="submit" name="btnsubmit" value="Click" />




Now you can implement the program, when we click on the radio button then the background color will be changed on the webpage.

I am using <body bgcolor="Red" >   property of body where the color will be implemented by logic method.



Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post