django modle
django modle
Develop a Model form for student with a model called project Student_Course using
Django App with ( Student : USN, Student name, Gender, Semester, Course_no) (Course :
Course_no, Course_name, Duration, Fees) For the aforementioned student and course models
created, register admin interfaces, perform migrations and illustrate data entry through admin
forms. For the aforementioned Student and Course Model create a generic class view which
displays list of students and detailview that displays student details for any selected student in the
list with one or more courses registered by him
myproject/settings.py
INSTALLED_APPS = [
...
'myapp',
]
myproject/urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
myapp/urls.py
urlpatterns = [
path('', home_view, name='home'),
path('students/', StudentListView.as_view(), name='student_list'),
path('students/<int:pk>/', StudentDetailView.as_view(), name='student_detail'),
path('courses/', CourseListView.as_view(), name='course_list'),
]
models.py
class Course(models.Model):
course_no = models.CharField(max_length=10, unique=True)
course_name = models.CharField(max_length=100)
duration = models.IntegerField() # Duration in weeks
fees = models.DecimalField(max_digits=10, decimal_places=2)
def __str__(self):
return self.course_name
class Student(models.Model):
USN = models.CharField(max_length=10, unique=True)
name = models.CharField(max_length=100)
GENDER_CHOICES = [
('M', 'Male'),
('F', 'Female'),
('O', 'Other'),
]
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
semester = models.IntegerField()
courses = models.ManyToManyField(Course)
def __str__(self):
return self.name
admin.py
class CourseInline(admin.TabularInline):
model = Student.courses.through
extra = 1
@admin.register(Student)
class StudentAdmin(admin.ModelAdmin):
list_display = ('USN', 'name', 'gender', 'semester')
search_fields = ('USN', 'name')
inlines = [CourseInline]
exclude = ('courses',) # To prevent the M2M field from showing up twice
@admin.register(Course)
class CourseAdmin(admin.ModelAdmin):
list_display = ('course_no', 'course_name', 'duration', 'fees')
search_fields = ('course_no', 'course_name')
inlines = [CourseInline]
views.py
class StudentListView(ListView):
model = Student
template_name = 'students/student_list.html'
context_object_name = 'students'
class StudentDetailView(DetailView):
model = Student
template_name = 'students/student_detail.html'
context_object_name = 'student'
class CourseListView(ListView):
model = Course
template_name = 'courses/course_list.html'
context_object_name = 'courses'
def home_view(request):
return render(request, 'home.html')
myapp/templates/courses/course_list.html
<!DOCTYPE html>
<html>
<head>
<title>Course List</title>
</head>
<body>
<h1>Course List</h1>
<ul>
{% for course in courses %}
<li>{{ course.course_name }} ({{ course.course_no }}) - Duration: {{ course.duration }} weeks,
Fees: ${{ course.fees }}</li>
{% endfor %}
</ul>
<a href="{% url 'home' %}">Back to Home</a>
</body>
</html>
myapp/template/students/student_list.html
<!DOCTYPE html>
<html>
<head>
<title>Student List</title>
</head>
<body>
<h1>Student List</h1>
<ul>
{% for student in students %}
<li><a href="{% url 'student_detail' student.pk %}">{{ student.name }}</a></li>
{% endfor %}
</ul>
<a href="{% url 'home' %}">Back to Home</a>
</body>
</html>
myapp/template/students/student_detail.html
<!DOCTYPE html>
<html>
<head>
<title>Student Detail</title>
</head>
<body>
<h1>{{ student.name }}</h1>
<p>USN: {{ student.USN }}</p>
<p>Gender: {{ student.get_gender_display }}</p>
<p>Semester: {{ student.semester }}</p>
<h4>Courses</h4>
<ul>
{% for course in student.courses.all %}
<li>{{ course.course_name }} ({{ course.course_no }}) - Duration: {{ course.duration }} weeks,
Fees: ${{ course.fees }}</li>
{% endfor %}
</ul>
<a href="{% url 'student_list' %}">Back to Student List</a>
</body>
</html>
http://127.0.0.1:8000/admin
To execute
http://127.0.0.1:8000
Click student list
click courses