0% found this document useful (0 votes)
106 views4 pages

Django CRUD Operations Example

The document describes the CRUD operation for inserting data into a Django database. It defines an EmpModel in models.py, creates a form to input data in forms.py, includes the form route in urls.py, handles form submission and saving to the database in views.py, and renders the form template in create.html. It also configures the database and templates directories in settings.py.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views4 pages

Django CRUD Operations Example

The document describes the CRUD operation for inserting data into a Django database. It defines an EmpModel in models.py, creates a form to input data in forms.py, includes the form route in urls.py, handles form submission and saving to the database in views.py, and renders the form template in create.html. It also configures the database and templates directories in settings.py.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CRUD Operations DJANGO:

Insert:
[Link]
from [Link] import models

# Create your models here.


class EmpModel([Link]):
Empname=[Link](max_length=10)
DeptNmae=[Link](max_length=10)
salary=[Link]()
#def __str__(self):
# return f"{[Link]} : {[Link]}:{[Link]}"

[Link]:

from django import forms


from .models import EmpModel
class EmpForm([Link]):
class Meta:

[Link]:
from [Link] import admin
from [Link] import path
from [Link] import insert
urlpatterns = [
path('admin/', [Link]),
path('insert/', insert),
]
model = EmpModel
fields = "__all__"
[Link]:
from [Link] import render
from .myEmpform import EmpForm
# Create your views here.
def insert(request):

if [Link]=="POST":
form=EmpForm([Link])
if form.is_valid():
[Link]()
return render(request,"[Link]")
else:
form=EmpForm()
context={"form":form}
return
render(request,"[Link]",context)f"{self.student_name} :
{[Link]}"

[Link]:
from [Link] import admin
from [Link] import path
from [Link] import insert
urlpatterns = [
path('admin/', [Link]),
path('insert/', insert),
]
[Link]

<html>
<head>
</head>
<body>
<form method="post">

{% csrf_token %}
{{form.as_p}}
<input type = "submit" value = "submit">
</form>
</body>

</html>

[Link]
TEMPLATES = [
{

[Link]:
from [Link] import admin
from [Link] import path
from [Link] import insert
urlpatterns = [
path('admin/', [Link]),
path('insert/', insert),
]
'BACKEND': '[Link]',
'DIRS': [BASE_DIR/'Templates/'],
]
DATABASES = {
'default': {

'ENGINE': '[Link]',
'NAME': 'test',
'USER':'root',
'PASSWORD':'root'
}
}

[Link]:
from [Link] import admin
from [Link] import path
from [Link] import insert
urlpatterns = [
path('admin/', [Link]),
path('insert/', insert),
]

You might also like