INFORMATION is not visible in DATABASE

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(App-level):
from django.urls import path
from . import views

urlpatterns = [
path(‘’, views.home, name=“home”),
]

**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')),
]

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’,
}
}

The Browser render:

Please format your code properly in order to be readable…

Where did you see this message?

Hey @anefta , Thank you for showing keen interest for my Django code. In above code, I have given the complete code including- forms.py, models.py, views.py, templates( home.html ), urls.py ( Project-level ), urls.py (App-level ) and settings.py.

When, I try to submit the information in rendered form in the browser, the information doesn’t appear in the database. There is no error during the compilation. I have given the pictures above.

I shall be remained grateful if you can help me to fix this problem.

Side Note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. It would help if you fixed your post as requested above.

One of the first things I see is that you are manually rendering your input widgets instead of rendering the form fields. You should be rendering the form in your template.

I suggest you read the docs at:

Also, you are inconsistent with how you’re returning your results in your view.

The GET portion of the view is returning an HttpResponse, while the POST version of a valid form returns a JsonResponse.

However, if the form is invalid, then the POST is going to return an HttpResponse, which your AJAX call is unable to handle.
Additionally, even if you did return errors in a JsonResponse, you don’t have any code in your JavaScript to handle it.

Thank you sir @KenWhitesell, But I could not understand the solution.
Sir, if you share the correct code, it will really help me to visualise the problem and my project will work.
I have been searching for the solution from past two days but I could not find any solution. I am trying to replace the Front-End code with HTML5. When user submit the information, the terminal displays the HTTP 200 ( successful ) but the information doesn’t appear in database - this is the problem.
Sir I shall be grateful if you can provide the solution code.

The examples you’re looking for are in the referenced docs.

The simplest case of building and referencing a form in your template is provided in the section Building a form in django. Other examples on that page show you different ways to handle them, such as in the section for Rendering fields manually.

Regarding the JavaScript issues, you’ve got to make the decision whether that’s the way you need to go - or not.

The simple answer is to get rid of the JavaScript and allow your submit button to submit the form data using standard HTML. That makes things easier on both sides. However, that may not satisfy all your requirements, and so you would need to add more code to make this all work appropriately - but that’s a decision for you to make.

Thank you sir @KenWhitesell, I am willing to submit the form with Standard HTML5 getting rid of JavaScript and I am willing to add more code so that, when the user submits information it must appear in the Database.
Sir, I shall be grateful if you can share me the complete code.

If you are interested in a more complete example of using forms, see the forms section in the Django Girls tutorial at: Django Forms · HonKit