The USER has sent their information successfully through the browser but the information is not visible in DATABASE- I need some help to fix this problem so that I can see those information in my database. Below is the code of the project.
**models.py**
from django.db import models
# Create your models here.
class UserComments(models.Model):
full_name = models.CharField(max_length=100)
email_id = models.EmailField(max_length=100)
mobile_number = models.CharField(max_length=10, default='DEFAULT VALUE')
address = models.CharField(max_length=1000)
```````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
**forms.py**
from django import forms
from .models import UserComments
class CommentForm(forms.ModelForm):
class Meta:
model = UserComments
fields = '__all__'
`````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
**views.py:**
from django.shortcuts import render
from django.http import HttpResponse
from .models import UserComments
from myapp.forms import CommentForm
from django.http import JsonResponse
# Create your views here.
def home(request):
form = CommentForm()
form = CommentForm()
if request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
uc = UserComments(
full_name = cd['full_name'],
email_id = cd['email_id'],
mobile_number = cd['mobile_number'],
address = cd['address'],
)
uc.save()
return JsonResponse ({
'message': 'success'
})
return render(request, 'home.html', {'form': form})
````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````````
**home.html:**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>This is Home Page</h1>
<div>
<h1>Contact US</h1>
<form method = 'POST'>
{% csrf_token %}
<p>
<label for ="full_name">Name</label>
<input type="text" placeholder="Your Name" maxlength="200" required = "" id = "full_name">
</p>
<p>
<label for ="Mobile">Mobile Number</label>
<input type="text" placeholder="Your Name" maxlength="200" required = "" id = "full_name">
</p>
<p>
<label for ="Email">Email</label>
<input type="text" placeholder="Your Name" maxlength="200" required = "" id = "full_name">
</p>
<p>
<label for ="Addresss">Address</label>
<input type="text" placeholder="Your Name" maxlength="200" required = "" id = "full_name">
</p>
<button>Submit</button>
</form>
</div>
<script>
const form = document.getElementById('form');
form.addEventListner ("submit", submitHandler);
function submitHandler(e){
e.preventDefault();
fetch(form.acton, {method: 'POST',
body: new FormData(form)
})
.then(response => response.json())
.then (data => {
if (data.message === 'success'){
alert('Success!');
form.reset()
}
})
}
</script>
</body>
</html>
urls.py(Project-Level):
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘’, include(‘myapp.urls’)),
]
**urls.py(App-level):**
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
]
settings.py:
DEBUG = True
ALLOWED_HOSTS =
Application definition
INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘myapp’,
]
TEMPLATES = [
{
‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
‘DIRS’: [‘templates’],
‘APP_DIRS’: True,
‘OPTIONS’: {
‘context_processors’: [
‘django.template.context_processors.debug’,
‘django.template.context_processors.request’,
‘django.contrib.auth.context_processors.auth’,
‘django.contrib.messages.context_processors.messages’,
],
},
},
]
DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.sqlite3’,
‘NAME’: ‘aninda’,
‘USER’: ‘root’,
‘HOST’: ‘127.0.0.1’,
‘PORT’: ‘3306’,
‘PASSWORD’: ‘aninda@24’,
}
}