Views.py
def create_course_save(request):
if request.method == 'POST':
courseCode = request.POST.get('course_code')
courseName = request.POST.get('course_name')
courseShortInfo = request.POST.get('course_short_info')
courseWYWL = request.POST.get('course_wywl')
courseSYWG = request.POST.get('course_sywg')
courseDesc = request.POST.get('course_desc')
courseCC = request.POST.get('course_coordinator')
courseCC = User.objects.get(id=courseCC)
courseBranch = request.POST.get('branch')
courseSemester = request.POST.get('semester')
courseSemester = Semester.objects.get(id=courseSemester)
courseFiles = request.FILES.getlist('course_files')
try:
for file in courseFiles:
courseObj = Course.objects.create(
course_code=courseCode,
course_name=courseName,
course_short_info=courseShortInfo,
course_wywl=courseWYWL,
course_sywg=courseSYWG,
course_desc=courseDesc,
course_coordinator=courseCC,
branch=courseBranch,
semester=courseSemester,
course_files=file)
except Exception as e:
print(e)
return redirect('manage_courses')
models.py
class Course(models.Model):
BRANCH_CHOICES = [
("","Branch Name"),
("Computer Science and Engineering","Computer Science and Engineering"),
("Aerospace/aeronautical Engineering","Aerospace/aeronautical Engineering"),
("Chemical Engineering","Chemical Engineering"),
("Civil Engineering","Civil Engineering"),
("Electronics and Communications Engineering","Electronics and Communications Engineering"),
("Electrical and Electronics Engineering","Electrical and Electronics Engineering"),
("Petroleum Engineering","Petroleum Engineering"),
("Bio Technology","Bio Technology"),
("Mechanical Engineering","Mechanical Engineering"),
]
id = models.UUIDField(primary_key = True, unique = True, default = uuid.uuid4, editable = False)
course_code = models.CharField(max_length = 100, unique = True)
course_name = models.CharField(max_length = 100, unique = True)
course_short_info = models.TextField(max_length = 500, default="Start your path to a career in project management. No degree or experience is required.")
course_wywl = models.TextField(max_length = 500, default="WHAT YOU WILL LEARN")
course_sywg = models.TextField(max_length = 500, default="SKILLS YOU WILL GAIN")
course_desc = models.TextField(max_length = 500, default="Description of the Course")
course_coordinator = models.ForeignKey(User, on_delete=models.SET_NULL, blank=True, null=True)
branch = models.CharField(max_length = 50, choices = BRANCH_CHOICES, default=1)
semester = models.ForeignKey(Semester, on_delete=models.SET_NULL, blank=True, null=True)
course_files = models.FileField(upload_to='Course Files/', blank=True, null=True)
def __str__(self):
return '%s - %s' % (self.course_code, self.course_name)
admin.py
from django.contrib import admin
from .models import (Course)
admin.site.register(Course)
Front-end (HTML)
<label for="Upload Files">Upload Files</label>
<input type="file" name="course_files" id="id_course_files" multiple>
<input type="submit">
And I didn’t used forms