В Django ошибка выглядит так: TypeError at /Hod/Student/Add 'Course' object is not iterable [duplicate]
(https://i.stack.imgur.com/1qBDd.png)
Even if I change the course = Course.objects.get(id = course_id) to course = Course.objects.filter(id = course_id) there seems no error thrown but the message does not get printed that 'Student added successfully'.
**Hod_views.py**
Even if I change the course = Course.objects.get(id = course_id) to course = Course.objects.filter(id = course_id) there seems no error thrown but the message does not get printed that 'Student added successfully'.
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from app.models import Course, Session_Year, CustomUser, Student
from django.contrib import messages
@login_required(login_url="/")
def HOME(request):
return render(request, "Hod/home.html")
@login_required(login_url="/")
def ADD_STUDENT(request):
course = Course.objects.all()
session_year = Session_Year.objects.all()
if request.method == "POST":
profile_pic = request.FILES.get('profile_pic')
first_name = request.POST.get('first_name')
last_name = request.POST.get('last_name')
email = request.POST.get('email')
username = request.POST.get('username')
password = request.POST.get('password')
address = request.POST.get('address')
gender = request.POST.get('gender')
course_id = request.POST.get('course_id')
session_year_id = request.POST.get('session_year_id')
if CustomUser.objects.filter(email=email).exists():
messages.warning(request, "Email Is Already Taken!")
return redirect('add_student')
if CustomUser.objects.filter(username=username).exists():
messages.warning(request, "Username Is Already Taken!")
return redirect('add_student')
else:
user = CustomUser(
first_name = first_name,
last_name = last_name,
username = username,
email = email,
profile_pic = profile_pic,
user_type = 3
)
user.set_password = password
user.save()
course = Course.objects.get(id = course_id)
session_year = Session_Year.objects.get(id = session_year_id)
student = Student(
admin = user,
address = address,
session_year_id = session_year,
course_id = course,
gender = gender,
)
student.save()
messages.success(request, 'Student Successfully Saved!')
redirect('add_student')
context = {
"course" : course,
"session_year" : session_year,
}
return render(request, "Hod/add_student.html", context)
**Models.py:**
Even if I change the course = Course.objects.get(id = course_id) to course = Course.objects.filter(id = course_id) there seems no error thrown but the message does not get printed that 'Student added successfully'.
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class CustomUser(AbstractUser):
USER = (
(1,'HOD'),
(2,'STAFF'),
(3,'STUDENT')
)
user_type = models.CharField(choices=USER,max_length=50,default=1)
profile_pic = models.ImageField(upload_to='media/profile_pic')
class Course(models.Model):
name = models.CharField(max_length=100)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class Session_Year(models.Model):
session_start = models.CharField(max_length=100)
session_end = models.CharField(max_length=100)
def __str__(self):
return self.session_start + " To " + self.session_end
class Student(models.Model):
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
address = models.TextField()
gender = models.CharField(max_length=100)
course_id = models.ForeignKey(Course, on_delete=models.DO_NOTHING)
session_year_id = models.ForeignKey(Session_Year, on_delete=models.DO_NOTHING)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.admin.first_name + " " + self.admin.last_name