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

Program 7

Uploaded by

iitshastry03
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Program 7

Uploaded by

iitshastry03
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Program-7

Develop a Model form for student that contains his topic chosen for
project, languages used and duration with a model called project.

Create application:

>python manage.py startapp lab7

In lab7 subfolder, write code in models.py

from django.db import models


from django.forms import ModelForm

class Student(models.Model):
student_usn=models.CharField(max_length=20)
student_name=models.CharField(max_length=100)
student_sem=models.IntegerField()
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']
In lab7 subfolder, write code in views.py

from django.shortcuts import render


from django.http import HttpResponse
from lab7.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>")
else:
return HttpResponse("<h1>Record not inserted</h1>")
else: form=ProjectReg()
return render(request,"add_project.html",{"form":form})

In the lab7 folder, create a subfolder ‘templates’ and within the templates subfolder
create a file ‘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>

In lab7 subfolder, write code in urls.py

from django.urls import path


from lab7.views import add_project

urlpatterns = [
path('add_project/',add_project),
]
Output:
 Activate app by adding app name ‘lab7’ in the settings.py in the project folder p.
 Run command for propagating changes into database schema.
>python manage.py makemigrations
>python manage.py migrate

 Save and Run server by executing command:

>python manage.py runserver

 Insert records into the student table.

 In the browser, enter the url :127.0.0.1:8000/add_project/


 On submitting records, it display:

 Submission is reflected in the project table.

You might also like