Hi all! I have model Employee and same table in my local database. I need to have the possibility to edit any record and save it locally. When I tried to edit record with actual id I got this error: invalid literal for int() with base 10: ‘undefined’ . I made a research but can’t really find something helpful in my case. I have troubles with this from a few days so thanks a lot I hope my question is clear hope someone can help
my edit function in views.py:
def staff_edit(request, id=0):
#employees = Employee.objects.all()
#print(employees)
if request.method == 'GET':
if id == 0:
form = EmployeeEditForm()
else:
employees = Employee.objects.get(pk=id)
form = EmployeeEditForm(instance=employees)
return render(request, 'staffedit.html', {'form': form})
else:
if id == 0:
form = EmployeeEditForm(request.POST)
else:
employees = Employee.objects.get(pk=id)
form = EmployeeEditForm(request.POST, instance=employees)
if form.is_valid():
form.save()
return redirect('feedback:index_employees')
context = {'form': form} #when the form is invalid
return render(request, 'staffedit.html', context)
this is my model.py for Employee:
class Employee(models.Model):
coreapiemployee_id = models.CharField(max_length=100)
webflow_id_employee = models.CharField(max_length=100, default=True)
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100, default=True)
email = models.EmailField(max_length=100)
user_type = models.CharField(max_length=100)
status = models.CharField(max_length=100, default=True)
roles = models.ManyToManyField('Role', through='EmployeeRole')
def __str__(self):
return self.coreapiemployee_id + " " + self.email + self.first_name + self.last_name
this is my general html staff.html:
$(document).ready(function() {
var data;
fetch("http://192.168.2.85:8000/displayEmployeesToFroentend/")
.then(response => response.json())
.then(json => data = json)
.then(() => {console.log(data);
let employees = JSON.parse(data.employees_json);
var table = $('#datatable').DataTable( {
data: employees,
select: "single",
"columns": [
{ "data": "fields.coreapiemployee_id"},
{ "data": "pk"},
{ "data": "fields.first_name" },
{ "data": "fields.last_name" },
{ "data": "fields.email" },
{ render: function ( data, type, row ) {
return '<a href="http://192.168.2.85:8000/staff/edit/' + row.coreapiemployee_id + '"><i class="far fa-edit fa-lg" aria-hidden="true"></i></a>';
} },
{ render: function ( data, type, row ) {
return '<i class="fa fa-plus-circle" aria-hidden="true"></i>';
} },
],
"order": [[1, 'asc']]
} )
})
} );
pk is my primary key and that’s how I want to edit this records, via this pk
staffedit.html:
<div class="container">
<div class="col-md-10.offset-md-1 mt-5">
<div class="jumbotron">
<h1 class="display-4">Edit Employee</h1>
<hr class="my-4">
<form action="" method="post" autocomplete="off">
{% csrf_token %}
{{form.as_p}}
<button type="submit" class="btn btn-success"><i class="far fa-save"></i> Save</button>
</form>
</div>
</div>
</div>
my forms.py for EmployeeEditForm:
class EmployeeEditForm(ModelForm):
class Meta:
model = Employee
fields = [
'coreapiemployee_id',
'first_name',
'last_name',
'roles',
urls.py:
path('staff/edit/<str:id>', views.staff_edit, name="staffedit"),
Kind regards,
Stela