Django Insert database with Modelform example

 1)models.py file code:-

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)

2)admin.py file code:-

from django.contrib import admin
from .models import ContactForm

# Register your models here.


admin.site.register(ContactForm)



list_display = ['fullname', 'email','contact','message' ]


3)forms.py file code:-

from django import forms
from .models import ContactForm


class FormContactForm(forms.ModelForm):
    class Meta:
        model= ContactForm
        fields= ["fullname", "email", "contact", "message"]
        widgets={'fullname':forms.TextInput(
              attrs={'class':'form-control','id':'fullname'}),
                 'email':forms.TextInput(
                       attrs={'class':'form-control','id':'email'}),
                 'contact':forms.TextInput(
                       attrs={'class':'form-control','id':'contact'}),
                 'message':forms.Textarea(
                       attrs={'class':'form-control','id':'message'})
                 
}


4)views.py file code:-

from urllib import request
from django.shortcuts import render,redirect
from  hello.models  import ContactForm

from  hello.forms import FormContactForm

def showform(request):
    form= FormContactForm(request.POST or None)
    if form.is_valid():
        form.save()
 
    context= {'form': form }
       
    return render(request, 'contactform.html', context)

or you can also  write above  views function code :-

def showform(request):
    form= FormContactForm(request.POST or None)
    if form.is_valid():
        fn=request.POST['fullname']
        em=request.POST['email']
        cn=request.POST['contact']
        msg=request.POST['message']
        data=ContactForm(fullname=fn,email=em,contact=cn,message=msg)
        data.save()
 
    context= {'form': form }
       
    return render(request, 'contactform.html', context)

5)urls.py file code  (app folder ):-


from django.urls import path

from .import views



urlpatterns=[
path('',views.index,name='index'),


path('showform/', views.showform),

]

6)contactform.html file code (create this file under templates folder of your app folder ):-

<!DOCTYPE html>  
<html lang="en">  
<head>  
<meta charset="UTF-8">  
<title>Django Contact Form</title>  
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js">
</script>
</head>  
<body>  
    <div class="container">
<form method="POST" class="post-form" action="#">  
{% csrf_token %}  
{{ form.as_p }}
<div class="form-group">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>  
</body>  
</html>
Previous
Next Post »