CRUD Operations using Django | CRUD Operation | Create Data | Read Data | Update Data | Delete Data | Django Framework | The ACX

 CRUD Operations using Django

  • Create Data 

    def index(request):
    if request.method == 'POST':
fm=student(request.POST)
if fm.is_valid():
nm = fm.cleaned_data['name']
em = fm.cleaned_data['email']
pw = fm.cleaned_data['password']
reg = User(name=nm, email=em, password=pw)
reg.save()
else:
fm=student()
return render(request,'index.html',{'form':fm})

  • Read Data
    def Show_data(request):
    stud = User.objects.all()
return render(request,'Showdata.html',{'stu':stud})
  • Update Data
    def Update_data(request):
    if request.method == 'POST':
       pi=User.objects.get(pk=id)
fm=student(request.POST, instance=pi)
if fm.is_valid():
nm = fm.cleaned_data['name']
em = fm.cleaned_data['email']
pw = fm.cleaned_data['password']
reg = User(name=nm, email=em, password=pw)
reg.save()
else:
pi=User.objects.get(pk=id)
         fm=student(instance=pi)
    return render(request,'updatedata.html',{'form':fm})
  • Delete Data
    def delete_data(request,id):
    if request.method=='POST':
pi=User.objects.get(pk=id)
pi.delete()
return HttpResponseRedirect('/')

Comments

Post a Comment

Popular posts from this blog

How to install Pip pillow | Install Pip Pillow | Python pip | Python pillow tutorial

How to create Splashscreen | Android Studio.

How to create Cardview with Toast | Cardview | Toast | Android Studio.