Я хочу сохранить данные формы в базе данных, но когда я нажимаю кнопку отправки, строки в базе данных обновляются, вместо этого INSERTING
с домашней страницы. Я нажимаю на ссылку под названием. fetch_faculty_subject , который будет извлекать некоторые данные из базы данных и отображать вывод в виде выпадающего списка (в HTML).снова из form Я добавляю некоторые данные в форму и сохраняю в таблицу с именем Mapped_faculty_n_subjects
это мои views.py
def fetch_faculty_subject(request):
faculties= facultyhours.objects.filter(user_id = request.user.id)
subjects= subject_code.objects.filter(user_id = request.user.id)
classname= class_number.objects.filter(user_id = request.user.id)
context1 = {
'faculties': faculties,
'subjects': subjects,
'classes': classname
}
return render(request, 'createtimetable_for_class/selecting_subjects_n_teachers.html', context1)
def store_the_faculty_subject(request):
userr = request.user.id
selected_class_name=request.POST["classname"]
selected_faculty_name=request.POST["facultyname"]
selected_subject_name=request.POST["subjectname"]
selected_total_hours_per_week=request.POST["totalhours"]
faculty_n_subject_db=Mapped_faculty_n_subjects(selected_class_name=selected_class_name,selected_faculty_name=selected_faculty_name,selected_subject_name=selected_subject_name,selected_total_hours_per_week=selected_total_hours_per_week,user_id=userr)
faculty_n_subject_db.save()
# return redirect('/fetch_faculty_subject/')
faculties= facultyhours.objects.filter(user_id = request.user.id)
subjects= subject_code.objects.filter(user_id = request.user.id)
classname= class_number.objects.filter(user_id = request.user.id)
context1 = {
'faculties': faculties,
'subjects': subjects,
'classes': classname
}
# return redirect(request,'createtimetable_for_class/selecting_subjects_n_teachers.html',context1)
return render(request, 'createtimetable_for_class/selecting_subjects_n_teachers.html',context1)
это моя models.py
class Mapped_faculty_n_subjects(models.Model):
selected_class_name=models.CharField(max_length=250,primary_key=True)
selected_faculty_name=models.CharField(max_length=250)
selected_subject_name=models.CharField(max_length=250)
selected_total_hours_per_week=models.CharField(max_length=250)
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True)
это моя HTML-форма
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div class="row justify-content-center">
<div class="col-6">
<div class="card">
<div class="card-body">
<h2>Select Faculty and Subject</h2>
<form method="post" action="/store_the_faculty_subject/">
{% csrf_token %}
{{ form|crispy }}
<label>Select The Class</label><br>
<select name="classname">
{% for classes in classes %}
<option>{{ classes.class_name }}</option>
{% endfor %}
</select><br><br>
<label>Select The Faculty</label><br>
<select name="facultyname">
{% for faculty in faculties %}
<option>{{ faculty.faculty_name }}</option>
{% endfor %}
</select><br><br>
<label>Select The Subject</label><br>
<select name="subjectname">
{% for subjects in subjects %}
<option>{{ subjects.subject_name }}</option>
{% endfor %}
</select><br><br>
<label>Total hours per week</label><br>
<input type="number" max="34" name="totalhours" required class="textinput textInput form-control"><br>
<button type="submit" class="btn btn-primary">SUBMIT</button>
<a class="btn btn-secondary" href="#">NEXT</a>
</form>
</div>
</div>
</div>
</div>
{% endblock %}