Tutorial 5: sql command(delete)

view.py
=====



############### standard library files##########
from django.shortcuts import *
from django.http import *


########### support files/library/etc.... of app #################
from dmlapp.forms import EmployeeForm
from dmlapp.models import employee


################## insert query in django.######################

def insert(request):
    ename=request.POST.get('ename')    
################## this will give you the  textbox #################
    if request.method == 'GET':
        form = EmployeeForm()
    else:
         form = EmployeeForm(request.POST)
################ insert query in django    ##############################
         if form.is_valid():
             a = employee(ename=ename)
             a.save()
             return redirect('/insert/')
    return render(request, 'insert.html', {'form': form})


######################## end of insert query ######################

################# select query in django####################

def select(request):
    emp=employee.objects.all()
    return render(request,'select.html',{'emp':emp})



################# end of select query ########################


def index(request):
    return render(request,'index.html')



def delete(request):
    empname=employee.objects.all()
    ename = request.POST.get('dropdown1')
    if request.method == 'GET':
        form = EmployeeForm()
    else:
        empname = employee.objects.get(ename = ename)
        empname.delete()
        return redirect('/delete/')
    return render(request, 'delete.html', {'form': form, 'empname':empname})



def update(request):
    return render(request,'update.html')




delete.html
=====

{% block content %}


    <h2>Delete User</h1>
   <form action="{% url 'delete' %}" method="POST" >{% csrf_token %}
    <table>
    <tr><td>employee Name:</td>
        <td>
            <select name="dropdown1">
                {% for empname in empname %}
                     <option value="{{ empname.ename }}">{{ empname.ename }}</option>
                {% endfor %}
            </select>
        </td>
    <td><input type="submit" value="Delete employee" /></td></tr>
    </table>

{% endblock %}





o/p
====

Delete User

employee Name:

No comments:

Post a Comment