open command prompt in windows
then type go to d: drive
D:\> mkdir myap
D:\myap>cd myap
D:\myap>python -m venv env
D:\myap>env\Scripts\activate
D:myap>pip install django
D:\myap>django-admin startproject myproject
D:\myap>cd myproejct
D:\myap\myproejct>python manage.py startapp hello
D:\mywork\myproejct>python manage.py runserver.
(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)
write code for admin.py file:-
from django.contrib import admin
from .models import ContactForm
# Register your models here.
admin.site.register(ContactForm)
step 2:- create views.py file under your app folder :-
from django.shortcuts import render
from hello.models import ContactForm,contactEnquiry
from hello.forms import FormContactForm
from django.http import HttpResponse
def index(request):
return render(request, "home.html")
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,course):
data={'course':course}
return render(request, "update.html",data)
Now create urls.py file under your app folder :-
from django.urls import path
from .import views
urlpatterns=[
path('',views.index,name='index'),
path('home/',views.home,name='home'),
path('editemp/<int:id>', views.editemp, name='editemp'),
path('updateemp/<str:course>', views.updateemp, name='updateemp'),
]
Write code for home .html file under templates folder:-
<html>
<head>
{% load static %}
<link rel="stylesheet" href="{% static 'ok.css' %}"">
</head>
<body>
<h1 class="myclass">hello world how are you
</h1>
<div>
{% for n in serviceData %}
{{n.id}}
<span class="{{n.fullname}}"> {{n.fullname}}</span>
<span class="{{n.email}}">{{n.email}} </span>
<p> {{n.contact}} </p>
<a href="{% url 'editemp' n.id %}">Edit</a>
{% endfor %}
</div>
</body>
</html>
And then write code for edit.html templates file :-
<form action="/update/{{editupdaterecord.id}}" method="post">
{% csrf_token %}
name <input type=text value={{editupdaterecord.fullname}}>
email <input type=text value={{editupdaterecord.email}}>
Contact <input type=text value={{editupdaterecord.contact}}>
email <input type=text value={{editupdaterecord.message}}>
<input type=submit value=update>
</form>
Note your project folder urls.py code will be like this :-
from django.contrib import admin
from django.urls import path, include
urlpatterns=[
path('hello/',include('hello.urls')),
path('admin/', admin.site.urls),
]
Sign up here with your email
ConversionConversion EmoticonEmoticon