This commit is contained in:
2025-04-22 13:40:09 +02:00
parent 63dffd1d80
commit 63556a1ac3
14 changed files with 221 additions and 344 deletions

Binary file not shown.

View File

@ -10,7 +10,7 @@ Function views
Class-based views Class-based views
1. Add an import: from other_app.views import Home 1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconftesting Including another URLconftesting test
1. Import the include() function: from django.urls import include, path 1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
@ -19,5 +19,5 @@ from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), # Admin site path('admin/', admin.site.urls), # Admin site
path('', include('Sheets.urls')), # Sheets app URLs path('', include('sheets.urls')), # Sheets app URLs
] ]

View File

@ -1,5 +1,6 @@
# Generated by Django 5.1.3 on 2024-12-04 13:22 # Generated by Django 5.1.5 on 2025-04-15 10:13
import django.db.models.deletion
from django.db import migrations, models from django.db import migrations, models
@ -12,10 +13,31 @@ class Migration(migrations.Migration):
operations = [ operations = [
migrations.CreateModel( migrations.CreateModel(
name='Sheet', name='Client',
fields=[ fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)), ('name', models.CharField(max_length=100)),
('address', models.TextField()),
],
),
migrations.CreateModel(
name='ExcelEntry',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('age', models.IntegerField()),
('email', models.EmailField(max_length=254)),
('date_joined', models.DateField(auto_now_add=True)),
('client', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='sheets.client')),
],
),
migrations.CreateModel(
name='SecondTableEntry',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('age', models.IntegerField()),
('email', models.EmailField(max_length=254)),
('date_joined', models.DateField(auto_now_add=True)),
('client', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='sheets.client')),
], ],
), ),
] ]

View File

@ -1,46 +0,0 @@
# Generated by Django 5.1.3 on 2024-12-04 14:19
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sheets', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='MainColumn',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('sheet', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='main_columns', to='sheets.sheet')),
],
),
migrations.CreateModel(
name='Row',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('sheet', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rows', to='sheets.sheet')),
],
),
migrations.CreateModel(
name='SubColumn',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('main_column', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='sub_columns', to='sheets.maincolumn')),
],
),
migrations.CreateModel(
name='Cell',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('value', models.CharField(blank=True, max_length=255, null=True)),
('row', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='cells', to='sheets.row')),
('sub_column', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='cells', to='sheets.subcolumn')),
],
),
]

View File

@ -1,50 +0,0 @@
# Generated by Django 5.1.4 on 2024-12-10 07:27
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sheets', '0002_maincolumn_row_subcolumn_cell'),
]
operations = [
migrations.RemoveField(
model_name='maincolumn',
name='sheet',
),
migrations.RemoveField(
model_name='subcolumn',
name='main_column',
),
migrations.RemoveField(
model_name='row',
name='sheet',
),
migrations.RenameModel(
old_name='Sheet',
new_name='Parent',
),
migrations.CreateModel(
name='Child',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('description', models.CharField(max_length=200)),
('parent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='children', to='sheets.parent')),
],
),
migrations.DeleteModel(
name='Cell',
),
migrations.DeleteModel(
name='MainColumn',
),
migrations.DeleteModel(
name='SubColumn',
),
migrations.DeleteModel(
name='Row',
),
]

View File

@ -1,29 +0,0 @@
# Generated by Django 5.1.5 on 2025-01-22 13:05
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sheets', '0003_remove_maincolumn_sheet_remove_subcolumn_main_column_and_more'),
]
operations = [
migrations.CreateModel(
name='ExcelEntry',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('age', models.IntegerField()),
('email', models.EmailField(max_length=254)),
('date_joined', models.DateField(auto_now_add=True)),
],
),
migrations.DeleteModel(
name='Child',
),
migrations.DeleteModel(
name='Parent',
),
]

View File

@ -1,23 +0,0 @@
# Generated by Django 5.1.5 on 2025-02-17 10:24
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sheets', '0004_excelentry_delete_child_delete_parent'),
]
operations = [
migrations.CreateModel(
name='SecondTableEntry',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('age', models.IntegerField()),
('email', models.EmailField(max_length=254)),
('date_joined', models.DateField(auto_now_add=True)),
],
),
]

View File

@ -1,21 +0,0 @@
# Generated by Django 5.1.5 on 2025-04-08 11:23
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('sheets', '0005_secondtableentry'),
]
operations = [
migrations.CreateModel(
name='Client',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('address', models.TextField()),
],
),
]

