FULL STACK DEVELOPMENT LAB COMPONENT
Module-3: Django Admin Interfaces and Model Forms
Laboratory Component:
1. For student and course models created in Lab experiment for Module2, register admin interfaces,
perform migrations and illustrate data entry through admin forms.
2. Develop a Model form for student that contains his topic chosen for project, languages used and
duration with a model called project.
1. For student and course models created in Lab experiment for Module2, register admin interfaces,
perform migrations and illustrate data entry through admin forms.
models.py
from django.db import models
# Create your models here.
class Course(models.Model):
course_code=models.CharField(max_length=40)
course_name=models.CharField(max_length=100)
course_credits=models.IntegerField(blank=True, null=True)
def __str__(self):
return self.course_name
class Student(models.Model):
student_usn=models.CharField(max_length=20)
student_name=models.CharField(max_length=100)
student_sem=models.IntegerField()
enrolment=models.ManyToManyField(Course)
def __str__(self):
return self.student_name+"("+self.student_usn+")"
admin.py
from django.contrib import admin
# Register your models here.
from ap1.models import Course, Student
# Register your models here.
#admin.site.register(Student)
admin.site.register(Course)
class StudentAdmin(admin.ModelAdmin):
list_display = ('student_name','student_usn','student_sem')
ordering=('student_name',)
search_fields = ('student_name',)
admin.site.register(Student, StudentAdmin)
Dept. of ISE, CITech [SK] pg. 1
FULL STACK DEVELOPMENT LAB COMPONENT
Next follow these steps
Open the settings.py file in your project.
Find the list called INSTALLED_APPS.
Add 'ap1' to the INSTALLED_APPS list.
Save and close the settings.py file.
Open your command line or terminal.
Type python manage.py makemigrations and press Enter.
Then type python manage.py migrate and press Enter.
Type python manage.py createsuperuser and press Enter.
Follow the prompts to enter a username, email address, and password.
After creating the superuser, start the development server by typing
python manage.py runserver and pressing Enter.
Open your web browser and go to http://127.0.0.1:8000/admin/.
Log in using the superuser credentials you created.
Once logged in, you can add, edit, or delete data from the admin interface.
Output :
Dept. of ISE, CITech [SK] pg. 2
FULL STACK DEVELOPMENT LAB COMPONENT
Dept. of ISE, CITech [SK] pg. 3
FULL STACK DEVELOPMENT LAB COMPONENT
2. Develop a Model form for student that contains his topic chosen for project, languages used and
duration with a model called project.
models.py
from django.db import models
from django.forms import ModelForm
# Create your models here.
class Course(models.Model):
course_code=models.CharField(max_length=40)
course_name=models.CharField(max_length=100)
course_credits=models.IntegerField(blank=True, null=True)
def __str__(self):
return self.course_name
class Student(models.Model):
student_usn=models.CharField(max_length=20)
student_name=models.CharField(max_length=100)
student_sem=models.IntegerField()
enrolment=models.ManyToManyField(Course)
def __str__(self):
return self.student_name+"("+self.student_usn+")"
class Project(models.Model):
student=models.ForeignKey(Student,on_delete=models.CASCADE)
ptopic=models.CharField(max_length=200)
plangauges=models.CharField(max_length=200)
pduration=models.IntegerField()
class ProjectReg(ModelForm):
required_css_class="required"
class Meta:
model=Project
fields=['student','ptopic','plangauges','pduration']
views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render
from ap1.models import ProjectReg
def add_project(request):
if request.method=="POST":
form=ProjectReg(request.POST)
if form.is_valid():
form.save()
return HttpResponse("<h1>Record inserted successfully</h1>")
Dept. of ISE, CITech [SK] pg. 4
FULL STACK DEVELOPMENT LAB COMPONENT
else:
return HttpResponse("<h1>Record not inserted</h1>")
else:
form=ProjectReg()
return render(request,"add_project.html",{"form":form})
In your application folder, create a new folder named templates.
Inside the templates folder, create a file named add_project.html.
add_project.html
<html>
<form method="post" action="">
{% csrf_token %}
<table>
{{ form.as_table}}
<tr>
<td>
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form>
</html>
urls.py
from django.contrib import admin
from django.urls import path
from ap1.views import add_project
urlpatterns = [
path('admin/', admin.site.urls),
path('add_project/', add_project),
]
Next follow these steps
Open the settings.py file in your project.
Find the list called INSTALLED_APPS.
Add 'ap1' to the INSTALLED_APPS list.
Save and close the settings.py file.
Open your command line or terminal.
Type python manage.py makemigrations and press Enter.
Then type python manage.py migrate and press Enter.
Type python manage.py createsuperuser and press Enter.
Follow the prompts to enter a username, email address, and password.
After creating the superuser, start the development server by typing
python manage.py runserver and pressing Enter.
Open your web browser and go to http://127.0.0.1:8000/add_project/
Dept. of ISE, CITech [SK] pg. 5
FULL STACK DEVELOPMENT LAB COMPONENT
Fill in the details in the form.
Click the "Submit" button.
The record will be inserted into the database.
Output
Dept. of ISE, CITech [SK] pg. 6