Hi Team,
I came to you asking for help again.
I am working on a little web app. For this one I have a HTML form to enter new data into model named Job_Position. Please note I am not using Django Form, I am using HTML form to allow user enter data and then save it by clicking submit button.
It seems a joke but the submit data stop working for all my HTML form suddenly, and that is weird because no one works, I spend an entire day trying to find the issue to fixed but not success.
Basically, I have the same HTML form to submit data to all my model, I just change the name and quantity of fields requested in the form based on the field in the model.
To avoid overloading the post, I will include all the information related how to data is saved for model Job_Position. (but as mentioned before, save is not working for any HTML form of any model)
If any of you have an idea of where the is or how to fixed, I will really appreciated.
If you need any further information form my end, please let me know.
Important: Database is SQLite
model.py
from django.db import models
from django.conf import settings
from django.contrib.auth.models import User
from django.utils.translation import gettext_lazy as _
# Create your models here.
class IsActive(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_active=True)
class SoftDelete(models.Model):
is_active = models.BooleanField(default=True, null=False, blank=False)
all_records = models.Manager()
active_records = IsActive()
def soft_delete(self):
self.is_active = False
self.save()
def undelete(self):
self.is_active = True
self.save()
class Meta:
abstract = True
class SoftDeleteManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_active = True)
class Job_Position(SoftDelete):
position_id = models.AutoField(primary_key=True, null=False, blank=False)
position_name = models.CharField(max_length=50, null=False, blank=False)
position_desc = models.CharField(max_length=200, null=True, blank=True)
sap_cd = models.CharField(max_length=10, null=True, blank=True)
created_dt = models.DateField(auto_now_add=True, null=False, blank=False)
created_by = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False, related_name='c_job_position')
last_modify_dt = models.DateField(auto_now=True, null=False, blank=False)
last_modify_by = models.ForeignKey(User, on_delete=models.PROTECT, null=False, blank=False, related_name='m_job_position')
def __str__(self) -> str:
return f"{self.position_name}"
views.py
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse, request
from django.template import loader
from .models import Factory, factory_Sector, Sector, Job_Position, Job_Shift, Person, Competency, Competency_Level, Person_Competency, Training, Scheduled_Training
from .models import Person_Training, Technology
from django.db.models import Subquery, OuterRef
#from openpyxl import Workbook
from django.views.generic import TemplateView
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login, authenticate
from django.contrib import messages
#from .serializers import Member_Model_Serializer
from rest_framework import viewsets
from rest_framework.permissions import IsAuthenticatedOrReadOnly
from rest_framework.authentication import SessionAuthentication
from django.contrib.auth.decorators import login_required
from .forms import LoginForm, EditColleagueForm
from django.utils import timezone
from django.http import JsonResponse
from django.views.decorators.http import require_POST
def addJobPosition(request):
if request.method == 'POST':
position_name = request.POST.get('positionName')
position_desc = request.POST.get('positionDesc')
sap_cd = request.POST.get('sapCd')
if not position_name:
error_msg = "Please make sure to enter data for all the mandatory fields (*)"
return render(request, 'addJobPosition.html', {'error_msg': error_msg})
try:
jobposition = Job_Position(
position_name=position_name,
position_desc=position_desc,
sap_cd=sap_cd,
created_by=request.user,
last_modify_by=request.user
)
jobposition.save()
return redirect('jobPosition')
except Exception as e:
error_msg = f"An error occurred while saving the data: {str(e)}"
context = {'error_msg': error_msg}
return render(request, 'addJobPosition.html', context)
return render(request, 'addJobPosition.html')
urls;py
from django.urls import path
from . import views
urlpatterns = [
path('', views.mainMenu, name='mainMenu'),
path('login/', views.user_login, name='login'), #loggedin
path('colleague/', views.personView, name='colleague'),
path('colleague/editColleague/<str:person_id>', views.editColleague, name='editColleague'),
path('factories/', views.factoryView, name='factories'),
path('competencies/', views.competencyView, name='competencies'),
path('sectorsByFactory/<int:factoryId>/', views.sectorsByFactoryView, name='sectorsByFactory'),
#path('addSectorToFactory/<int:factoryId>/', views.addSectorToFactoryView, name='addSectorToFactory'),
path('Sectors/', views.sectorView, name='Sectors'),
path('factoriesBySector/<int:sectorId>', views.factoriesBySectorView, name='factoriesBySector'),
path('technologies/', views.technologyView, name='technologies'),
path('trainings/', views.trainingView, name='trainings'),
path('calendar/', views.calendarView, name='calendar'),
path('jobShift/', views.jobShiftView, name='jobShift'),
path('jobPosition/', views.jobPositionView, name='jobPosition'),
path('addColleague/', views.addColleague, name='addColleague'),
path('addFactory/', views.addFactory, name='addFactory'),
path('addSector/', views.addSector, name='addSector'),
path('addJobPosition/', views.addJobPosition, name='addJobPosition'),
path('addJobShift/', views.addJobShift, name='addJobShift'),
path('addTechnology/', views.addTechnology, name='addTechnology'),
path('addTraining/', views.addTraining, name='addTraining'),
path('addCompetency/', views.addCompetency, name='addCompetency'),
path('addCalendarTraining/', views.addCalendarTraining, name='addCalendarTraining'),
path('new_training_completed/', views.PersonTrainingView, name='PersonTrainingView'),
]
Finally I have a sparate JavaScript file loading by load static
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('homeb').addEventListener('click', function() {
var url = this.getAttribute('data-url');
window.location.href = url;
});
document.getElementById('cancel').addEventListener('click', function() {
var url = this.getAttribute('data-url');
window.location.href = url;
});
});