View File

@ -8,19 +8,13 @@ class Client(models.Model):
return self.name return self.name
class ExcelEntry(models.Model): class ExcelEntry(models.Model):
name = models.CharField(max_length=100) client = models.ForeignKey(Client, on_delete=models.CASCADE)
age = models.IntegerField()
email = models.EmailField()
date_joined = models.DateField(auto_now_add=True)
def __str__(self):
return self.name
class SecondTableEntry(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField() age = models.IntegerField()
email = models.EmailField() email = models.EmailField()
date_joined = models.DateField(auto_now_add=True) date_joined = models.DateField(auto_now_add=True)
def __str__(self): class SecondTableEntry(models.Model):
return self.name client = models.ForeignKey(Client, on_delete=models.CASCADE)
age = models.IntegerField()
email = models.EmailField()
date_joined = models.DateField(auto_now_add=True)

View File

@ -109,7 +109,6 @@
</head> </head>
<body> <body>
<a href="/" class="btn-go-back">&#8592; Back to Main</a>
<div class="container"> <div class="container">
<h2>Clients Table</h2> <h2>Clients Table</h2>
@ -162,10 +161,10 @@
<div style="margin-top: 30px; text-align: center;"> <div style="margin-top: 30px; text-align: center;">
<a href="{% url 'table_one' %}"> <a href="{% url 'table_one' %}">
<button class="add-row-btn">Go to Table One</button> <button class="add-row-btn">Go to Helium input</button>
</a> </a>
<a href="{% url 'table_two' %}"> <a href="{% url 'table_two' %}">
<button class="add-row-btn">Go to Table Two</button> <button class="add-row-btn">Go to Table output</button>
</a> </a>
</div> </div>
@ -189,20 +188,18 @@
let address = $('#add-address').val(); let address = $('#add-address').val();
$.ajax({ $.ajax({
url: `/add-entry/${currentModelName}/`, url: `/add-entry/Client/`,
method: 'POST', method: 'POST',
data: { data: {
'name': name, 'name': name,
'address': address, 'address': address,
'age': 0,
'email': 'none@example.com',
'csrfmiddlewaretoken': '{{ csrf_token }}' 'csrfmiddlewaretoken': '{{ csrf_token }}'
}, },
success: function (response) { success: function (response) {
let rowCount = $('tbody tr').length + 1; let rowCount = $('tbody tr').length + 1;
let newRow = ` let newRow = `
<tr data-id="${response.id}"> <tr data-id="${response.id}">
<td>${rowCount}</td> <!-- Serial number --> <td>${rowCount}</td>
<td>${response.id}</td> <td>${response.id}</td>
<td>${response.name}</td> <td>${response.name}</td>
<td>${response.address}</td> <td>${response.address}</td>
@ -214,6 +211,9 @@
`; `;
$('tbody').append(newRow); $('tbody').append(newRow);
$('#add-popup').fadeOut(); $('#add-popup').fadeOut();
},
error: function (xhr) {
alert('Failed to add client: ' + xhr.responseText);
} }
}); });
}); });
@ -272,7 +272,14 @@
success: function (response) { success: function (response) {
if (response.status === 'success') { if (response.status === 'success') {
row.fadeOut(300, function () { $(this).remove(); }); row.fadeOut(300, function () { $(this).remove(); });
} else {
alert('Delete failed: ' + response.message);
} }
},
error: function (xhr, status, error) {
alert('Delete request failed:\n' + xhr.responseText);
console.log('Error:', error);
console.log('Status:', status);
} }
}); });
}); });

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Excel-like Table</title> <title>Helium Input</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />
@ -14,11 +14,6 @@
padding: 20px; padding: 20px;
background-color: #f4f4f9; background-color: #f4f4f9;
} }
.container {
display: flex;
justify-content: space-between;
gap: 20px;
}
.table-container { .table-container {
width: 48%; width: 48%;
background-color: white; background-color: white;
@ -56,11 +51,11 @@
cursor: pointer; cursor: pointer;
font-size: 14px; font-size: 14px;
} }
.edit-btn { .edit-btn-one {
background-color: #28a745; background-color: #28a745;
color: white; color: white;
} }
.delete-btn { .delete-btn-one {
background-color: #dc3545; background-color: #dc3545;
color: white; color: white;
} }
@ -76,7 +71,7 @@
box-shadow: 0 0 10px rgba(0, 0, 0, 0.3); box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
z-index: 1000; z-index: 1000;
} }
.popup input { .popup input, .popup select {
display: block; display: block;
margin-bottom: 10px; margin-bottom: 10px;
width: 100%; width: 100%;
@ -115,92 +110,90 @@
</head> </head>
<body> <body>
<div class="d-flex justify-content-start mb-2"> <a href="{% url 'clients_list' %}" class="btn btn-outline-primary">
<!-- "Go to Clients" button at top-left --> &#8678; Go to Clients
<a href="{% url 'clients_list' %}" class="btn btn-outline-primary"> </a>
&#8678; Go to Clients
</a>
</div>
<h2>Excel-like Table</h2> <h2>Helium Input</h2>
<div class="table-container">
<button class="add-row-btn" id="add-row-one">Add Row</button>
<div class="container"> <table id="table-one">
<!-- First Table --> <thead>
<div class="table-container"> <tr>
<button class="add-row-btn" id="add-row-1">Add Row</button> <th>#</th>
<table id="table-1"> <th>ID</th>
<thead> <th>Client</th>
<tr> <th>Entry 1</th>
<th>#</th> <th>Entry 2</th>
<th>ID</th> <th>Date Joined</th>
<th>Name</th> <th>Actions</th>
<th>Age</th> </tr>
<th>Email</th> </thead>
<th>Date Joined</th> <tbody>
<th>Actions</th> {% for entry in entries_table1 %}
<tr data-id="{{ entry.id }}">
<td>{{ forloop.counter }}</td>
<td>{{ entry.id }}</td>
<td>{{ entry.client.name }}</td>
<td>{{ entry.age }}</td>
<td>{{ entry.email }}</td>
<td>{{ entry.date_joined }}</td>
<td class="actions">
<button class="edit-btn-one">Edit</button>
<button class="delete-btn-one">Delete</button>
</td>
</tr> </tr>
</thead> {% endfor %}
<tbody> </tbody>
{% for entry in entries_table1 %} </table>
<tr data-id="{{ entry.id }}">
<td>{{ forloop.counter }}</td> <!-- ← sequential number -->
<td>{{ entry.id }}</td>
<td>{{ entry.name }}</td>
<td>{{ entry.age }}</td>
<td>{{ entry.email }}</td>
<td>{{ entry.date_joined }}</td>
<td class="actions">
<button class="edit-btn">Edit</button>
<button class="delete-btn">Delete</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div> </div>
<!-- Add Entry Popup -->
<div id="add-popup" class="popup"> <!-- Add Popup -->
<div id="add-popup-one" class="popup">
<span class="close-btn">&times;</span> <span class="close-btn">&times;</span>
<h3>Add Entry</h3> <h3>Add Entry</h3>
<input type="text" id="add-name" placeholder="Name"> <select id="add-client-id">
{% for client in clients %}
<option value="{{ client.id }}">{{ client.name }}</option>
{% endfor %}
</select>
<input type="number" id="add-age" placeholder="Age"> <input type="number" id="add-age" placeholder="Age">
<input type="email" id="add-email" placeholder="Email"> <input type="email" id="add-email" placeholder="Email">
<button id="save-add">Add</button> <button id="save-add-one">Add</button>
</div> </div>
<!-- Edit Entry Popup --> <!-- Edit Popup -->
<div id="edit-popup" class="popup"> <div id="edit-popup-one" class="popup">
<span class="close-btn">&times;</span> <span class="close-btn">&times;</span>
<h3>Edit Entry</h3> <h3>Edit Entry</h3>
<input type="hidden" id="edit-id"> <input type="hidden" id="edit-id">
<input type="text" id="edit-name" placeholder="Name"> <select id="edit-client-id">
{% for client in clients %}
<option value="{{ client.id }}">{{ client.name }}</option>
{% endfor %}
</select>
<input type="number" id="edit-age" placeholder="Age"> <input type="number" id="edit-age" placeholder="Age">
<input type="email" id="edit-email" placeholder="Email"> <input type="email" id="edit-email" placeholder="Email">
<button id="save-edit">Save</button> <button id="save-edit-one">Save</button>
</div> </div>
<script> <script>
$(document).ready(function () { $(document).ready(function () {
let currentTableId = null; // To track which table is being edited let currentTableId = 'table-one';
let currentModelName = null; // To track which model is being used let currentModelName = 'ExcelEntry';
// Open Add Popup for Table 1 $('#add-row-one').on('click', function () {
$('#add-row-1').on('click', function () { $('#add-popup-one').fadeIn();
currentTableId = 'table-1';
currentModelName = 'ExcelEntry'; // Model name for Table 1
$('#add-popup').fadeIn();
}); });
// Close Popups
$('.close-btn').on('click', function () { $('.close-btn').on('click', function () {
$('.popup').fadeOut(); $('.popup').fadeOut();
}); });
// Save Add Entry // Add
$('#save-add').on('click', function () { $('#save-add-one').on('click', function () {
let name = $('#add-name').val(); let client_id = $('#add-client-id').val();
let age = $('#add-age').val(); let age = $('#add-age').val();
let email = $('#add-email').val(); let email = $('#add-email').val();
@ -208,7 +201,7 @@
url: `/add-entry/${currentModelName}/`, url: `/add-entry/${currentModelName}/`,
method: 'POST', method: 'POST',
data: { data: {
'name': name, 'client_id': client_id,
'age': age, 'age': age,
'email': email, 'email': email,
'csrfmiddlewaretoken': '{{ csrf_token }}' 'csrfmiddlewaretoken': '{{ csrf_token }}'
@ -217,40 +210,37 @@
let rowCount = $(`#${currentTableId} tbody tr`).length + 1; let rowCount = $(`#${currentTableId} tbody tr`).length + 1;
let newRow = ` let newRow = `
<tr data-id="${response.id}"> <tr data-id="${response.id}">
<td>${rowCount}</td> <!-- serial number --> <td>${rowCount}</td>
<td>${response.id}</td> <td>${response.id}</td>
<td>${response.name}</td> <td>${response.client_name}</td>
<td>${response.age}</td> <td>${response.age}</td>
<td>${response.email}</td> <td>${response.email}</td>
<td>${response.date_joined}</td> <td>${response.date_joined}</td>
<td class="actions"> <td class="actions">
<button class="edit-btn">Edit</button> <button class="edit-btn-one">Edit</button>
<button class="delete-btn">Delete</button> <button class="delete-btn-one">Delete</button>
</td> </td>
</tr> </tr>
`; `;
$(`#${currentTableId} tbody`).append(newRow); $(`#${currentTableId} tbody`).append(newRow);
$('#add-popup').fadeOut(); $('#add-popup-one').fadeOut();
} }
}); });
}); });
// Open Edit Popup // Edit
$(document).on('click', '.edit-btn', function () { $(document).on('click', '.edit-btn-one', function () {
let row = $(this).closest('tr'); let row = $(this).closest('tr');
currentTableId = row.closest('table').attr('id'); // Set current table ID
currentModelName = currentTableId === 'table-1' ? 'ExcelEntry' : 'SecondTableEntry'; // Set model name
$('#edit-id').val(row.data('id')); $('#edit-id').val(row.data('id'));
$('#edit-name').val(row.find('td:eq(2)').text()); // Name is now in column 2 $('#edit-client-id').val(row.find('td:eq(2)').text());
$('#edit-age').val(row.find('td:eq(3)').text()); // Age is now in column 3 $('#edit-age').val(row.find('td:eq(3)').text());
$('#edit-email').val(row.find('td:eq(4)').text()); // Email is now in column 4 $('#edit-email').val(row.find('td:eq(4)').text());
$('#edit-popup').fadeIn(); $('#edit-popup-one').fadeIn();
}); });
// Save Edit Entry $('#save-edit-one').on('click', function () {
$('#save-edit').on('click', function () {
let id = $('#edit-id').val(); let id = $('#edit-id').val();
let name = $('#edit-name').val(); let client_id = $('#edit-client-id').val();
let age = $('#edit-age').val(); let age = $('#edit-age').val();
let email = $('#edit-email').val(); let email = $('#edit-email').val();
@ -259,7 +249,7 @@
method: 'POST', method: 'POST',
data: { data: {
'id': id, 'id': id,
'name': name, 'client_id': client_id,
'age': age, 'age': age,
'email': email, 'email': email,
'csrfmiddlewaretoken': '{{ csrf_token }}' 'csrfmiddlewaretoken': '{{ csrf_token }}'
@ -270,7 +260,7 @@
row.find('td:eq(2)').text(response.name); row.find('td:eq(2)').text(response.name);
row.find('td:eq(3)').text(response.age); row.find('td:eq(3)').text(response.age);
row.find('td:eq(4)').text(response.email); row.find('td:eq(4)').text(response.email);
$('#edit-popup').fadeOut(); $('#edit-popup-one').fadeOut();
} else { } else {
alert('Failed to update entry: ' + response.message); alert('Failed to update entry: ' + response.message);
} }
@ -281,12 +271,10 @@
}); });
}); });
// Delete Entry // Delete
$(document).on('click', '.delete-btn', function () { $(document).on('click', '.delete-btn-one', function () {
let row = $(this).closest('tr'); let row = $(this).closest('tr');
let id = row.data('id'); let id = row.data('id');
currentTableId = row.closest('table').attr('id'); // Set current table ID
currentModelName = currentTableId === 'table-1' ? 'ExcelEntry' : 'SecondTableEntry'; // Set model name
if (!confirm('Are you sure you want to delete this entry?')) return; if (!confirm('Are you sure you want to delete this entry?')) return;
@ -313,4 +301,4 @@
</script> </script>
</body> </body>
</html> </html>

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Excel-like Table</title> <title>Helium Output</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-modal/0.9.1/jquery.modal.min.css" />
@ -56,11 +56,11 @@
cursor: pointer; cursor: pointer;
font-size: 14px; font-size: 14px;
} }
.edit-btn { .edit-btn-two {
background-color: #28a745; background-color: #28a745;
color: white; color: white;
} }
.delete-btn { .delete-btn-two {
background-color: #dc3545; background-color: #dc3545;
color: white; color: white;
} }
@ -111,6 +111,15 @@
.add-row-btn:hover { .add-row-btn:hover {
background-color: #0056b3; background-color: #0056b3;
} }
.popup select {
display: block;
margin-bottom: 10px;
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
font-family: Arial, sans-serif;
}
</style> </style>
</head> </head>
<body> <body>
@ -122,22 +131,22 @@
</a> </a>
</div> </div>
<h2>Excel-like Table</h2> <h2>Helium Output</h2>
<!-- Second Table --> <!-- Second Table -->
<div class="table-container"> <div class="table-container">
<button class="add-row-btn" id="add-row-2">Add Row</button> <button class="add-row-btn" id="add-row-two">Add Row</button>
<table id="table-2"> <table id="table-two">
<thead> <thead>
<tr> <tr>
<th>#</th> <th>#</th>
<th>ID</th> <th>ID</th>
<th>Name</th> <th>Client</th>
<th>Age</th> <th>Entry 1</th>
<th>Email</th> <th>Entry 2</th>
<th>Date Joined</th> <th>Date Joined</th>
<th>Actions</th> <th>Actions</th>
</tr> </tr>
@ -147,13 +156,13 @@
<tr data-id="{{ entry.id }}"> <tr data-id="{{ entry.id }}">
<td>{{ forloop.counter }}</td> <td>{{ forloop.counter }}</td>
<td>{{ entry.id }}</td> <td>{{ entry.id }}</td>
<td>{{ entry.name }}</td> <td>{{ entry.client.name }}</td>
<td>{{ entry.age }}</td> <td>{{ entry.age }}</td>
<td>{{ entry.email }}</td> <td>{{ entry.email }}</td>
<td>{{ entry.date_joined }}</td> <td>{{ entry.date_joined }}</td>
<td class="actions"> <td class="actions">
<button class="edit-btn">Edit</button> <button class="edit-btn-two">Edit</button>
<button class="delete-btn">Delete</button> <button class="delete-btn-two">Delete</button>
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
@ -163,24 +172,32 @@
</div> </div>
<!-- Add Entry Popup --> <!-- Add Entry Popup -->
<div id="add-popup" class="popup"> <div id="add-popup-two" class="popup">
<span class="close-btn">&times;</span> <span class="close-btn">&times;</span>
<h3>Add Entry</h3> <h3>Add Entry</h3>
<input type="text" id="add-name" placeholder="Name"> <select id="add-client-id">
{% for client in clients %}
<option value="{{ client.id }}">{{ client.name }}</option>
{% endfor %}
</select>
<input type="number" id="add-age" placeholder="Age"> <input type="number" id="add-age" placeholder="Age">
<input type="email" id="add-email" placeholder="Email"> <input type="email" id="add-email" placeholder="Email">
<button id="save-add">Add</button> <button id="save-add-two">Add</button>
</div> </div>
<!-- Edit Entry Popup --> <!-- Edit Entry Popup -->
<div id="edit-popup" class="popup"> <div id="edit-popup-two" class="popup">
<span class="close-btn">&times;</span> <span class="close-btn">&times;</span>
<h3>Edit Entry</h3> <h3>Edit Entry</h3>
<input type="hidden" id="edit-id"> <input type="hidden" id="edit-id">
<input type="text" id="edit-name" placeholder="Name"> <select id="edit-client-id">
{% for client in clients %}
<option value="{{ client.id }}">{{ client.name }}</option>
{% endfor %}
</select>
<input type="number" id="edit-age" placeholder="Age"> <input type="number" id="edit-age" placeholder="Age">
<input type="email" id="edit-email" placeholder="Email"> <input type="email" id="edit-email" placeholder="Email">
<button id="save-edit">Save</button> <button id="save-edit-two">Save</button>
</div> </div>
<script> <script>
@ -189,10 +206,10 @@
let currentModelName = null; // To track which model is being used let currentModelName = null; // To track which model is being used
// Open Add Popup for Table 2 // Open Add Popup for Table 2
$('#add-row-2').on('click', function () { $('#add-row-two').on('click', function () {
currentTableId = 'table-2'; currentTableId = 'table-two';
currentModelName = 'SecondTableEntry'; // Model name for Table 2 currentModelName = 'SecondTableEntry'; // Model name for Table 2
$('#add-popup').fadeIn(); $('#add-popup-two').fadeIn();
}); });
// Close Popups // Close Popups
@ -201,8 +218,8 @@
}); });
// Save Add Entry // Save Add Entry
$('#save-add').on('click', function () { $('#save-add-two').on('click', function () {
let name = $('#add-name').val(); let client_id = $('#add-client-id').val(); // ✅ Correct variable
let age = $('#add-age').val(); let age = $('#add-age').val();
let email = $('#add-email').val(); let email = $('#add-email').val();
@ -210,49 +227,49 @@
url: `/add-entry/${currentModelName}/`, url: `/add-entry/${currentModelName}/`,
method: 'POST', method: 'POST',
data: { data: {
'name': name, 'client_id': client_id, // ✅ REPLACE 'name' with this
'age': age, 'age': age,
'email': email, 'email': email,
'csrfmiddlewaretoken': '{{ csrf_token }}' 'csrfmiddlewaretoken': '{{ csrf_token }}'
}, },
success: function (response) { success: function (response) {
let rowCount = $('#table-2 tbody tr').length + 1; let rowCount = $('#table-two tbody tr').length + 1;
let newRow = ` let newRow = `
<tr data-id="${response.id}"> <tr data-id="${response.id}">
<td>${rowCount}</td> <!-- Serial # --> <td>${rowCount}</td> <!-- Serial # -->
<td>${response.id}</td> <!-- ID --> <td>${response.id}</td> <!-- ID -->
<td>${response.name}</td> <!-- Name --> <td>${response.client_name}</td> <!-- Name -->
<td>${response.age}</td> <!-- Age --> <td>${response.age}</td> <!-- Age -->
<td>${response.email}</td> <!-- Email --> <td>${response.email}</td> <!-- Email -->
<td>${response.date_joined}</td> <!-- Date Joined --> <td>${response.date_joined}</td> <!-- Date Joined -->
<td class="actions"> <!-- Actions --> <td class="actions"> <!-- Actions -->
<button class="edit-btn">Edit</button> <button class="edit-btn-two">Edit</button>
<button class="delete-btn">Delete</button> <button class="delete-btn-two">Delete</button>
</td> </td>
</tr> </tr>
`; `;
$('#table-2 tbody').append(newRow); $(`#${currentTableId} tbody`).append(newRow);
$('#add-popup').fadeOut(); $('#add-popup-two').fadeOut();
} }
}); });
}); });
// Open Edit Popup // Open Edit Popup
$(document).on('click', '.edit-btn', function () { $(document).on('click', '.edit-btn-two', function () {
let row = $(this).closest('tr'); let row = $(this).closest('tr');
currentTableId = row.closest('table').attr('id'); // Set current table ID currentTableId = row.closest('table').attr('id'); // Set current table ID
currentModelName = currentTableId === 'table-1' ? 'ExcelEntry' : 'SecondTableEntry'; // Set model name currentModelName = 'SecondTableEntry'; // Set model name
$('#edit-id').val(row.data('id')); $('#edit-id').val(row.data('id'));
$('#edit-name').val(row.find('td:eq(2)').text()); // Name is now in column 2 $('#edit-client-id').val(row.find('td:eq(2)').text()); // Name is now in column 2
$('#edit-age').val(row.find('td:eq(3)').text()); // Age is now in column 3 $('#edit-age').val(row.find('td:eq(3)').text()); // Age is now in column 3
$('#edit-email').val(row.find('td:eq(4)').text()); // Email is now in column 4 $('#edit-email').val(row.find('td:eq(4)').text()); // Email is now in column 4
$('#edit-popup').fadeIn(); $('#edit-popup-two').fadeIn();
}); });
// Save Edit Entry // Save Edit Entry
$('#save-edit').on('click', function () { $('#save-edit-two').on('click', function () {
let id = $('#edit-id').val(); let id = $('#edit-id').val();
let name = $('#edit-name').val(); let client_id = $('#edit-client-id').val();
let age = $('#edit-age').val(); let age = $('#edit-age').val();
let email = $('#edit-email').val(); let email = $('#edit-email').val();
@ -261,7 +278,7 @@
method: 'POST', method: 'POST',
data: { data: {
'id': id, 'id': id,
'name': name, 'client_id': client_id,
'age': age, 'age': age,
'email': email, 'email': email,
'csrfmiddlewaretoken': '{{ csrf_token }}' 'csrfmiddlewaretoken': '{{ csrf_token }}'
@ -272,7 +289,7 @@
row.find('td:eq(2)').text(response.name); row.find('td:eq(2)').text(response.name);
row.find('td:eq(3)').text(response.age); row.find('td:eq(3)').text(response.age);
row.find('td:eq(4)').text(response.email); row.find('td:eq(4)').text(response.email);
$('#edit-popup').fadeOut(); $('#edit-popup-two').fadeOut();
} else { } else {
alert('Failed to update entry: ' + response.message); alert('Failed to update entry: ' + response.message);
} }
@ -284,11 +301,11 @@
}); });
// Delete Entry // Delete Entry
$(document).on('click', '.delete-btn', function () { $(document).on('click', '.delete-btn-two', function () {
let row = $(this).closest('tr'); let row = $(this).closest('tr');
let id = row.data('id'); let id = row.data('id');
currentTableId = row.closest('table').attr('id'); // Set current table ID currentTableId = row.closest('table').attr('id'); // Set current table ID
currentModelName = currentTableId === 'table-1' ? 'ExcelEntry' : 'SecondTableEntry'; // Set model name currentModelName = 'SecondTableEntry'; // Set model name
if (!confirm('Are you sure you want to delete this entry?')) return; if (!confirm('Are you sure you want to delete this entry?')) return;

View File

@ -1,6 +1,6 @@
from django.urls import path from django.urls import path
from . import views from . import views
# Create your URLs here.
urlpatterns = [ urlpatterns = [
path('', views.clients_list, name='clients_list'), # Main page path('', views.clients_list, name='clients_list'), # Main page
path('table-one/', views.table_one_view, name='table_one'), # Table One path('table-one/', views.table_one_view, name='table_one'), # Table One

View File

@ -4,29 +4,43 @@ from django.apps import apps
from datetime import date from datetime import date
from .models import Client from .models import Client
# Clients Page (Now the homepage)
# Clients Page (Main)
def clients_list(request): def clients_list(request):
clients = Client.objects.all().order_by('id') clients = Client.objects.all().order_by('id')
return render(request, 'clients_table.html', {'clients': clients}) return render(request, 'clients_table.html', {'clients': clients})
# Table One View (ExcelEntry) # Table One View (ExcelEntry)
def table_one_view(request): def table_one_view(request):
entries_table1 = apps.get_model('sheets', 'ExcelEntry').objects.all() ExcelEntry = apps.get_model('sheets', 'ExcelEntry')
return render(request, 'table_one.html', {'entries_table1': entries_table1}) entries_table1 = ExcelEntry.objects.all()
clients = Client.objects.all()
return render(request, 'table_one.html', {
'entries_table1': entries_table1,
'clients': clients,
})
# Table Two View (SecondTableEntry) # Table Two View (SecondTableEntry)
def table_two_view(request): def table_two_view(request):
entries_table2 = apps.get_model('sheets', 'SecondTableEntry').objects.all() SecondTableEntry = apps.get_model('sheets', 'SecondTableEntry')
return render(request, 'table_two.html', {'entries_table2': entries_table2}) entries_table2 = SecondTableEntry.objects.all()
clients = Client.objects.all()
return render(request, 'table_two.html', {
'entries_table2': entries_table2,
'clients': clients,
})
# Add Entry (Generic for all models)
# Add Entry (Generic)
def add_entry(request, model_name): def add_entry(request, model_name):
if request.method == 'POST': if request.method == 'POST':
try: try:
model = apps.get_model('sheets', model_name) model = apps.get_model('sheets', model_name)
name = request.POST.get('name', 'New Name')
if model_name.lower() == 'client': if model_name.lower() == 'client':
name = request.POST.get('name', 'New Name')
address = request.POST.get('address', '') address = request.POST.get('address', '')
entry = model.objects.create(name=name, address=address) entry = model.objects.create(name=name, address=address)
return JsonResponse({ return JsonResponse({
@ -35,11 +49,13 @@ def add_entry(request, model_name):
'address': entry.address, 'address': entry.address,
}) })
client_id = request.POST.get('client_id')
client = Client.objects.get(id=client_id)
age = int(request.POST.get('age', 0)) age = int(request.POST.get('age', 0))
email = request.POST.get('email', 'example@email.com') email = request.POST.get('email', 'example@email.com')
entry = model.objects.create( entry = model.objects.create(
name=name, client=client,
age=age, age=age,
email=email, email=email,
date_joined=date.today() date_joined=date.today()
@ -47,7 +63,7 @@ def add_entry(request, model_name):
return JsonResponse({ return JsonResponse({
'id': entry.id, 'id': entry.id,
'name': entry.name, 'client_name': client.name,
'age': entry.age, 'age': entry.age,
'email': entry.email, 'email': entry.email,
'date_joined': entry.date_joined.strftime('%Y-%m-%d'), 'date_joined': entry.date_joined.strftime('%Y-%m-%d'),
@ -58,19 +74,18 @@ def add_entry(request, model_name):
return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400) return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400)
# Update Entry (Generic for all models)
# Update Entry (Generic)
def update_entry(request, model_name): def update_entry(request, model_name):
if request.method == 'POST': if request.method == 'POST':
try: try:
model = apps.get_model('sheets', model_name) model = apps.get_model('sheets', model_name)
entry_id = int(request.POST.get('id')) entry_id = int(request.POST.get('id'))
entry = model.objects.get(id=entry_id) entry = model.objects.get(id=entry_id)
name = request.POST.get('name')
if model_name.lower() == 'client': if model_name.lower() == 'client':
address = request.POST.get('address', '') entry.name = request.POST.get('name')
entry.name = name entry.address = request.POST.get('address', '')
entry.address = address
entry.save() entry.save()
return JsonResponse({ return JsonResponse({
'status': 'success', 'status': 'success',
@ -79,10 +94,12 @@ def update_entry(request, model_name):
'address': entry.address, 'address': entry.address,
}) })
client_id = request.POST.get('client_id')
client = Client.objects.get(id=client_id)
age = int(request.POST.get('age')) age = int(request.POST.get('age'))
email = request.POST.get('email') email = request.POST.get('email')
entry.name = name entry.client = client
entry.age = age entry.age = age
entry.email = email entry.email = email
entry.save() entry.save()
@ -90,7 +107,7 @@ def update_entry(request, model_name):
return JsonResponse({ return JsonResponse({
'status': 'success', 'status': 'success',
'id': entry.id, 'id': entry.id,
'name': entry.name, 'name': client.name,
'age': entry.age, 'age': entry.age,
'email': entry.email, 'email': entry.email,
'date_joined': entry.date_joined.strftime('%Y-%m-%d'), 'date_joined': entry.date_joined.strftime('%Y-%m-%d'),
@ -103,7 +120,8 @@ def update_entry(request, model_name):
return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400) return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400)
# Delete Entry (Generic for all models)
# Delete Entry (Generic)
def delete_entry(request, model_name): def delete_entry(request, model_name):
if request.method == 'POST': if request.method == 'POST':
try: try: