Develop a Django app that performs student registration to a course. It should also display list of students registered for any selected course. Create students and course as models with enrolment as ManyToMany field
Note
Make sure to execute this program properly and also keep this one for future alterations cause we are going to use this exact program for Moduel4's 1st and 2nd lab programs
Start project and app
No need of creating new virtual environment, If there exist a previously created virtual environment just activate it and follow the below steps.
start a new django project django-admin startproject models_demo, enter into that directory cd models_demo and open VScode in that directory code .
VScode part
In VS code open terminal and start a new app python3 manage.py startapp models_app.
Register this app in settings.py file. Open settings.py and goto Installed apps section and add app name models_app in the list.
Edit urls.py file in models_demo
edit that file as shown below.
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include("models_app.urls"))
]
Creat a models
goto models_app folder and edit model.py file
models_app->models.py
from django.db import models
# Create your models here.
class Student(models.Model):
name = models.CharField(max_length=50)
email = models.EmailField(unique=True)
def __str__(self):
return self.name
class Course(models.Model):
title = models.CharField(max_length=200)
students = models.ManyToManyField(Student, related_name='courses', blank=True)
def __str__(self):
return self.title
Register the models in admin.py file
models_app->admin.py
from django.contrib import admin
from models_app.models import Student,Course
# Register your models here.
admin.site.register(Student)
admin.site.register(Course)
Create a templates folder and add html files
create a templates folder at models_demo project folder not in the app.
Register this templates folder in settings.py.
In models_demo->settings.py add this line 'DIRS':[BASE_DIR,templates'],