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

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

Code for Select Operation from Database in Django



views.py

from django.shortcuts import render
from . models import Course
def index(request):
if request.method=='POST' and request.POST['btnsubmit']:
p = request.POST["txtp"]
r = request.POST["txtr"]
if request.POST['btnsubmit']=='+':
c=int(p)+int(r)
elif request.POST['btnsubmit']=='-':
c=int(p)-int(r)
elif request.POST['btnsubmit']=='*':
c=int(p)*int(r)
return render(request,"siapp/index.html",{'res':c})
return render(request,"siapp/index.html")
def reg(request):
return render(request,"siapp/reg.html")
def reglogic(request):
s = request.GET["course"]
cmb=request.GET["country"]
msg = request.GET["txtpost"]
s1=''
data = request.GET.getlist('c[]')
for d in data:
s1=s1+d+" "
st = request.GET.getlist('state[]')
stdata=''
for d in st:
stdata=stdata+d+" "
c = Course(adcourse=s,basiccourse=s1,countryname=cmb,statename=stdata,message=msg)
c.save()
return render(request,"siapp/reglogic.html",{'res':s+' '+s1+' '+cmb+' '+stdata+' '+msg})
def viewall(request):
r = Course.objects.all()
return render(request,"siapp/viewreg.html",{'res':r})

reg.html

<!DOCTYPE html> <html> <head> <title></title> </head> <body> <form action="reglogic" method="get"> <input type="radio" name="course" value="PYTHON" />PYTHON<input type="radio" name="course" value="PHP" />PHP<br><br> <input type="checkbox" name="c[]" value="C" />C<input type="checkbox" name="c[]" value="CPP" />CPP<input type="checkbox" name="c[]" value="DS" />DS<br><br> <select name="country"> <option value="">Select Country</option> <option value="India">India</option> <option value="USA">USA</option> <option value="CHINA">CHINA</option> </select> <br><br> <select name="state[]" multiple=""> <option value="MP">MP</option> <option value="UP">UP</option> <option value="AP">AP</option> <option value="HP">HP</option> </select> <br><br> <textarea name="txtpost"> </textarea> <br><br> <input type="submit" name="btnsubmit" value="Click" /> </form> <a href="viewall">View All</a> </body> </html>

viewreg.html
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
{% for data in res %}

<p>{{data.adcourse}} || {{data.basiccourse}} || {{data.countryname}} || {{data.statename}} || {{data.message}}</p>
{% endfor %}
</body>
</html>

models.py:

from django.db import models

class Course(models.Model):
adcourse=models.CharField(max_length=50)
basiccourse=models.CharField(max_length=50)
countryname=models.CharField(max_length=50)
statename=models.CharField(max_length=50)
message=models.CharField(max_length=50)
def __str__(self):
return "Advance Course:"+self.adcourse +" basiccourse is "+self.basiccourse +" Country "+self.countryname +" Statename is "+self.statename +" Message is "+self.message


urls.py:-

from django.urls import path
from . import views

urlpatterns=[
  path('',views.index,name='index'),
  path('home',views.index,name='index'),
  path('reg',views.reg,name='reg'),
  path('reglogic',views.reglogic,name='reglogic'),
  path('viewall',views.viewall,name='viewall')




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