0% found this document useful (0 votes)
60 views

FSD Lab Component-3

MODEL FORMS IN DJANGO

Uploaded by

sunil.ise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

FSD Lab Component-3

MODEL FORMS IN DJANGO

Uploaded by

sunil.ise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy