I have the following code below, which would be the editing of a form of a request I made here for my work, as I had to change some of the views my update has to be manual, and the select field is not getting the result that I'm bringing it from the db, all fields are working except the select.
as it is now ||| as should be
class EditPedido(View):
def get(self, request, venda):
data = {}
venda = fixa.objects.get(id=venda)
data['filial'] = venda.regional
return render(request, 'fixa/fixa_update.html', data)
<select name="filial" class="select form-control" required="" id="filial">
<option value="" selected="">---------</option>
{% for filial in filiais %}
<option value="{{ filial.id }}">{{ filial.nome }}</option>
{% endfor %}
</select>
EDIT
class EditPedido(View):
def get(self, request, venda):
empresa_logada = request.user.funcionario.empresa
data = {}
data['filiais'] = empresa.objects.filter(nome=empresa_logada)
venda = fixa.objects.get(id=venda)
data['filial'] = venda.regional
return render(request, 'fixa/fixa_update.html', data)
<select name="filial" class="select form-control" required="" id="filial">
<option value="" selected="">---------</option>
{% for filial in filiais %}
<option value="{{ filial.id }}">{{ filial.nome }}</option>
{% endfor %}
</select>
<form method="POST" action="{% url 'create_fixa' %}">
{% csrf_token %}
<h2>number do Pedido: {{ sell.number }}</h2>
<p><font color="RED">{{MSG}}</font></p>
<hr>
<br>
<div class="form-row">
<div class="form-group col-md-3 mb-0">
<label for="number">Nº da Simulação</label>
<input type="text" name="number" class="form-control" value="{{number}}" required="">
</div>
<div class="form-group col-md-9 mb-0">
<label for="razao">Razão Social</label>
<input type="text" name="razao" class="form-control" value="{{razao}}" required="">
</div>
</div>
<br>
<div class="form-row">
<div class="form-group col-md-6 mb-0">
<label for="razao">CNPJ</label>
<input type="text" name="cnpj" class="form-control" data-mask="00.000.000/0000-00" value="{{cnpj}}" required="">
</div>
<div class="form-group col-md-6 mb-0">
<label for="tipo">TIPO</label>
<select name="tp_cli" class="select form-control" required="" id="tp_cli">
<option value="" selected="">---------</option>
<option value="FRESH">FRESH</option>
<option value="BASE">BASE</option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6 mb-0">
<label for="razao">Filial</label>
<select name="filial" class="select form-control" required="" id="id_regional">
{% for filial in filiais %}
<option value="{{filial.id }}">{{ filial.nome }} {% if filial.id == filial %}selected{% endif %}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-6 mb-0">
<label for="tipo">Indicação</label>
<select name="indicacao" class="select form-control" id="indicacao">
<option value="" selected="">---------</option>
{% for indicacao in indicaoes %}
<option value="{{ indicacao.id }}">{{ indicacao.nm_primeiro_nome }} {{indicacao.nm_segundo_nome}}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-6 mb-0">
<label for="tipo">Status</label>
<select name="status" class="select form-control" id="status">
<option value="" selected="">---------</option>
{% for statu in status %}
<option value="{{ statu.id }}">{{ statu.nome_st }}</option>
{% endfor %}
</select>
</div>
</div>
<br>
<input type="hidden" value="{{sell.id}}" name="sell_id">
<button type="submit" class="btn btn-success">Salvar</button>
</form>
class EditOrder(View):
def get(self, request, sell, *args, **kwargs):
company_entered = request.user.employee.company
parent_company = request.user.employee.company.company_pai
data = {}
data['status'] = tb_status.objects.all()
sell = fixa.objects.get(id=sell)
if parent_company:
data['filiais'] = company.objects.filter(nome=company_entered)
data['indicaoes'] = employee.objects.filter(cargo__nome='Vendedor', company__nome=company_entered)
else:
data['filiais'] = company.objects.filter(nome=company_entered) | company.objects.filter(
company_pai=company_entered)
data['indicaoes'] = employee.objects.filter(cargo__nome='Vendedor')
data['number'] = sell.number
data['razao'] = sell.nm_razao
data['cnpj'] = sell.nr_cnpj
data['tp_cli'] = sell.tipo_cli
data['filia'] = sell.regional_id
data['indicacao'] = sell.indicacao
data['sell'] = sell
data['family'] = tb_tp_prod.objects.all()
data['type'] = tb_tipo_servico.objects.all()
data['qtds'] = tb_qtd.objects.all()
data['itens'] = sell.itemdopedido_set.all()
return render(request, 'fixa/fixa_update.html', data)
So this is what you had in your last comment (I don't know where the space before the words in the third and fourth options are coming from, but perhaps you should remove them)
<select name="filial" class="select form-control" required="" id="filial">
<option value="" selected="">--------</option>
<option value="1">TESTE</option>
<option value="2"> SOROCABA</option>
<option value="5"> MARILIA</option>
</select>
You have the 1st option selected, which is --------. I think you want this
<select name="filial" class="select form-control" required="" id="filial">
<option value="1">TESTE</option>
<option value="2"> SOROCABA</option>
<option value="5" selected=""> MARILIA</option>
</select>
You will have to indicate to the template which option to select, you could pass this as a variable something like
data['selection'] = 5
Then in the template
<option value="{{ filial.id }}" {% if filial.id == selection %}selected=""{% endif %}>{{ filial.nome }}</option>
I'm not certain about the syntax of the {% if filial.id == selected %} statement. I don't use it much but the idea is to just put the tag in the option that requires it.
Related
I want to update the leads while doing so
If you look on the url section it says update_lead/1 which is perfectly right but when i hit update lead i am getting extra update lead in the url section due to which django cant reach its function i am very confuse about it please help
here are my codes from update_lead.html
{% extends 'base.html' %}
{% block exhead %}
{% endblock exhead %}
{% block body %}
<div class="container">
<h2>Update Lead</h2>
<form action="update_lead_handle" method="POST">
{% csrf_token %}
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">Name</label>
<input type="text" class="form-control" id="inputEmail4" required name="name" value="{{lead.name}}">
</div>
<div class="form-group col-md-6">
<label for="inputPassword4">Subject</label>
<input type="text" class="form-control" id="inputPassword4" name="subject" required value="{{lead.subject}}">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputAddress">Email</label>
<input type="email" class="form-control" id="inputAddress" name="email" placeholder="abc#email.com" value="{{lead.email}}">
</div>
<div class="form-group col-md-6">
<label for="inputAddress2">Contact Number</label>
<input type="number" class="form-control" id="inputAddress2" name="number"value = "{{lead.mobile_no}}" placeholder="99XX80XXXX">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="inputState">Source</label>
<select id="inputState" class="form-control" name="source" >
<option selected value="{{lead.source}}">{{lead.source}}</option>
{% for x in source %}
<option value="{{x.name}}">{{x.name}}</option>
{% endfor %}
</select>
</div>
<div class="form-group col-md-4">
<label for="inputState">Assign To</label>
<select id="inputState" class="form-control" name="assign">
<option selected value="{{lead.assign_to}}">{{lead.assign_to}}</option>
{% for x in agent %}
<option value="{{x.name}}">{{x.name}}</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label for="inputState">Status</label>
<select data-target="secondList" class="firstList selectFilter form-control" name="state">
<option selected value="{{lead.state}}">{{lead.state}}</option>
<option data-ref="one">Fresh</option>
<option data-ref="two">Open</option>
<option data-ref="pending">Pending</option>
<option data-ref="close">Close</option>
</select>
<!-- <select id="subject" class="form-control" name="source" >
<option selected value="{{lead.state}}">{{lead.state}}</option>
</select> -->
</div>
<div class="form-group col-md-4">
<label for="inputState">If Pending / Close</label>
<select data-target="thirdList" class="secondList selectFilter form-control" name="pending_close">
<option value="-1">Select</option>
<option data-ref="A" data-belong="close">We Cant Do</option>
<option data-ref="A" data-belong="close">Low Budget</option>
<option data-ref="B" data-belong="close">Client Converted</option>
<option data-ref="C" data-belong="pending">Pending With Customer</option>
<option data-ref="D" data-belong="pending">Pending On Us</option>
<option data-ref="E" data-belong="pending">Pending With Process</option>
<!-- <option data-ref="F" data-belong="three">Second Three</option> -->
</select>
<!-- <select id="topic" class="form-control" name="assign">
<option selected value="{{lead.assign_to}}">{{lead.assign_to}}</option>
</select> -->
</div>
</div>
<!-- <div class="form-group">
<label for="exampleFormControlTextarea1">Initial Followup</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" name="followup" value=""></textarea>
</div> -->
<button type="submit" class="btn btn-primary">Update Lead </button>
</form>
</div>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
$(".selectFilter").on("change", function () { var e = $(this).data("target"), i = $(this).find(":selected").data("ref"); $("select." + e).val("-1"), null == i ? $("select." + e).find("option").each(function () { console.log("inside undefined"), $(this).removeAttr("disabled hidden") }) : $("select." + e).find("option").each(function () { var e = $(this).data("belong"), t = $(this).val(); i != e && -1 != t ? ($(this).prop("disabled", !0), $(this).prop("hidden", !0)) : ($(this).prop("disabled", !1), $(this).prop("hidden", !1)) }) });
</script>
{% endblock body %}
here are my codes from urls.py
from django.contrib import admin
from django.urls import path
from home import views
urlpatterns = [
path('',views.index,name="index"),
# create agent
path('create_agent_page',views.create_agent_page,name="create_agent_page"),
path('create_agent',views.create_agent,name="create_agent"),
path('signup_page',views.signup_page,name="signup_page"),
path('login_page',views.login_page,name="login_page"),
path('signup_handle',views.signup_handle,name="signup_handle"),
path('login_handle',views.login_handle,name="login_handle"),
path('logout_handle',views.logout_handle,name="logout_handle"),
#Lead handleing
path('create_lead',views.create_lead_page,name="create_lead"),
path('follow_up/<int:id>',views.follow_up,name="follow_up"),
path('update_lead/<int:id>',views.update_lead,name="update_lead"),
path('update_lead_handle',views.update_lead_handle,name="update_lead_handle"),
# path('update_lead',views.update_lead,name="update_lead"),
path('creat_handle_lead',views.creat_handle_lead,name="creat_handle_lead"),
path('lead_list',views.lead_list,name="lead_list"),
]
here are the two views which are requested
def update_lead(request,id):
leads = Lead.objects.get(id = id)
followup = Followup.objects.all
agent = Agent.objects.all
source = Source.objects.all
print(f"the leads are {leads}")
context = {"lead":leads,"followup":followup,"agent":agent,"source":source}
# context = {"lead":leads,"followup":followup}
return render(request,"home/update_lead.html",context)
def update_lead_handle(request):
if request.method == "POST":
organization=get_object_or_404(Organization,user=User.objects.get(username=request.user.username))
name = request.POST.get('name')
subject = request.POST.get('subject')
email = request.POST.get('email')
subject = request.POST.get('subject')
number = request.POST.get('number')
assign= request.POST.get('assign')
source_o= request.POST.get('source')
followup= request.POST.get('followup')
status= request.POST.get('state')
pending_close= request.POST.get('pending_close')
x = Lead.objects.update(name=name,organ = organization,mobile_no = number,source = source_o,subject = subject,message = followup,email= email,assign_to = assign,state = status,closed_or_pending = pending_close)
x.save()
return HttpResponse("The lead has been updated successfully")
else:
return HttpResponse("Babe you are going wrong ")
Kindly put a slash before the update_lead_handle in your form action let it be "/update_lead_handle"
I'm trying to implement CRUD operations in django, however, I'm stuck in the edit operation.
After passing the data to edit template I need to show the selected values of operating system dropdown list, how can I achieve that?
I utilized the manual rendering of django form with the following code in the template:
<div class="form-group row">
<label class="col-sm-3 col-form-label">Operating System</label>
<div class="col-sm-9">
<select class="form-control" name="os_id" id="os_id" required>
<option value="">--Select--</option>
{% for os in os %}
<option value="{{ os.id}}">{{ os.key_name}}</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-3 col-form-label">Title</label>
<div class="col-sm-9">
<textarea class="form-control" id="title" name="title" rows="3" required>{{ servicedesk.title }}</textarea>
</div>
</div>
here's the view code:
def edit(request, id):
servicedesk=Servicedesk.objects.get(id=id)
softwares=Software.objects.all
os=OperatingSystem.objects.all
context = {'servicedesk':servicedesk, "softwares":softwares, "os":os}
return render(request, 'servicedesks/edit.html', context)
and the model:
class OperatingSystem(models.Model):
key_name = models.CharField(max_length=100,null=True)
key_description = models.CharField(max_length=255,null=True)
class Meta:
db_table = "operating_systems"
def __str__(self):
return self.key_name
class Software(models.Model):
key_name = models.CharField(max_length=100,null=True)
key_description = models.CharField(max_length=255,null=True)
class Meta:
db_table = "softwares"
def __str__(self):
return self.key_name
class Servicedesk(models.Model):
os_id=models.ForeignKey(OperatingSystem, on_delete=models.SET(0))
software_id = models.ForeignKey(Software, on_delete=models.SET(0))
title = models.CharField(max_length=255,null=True)
I tried this but it's not working:
<div class="form-group row">
<label class="col-sm-3 col-form-label">Operating System {{os_id}}</label>
<div class="col-sm-9">
<select class="form-control" name="os_id" id="os_id" required>
<option value="">--Select--</option>
{% for os in os %}
{% if os.id == servicedesk.os_id %}
<option value="{{os.id}}" selected>{{os.key_name}}</option>
{% endif %}
<option value="{{ os.id}}">{{ os.key_name}}</option>
{% endfor %}
</select>
</div>
</div>
I have a drop down and it is populated through my models. I am able to select one and then push submit. But the data that I am getting is being broke by spaces in the name. So if I have an option in my drop down menu such as:
Please Pick Me
I will only get
Please
template.html
<form action="{% url 'parsed' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="sel1">Select Test:</label>
<select class="form-control" name="selectedtest" id="sel1">
{% for test in test %}
<option value={{ test.name }}>{{ test.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label>Paste Event JSON</label>
<textarea class="form-control" name="jsontextarea" rows="20"></textarea>
<div style="text-align:center">
</br>
<input class="btn btn-primary" type="submit" value="Parse">
</div>
</div>
</form>
views.py
def parsed(request):
data = request.POST.get('jsontextarea')
testname = request.POST.get('selectedtest')
print(testname)
context = {
"json" : data,
"test" : Test.objects.all(),
"event" : Event.objects.all(),
"platform" : Platform.objects.all(),
"device" : Device.objects.all(),
"property" : Property.objects.all(),
"testname" : testname
}
return render(request, 'jsonparser/parsed.html', context)
Try replacing
<option value={{ test.name }}>{{ test.name }}</option>
with
<option value="{{ test.name }}">{{ test.name }}</option> (Notice the double quotes)
My guess is that your HTML becomes - <option value=Please Pick Me>Please pick me</option> and in this case the value of the option is "Please". If you use double quotes however, it would become <option value="Please Pick Me">Please pick me</option> which is what you want
I am having problem to get the selected data from a form. Here is my form
<form action="#" method="GET">
{% csrf_token %}
<select name="country" id="selectcountries" class="custom-select">
<option>Select country</option>
{% for item in countries %}
<option val="{{ item.name }}"> {{ item.name }} </option>
{% endfor %}
</select>
<select name ="city" id="selectcities" class="custom-select">
<option>Select city</option>
</select>
<select class="custom-select" name="option" >
<option selected> Tourist Spot </option>
<option> Hotel </option>
<option> Restaurent </option>
</select>
<button type="submit" class="btn tour-btn"><i class="fa fa-search pr-2" aria-hidden="true"></i> Search </button>
</form>
And my views.py is
def advanceSearch(request):
country = request.GET.get('country')
city = request.GET.get('city')
option = request.GET.get('option')
if request.method == "GET" :
if country:
message = 'q= %s' % country
else:
message = 'Empty'
else:
message = 'oops'
return HttpResponse(message)
HTTPResponse always give me empty message even after with passing values by the form. I want to get the data from this form but i cant.
I tried to replicate the scenario with the provided code, and I think your search view is not getting executed. You have provided {% url 'advanceSearch' %} in the anchor tag inside button. It should be in the action attribute of the form.
<form action="{% url 'advanceSearch' %}" method="GET">
{% csrf_token %}
<select name="country" id="selectcountries" class="custom-select">
<option>Select country</option>
{% for item in countries %}
<option val="{{ item.name }}"> {{ item.name }} </option>
{% endfor %}
</select>
<select name ="city" id="selectcities" class="custom-select">
<option>Select city</option>
</select>
<select class="custom-select" name="option" >
<option selected> Tourist Spot </option>
<option> Hotel </option>
<option> Restaurent </option>
</select>
<button type="submit" class="btn tour-btn"><i class="fa fa-search pr-2" aria-hidden="true"></i>Search</button>
</form>
Building an Employee application, After updating an Employee using a form, it just shows this error and the Url seems correct so I can really say where the error is coming from
I've crosschecked all my url patterns my views and my url in the form and also tried using the solution in this question, this gives me a bigger error
urls.py
urlpatterns = [
path('edit/<int:pk>/', views.edit, name = 'edit'),
]
views.py
#login_required(login_url='/accounts/login')
def edit(request, pk):
employ = get_object_or_404(Employee, id=pk)
logging.info(type(employ))
departments = Department.objects.all()
context = {
'employ': employ,
'departments':departments
}
if request.method == "POST":
first_name = request.POST['first_name']
last_name = request.POST['last_name']
name = last_name +' '+first_name
employee_id = request.POST['employee_id']
email = request.POST['email']
department = Department.objects.get(dept_name = request.POST['department'])
address = request.POST['address']
employment_type = request.POST['employment_type']
employment_status = request.POST['employment_status']
role = request.POST['role']
marital_status = request.POST['marital_status']
gender = request.POST['gender']
join_date = request.POST['join_date']
end_date = None if len(request.POST['end_date']) ==0 else request.POST['end_date']
location = request.POST['location']
credentials = request.POST['credentials']
passport = request.POST['passport']
hod = request.POST['hod']
phone_number = request.POST['phone_number']
date_of_birth = request.POST['date_of_birth']
date_added = datetime.now()
if Employee.objects.filter(employee_id = employee_id).exists() or Employee.objects.filter(email = email).exists():
messages.error(request, 'That ID/Email is Taken')
return redirect('edit')
else:
employee = Employee(first_name='first_name',last_name='last_name',email='email',
employee_id='employee_id',department='department',address='address',employment_type='employment_type',
employment_status='employment_status',role='role',marital_status='marital_status',gender='gender',join_date='join_date',
end_date='end_date',location='location',credentials='credentials',passport='passport',hod='hod',
phone_number='phone_number',date_added='date_added',date_of_birth='date_of_birth')
employee.save()
messages.success(request, 'Employee Created')
return redirect('all')
return render(request, 'employees/edit.html', context, employ)
edit.html
<form method = 'POST' action="{% url 'edit' pk=employ.pk %}" class="m-b-30" enctype="multipart/form-data">
{% csrf_token %}
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label for='first_name' class="control-label">First Name <span class="text-danger">*</span></label>
<input name='first_name' class="form-control" type="text" required value="{{employ.first_name}}">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='last_name' class="control-label">Last Name</label>
<input name='last_name' class="form-control" type="text" required value="{{employ.last_name }}">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='employee_id' class="control-label">Employee ID <span class="text-danger">*</span></label>
<input name='employee_id' class="form-control" type="number" required value="{{employ.employee_id }}">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='email' class="control-label">Email <span class="text-danger">*</span></label>
<input name='email' class="form-control" type="email" required value="{{employ.email }}">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='phone_number' class="control-label">Phone Number</label>
<input name='phone_number' class="form-control" type="text" required value="{{employ.phone_number }}">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='gender' class="control-label">Gender</label>
<select name = 'gender' class="form-control" value = "{{employ.gender}} " type="text" >
<option>select gender</option>
{% if employ.gender == 'Male' %}
<option selected ='selected'>Male</option>
{% else %}
<option selected ='selected'>Female</option>
{% endif %}
<option>Male</option>
<option>Female</option>
</select>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='marital_status' class="control-label">Marital Status</label>
<select name='marital_status' class="form-control" value = "{{employ.marital_status}} " type="text" >
{% if employ.marital_status == 'Married' %}
<option selected ='selected'>Married</option>
{% else %}
<option selected ='selected'>Single</option>
{% endif %}
<option>select martial status</option>
<option>Married</option>
<option>Single</option>
<option>Single</option>
</select>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='date_of_birth' class="control-label">Date of Birth <span class="text-danger">*</span></label>
<input name='date_of_birth' class="form-control" type="date" required value="{{employ.date_of_birth|date:'Y-m-d' }}">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='address' class="control-label">Address </label>
<input name='address' class="form-control" type="text" required value="{{employ.address }}">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='department' class="control-label">Department <span class="text-danger">*</span></label>
<select name = 'department' type="text" class="form-control" value = "{{employ.department}} " >
<option>select department</option>
{% for department in departments %}
{% if employ.department == department %}
<option selected ='selected'>{{employ.department}}</option>
{% endif %}
<option>{{department.dept_name}}</option>
{% endfor %}
</select>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='employment_type' class="control-label">Employment Type </label>
<select id= "employment_type" name='employment_type' type="text" class="form-control" value = "{{employ.employment_type}} " >
{% if employ.employment_type == 'Contract' %}
<option selected ='selected'>Contract</option>
{% elif employ.employment_type == 'Fulltime' %}
<option selected ='selected'>Fulltime</option>
{% else %}
<option selected ='selected'>Intern</option>
{% endif %}
<option>select employment type</option>
<option>Contract</option>
<option>Fulltime</option>
<option>Intern</option>
</select>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='join_date' class="control-label">Join Date <span class="text-danger">*</span></label>
<input name='join_date' class="form-control " type="date" required value="{{employ.join_date|date:'Y-m-d' }}">
</div>
</div>
<div class="col-sm-6" id="end_date">
<div class="form-group">
<label for='end_date' class="control-label">End Date <span class="text-danger">*</span></label>
<input name='end_date' class="form-control " type="date" value="{{employ.end_date|date:'Y-m-d' }}">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='employment_status' class="control-label">Employment Status </label>
<select name='employment_status' type="text" class="form-control" value="{{employ.employment_status}}" >
{% if employ.employment_status == 'Active' %}
<option selected ='selected'>Active</option>
{% else %}
<option selected ='selected'>Inactive</option>
{% endif %}
<option>select employment status</option>
<option>Active</option>
<option>Inactive</option>
</select>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='role' class="control-label">Role </label>
<input name='role' class="form-control" type="text" required value="{{employ.role }}">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='location' class="control-label">Work Location </label>
<input name='location' class="form-control" type="text" required value="{{employ.location }}">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='hod' class="control-label">HOD </label>
<select name='hod' class="form-control" type="text" >
<option>Select HOD</option>
{% for department in departments %}
{% if employ.department.lead == department.lead %}
<option selected ='selected'>{{employ.department.lead}}</option>
{% endif %}
<option>{{department.lead}}</option>
{% endfor %}
</select>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='passport' class="control-label">Upload passport </label>
<input name='passport' type="file" accept="image/*" value="{{employ.passport }}">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for='credentials' class="control-label">Add Credentials </label>
<input name='credentials' type="file" accept="image/*" value="{{employ.credentials }}">
</div>
</div>
</div>
<div class="m-t-20 text-center">
<button class="btn btn-primary">Update Employee</button>
</div>
</form>
it's meant to be submitted and routed to edit.hmtl in case there's any error while submitting
When you redirect to the edit view, you need to pass the primary key.
return redirect('edit', pk=employ.pk)
As an aside, it's not recommended to access values from request.POST directly. Have a look at Django forms.