Tutorial 6: sql command(update)

views.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):
    empname=employee.objects.all()
    ename1 = request.POST.get('dropdown1')
    ename2 = request.POST.get('emp_update')

    if request.method == 'GET':
        form = EmployeeForm()
    else:
        empname = employee.objects.filter(id = ename1).update(ename = ename2)

        return redirect('/update/')
    return render(request, 'update.html', {'form': form, 'empname':empname})



update.html
=======


{% block content %}


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

        </td>
    <td>update the emp name<input type="text" name="emp_update" placeholder="enter the value to update" /></td>    <td><input type="submit" value="update employee name" /></td></tr>
    </table>

{% endblock %}


No comments:

Post a Comment