Open command prompt in windows
then in command prompt type d:
and press enter .
and use following commands
To create folder myap -execute the following command:
D:\> mkdir myap
To go myap folder path -execute the following command:
D:\>cd myap
To create a virtual environment for your project, open a new command prompt, navigate to the myap folder where you want to create your project and then execute the following command:-
D:\myap>python -m venv env
In the command prompt, ensure your virtual environment is active, and execute the following command:
D:\myap>env\Scripts\activate
execute the following command to install django.
D:myap>pip install django
run the following command - This will create a myproject
directory in your current directory
D:\myap>django-admin startproject myproject
then go to myproject folder path type following command :-
D:\myap>cd myproejct
To create your app, make sure you’re in the your project directory & type this command:
D:\myap\myproejct>python manage.py startapp hello
That’ll create a directory hello app folder inside my_project folder:
after it open your myap folder inside it you will see my_project folder
D:\mywork\myproejct>python manage.py runserver.
Step 1:-
Write code for models.py file:-
from django.db import models
# Create your models here.
class ContactForm(models.Model):
fullname= models.CharField(max_length=100)
email= models.EmailField()
contact= models.CharField(max_length=50)
message= models.CharField(max_length=200)
class contactEnquiry(models.Model):
name=models.CharField(max_length = 50)
email=models.CharField(max_length = 50)
message=models.TextField(max_length=50)
class Dreamreal(models.Model):
website = models.CharField(max_length = 50)
mail = models.CharField(max_length = 50)
name = models.CharField(max_length = 50)
phonenumber = models.IntegerField()
class Meta:
db_table = "dreamreal"
class editupdaterecord(models.Model):
empname=models.CharField(max_length=50)
email=models.CharField(max_length=50)
salary=models.IntegerField()
class Meta:
db_table="empdetails"
write code for admin.py file :-
from django.contrib import admin
from .models import contactEnquiry,ContactForm
# Register your models here.
admin.site.register(contactEnquiry)
admin.site.register(ContactForm)
list_display = ['fullname', 'email','contact','message' ]
then run command
py manage.py makemigrations
write code for forms.py file :-
from django import forms
from .models import ContactForm
class FormContactForm(forms.ModelForm):
class Meta:
model= ContactForm
fields= ["fullname", "email", "contact", "message"]
urls.py file code :-
from django.urls import path
from .import views
urlpatterns=[
path('',views.index,name='index'),
path('home/',views.home,name='home'),
path('about/',views.about,name='about'),
path('showform/', views.showform),
path('search/', views.search,name='search'),
path('search1/', views.search1,name='search1'),
path('editemp/<int:id>', views.editemp, name='editemp'),
path('updateemp/<str:id>', views.updateemp, name='updateemp'),
path('saveenquiry/',views.saveenquiry,name="saveenquiry"),
]
Step 2:- views.py file code :-
from urllib import request
from django.shortcuts import render
from hello.models import ContactForm,contactEnquiry
from hello.forms import FormContactForm
from django.http import HttpResponse
from django.db.models import Q
def index(request):
return render(request, "home.html")
def home(request):
return render(request, "home.html")
def about(request):
return render(request, "about.html")
def showform(request):
form= FormContactForm(request.POST or None)
if form.is_valid():
form.save()
context= {'form': form }
return render(request, 'contactform.html', context)
def saveenquiry(request):
if request.method=="POST":
email=request.POST.get('email',False)
name=request.POST.get('name',False)
message=request.POST.get('msg',False)
en=contactEnquiry(name=name,email=email,message=message)
en.save()
n='data inserted'
return render(request,"contact.html",{'n':n})
def home(request):
serviceData=ContactForm.objects.all()
data={'serviceData':serviceData
}
return render(request,"home.html",data)
def editemp(request,id):
data=ContactForm.objects.get(id=id)
return render(request, "edit.html",{'editupdaterecord':data})
def updateemp(request,id):
if request.method=='POST':
pi=ContactForm.objects.get(id=id)
form= FormContactForm(request.POST ,instance=pi)
if form.is_valid():
form.save()
msg="record updated"
return render(request, "update.html",{'editupdaterecord':msg})
def search(request):
if request.GET.get('myform'): # write your form name here
book_name = request.GET.get('myform')
try:
status = ContactForm.objects.filter(fullname__icontains=book_name)
return render(request,"search1.html",{"books":status})
except:
return render(request,"search1.html",{'books':status})
else:
return render(request, 'search1.html', {'books':status})
def search1(request):
if 'q' in request.GET:
q=request.GET['q']
multiple_q=Q(Q(fullname=q)| Q(message=q))
data=ContactForm.objects.filter(multiple_q)
else:
data =ContactForm.objects.all()
return render(request,'search1.html',{
'data':data
})
Step 3:- code for search1.html file :-
<form name="myform" method="GET" action="{% url 'search1' %}">
<input type="text" name="q" placeholder="Search" />
<button type=submit> search</button></form>
{% for data in data %}
<td>{{data.fullname}}</td>
<td>{{data.email}}</td>
<td>{{data.contact}}</td>
{% endfor %}
Sign up here with your email
ConversionConversion EmoticonEmoticon