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

CRUD Operations DJANGO

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)
154 views

CRUD Operations DJANGO

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
You are on page 1/ 4

CRUD Operations DJANGO:

Insert:
Models.py
from django.db import models

# Create your models here.


class EmpModel(models.Model):
Empname=models.CharField(max_length=10)
DeptNmae=models.CharField(max_length=10)
salary=models.IntegerField()
#def __str__(self):
# return f"{self.Empname} : {self.DeptNmae}:{self.salary}"

Forms.py:

from django import forms


from .models import EmpModel
class EmpForm(forms.ModelForm):
class Meta:

URLS.PY:
from django.contrib import admin
from django.urls import path
from myapp.views import insert
urlpatterns = [
path('admin/', admin.site.urls),
path('insert/', insert),
]
model = EmpModel
fields = "__all__"
Views.py:
from django.shortcuts import render
from .myEmpform import EmpForm
# Create your views here.
def insert(request):

if request.method=="POST":
form=EmpForm(request.POST)
if form.is_valid():
form.save()
return render(request,"create.html")
else:
form=EmpForm()
context={"form":form}
return
render(request,"create.html",context)f"{self.student_name} :
{self.rollnumber}"

URLS.PY:
from django.contrib import admin
from django.urls import path
from myapp.views import insert
urlpatterns = [
path('admin/', admin.site.urls),
path('insert/', insert),
]
Create.html

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

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

</html>

Settings.py
TEMPLATES = [
{

URLS.PY:
from django.contrib import admin
from django.urls import path
from myapp.views import insert
urlpatterns = [
path('admin/', admin.site.urls),
path('insert/', insert),
]
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR/'Templates/'],
]
DATABASES = {
'default': {

'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER':'root',
'PASSWORD':'root'
}
}

URLS.PY:
from django.contrib import admin
from django.urls import path
from myapp.views import insert
urlpatterns = [
path('admin/', admin.site.urls),
path('insert/', insert),
]

You might also like