data the is not saving the data in to database

when i was login as admin the form is saving the data and when i was log-out as admin and enter my form data the is not saving the data in to database but when i log in as guest the form is saving the data

# views.py
from django.shortcuts import render, redirect
from django.http import JsonResponse
from .models import FormData

from django.shortcuts import render, redirect
from django.http import JsonResponse
from .models import FormData

def save_form_data(request):
    print(f"Request method: {request.method}")
    print(f"Request user: {request.user}")
    if request.method == 'POST':
        # Retrieve form data
        first_name = request.POST.get('firstname')
        last_name = request.POST.get('lastname')
        email = request.POST.get('email')
        phone = request.POST.get('phone')
        message = request.POST.get('message')

        # Save the data to the FormData model
        form_data = FormData.objects.create(
            first_name=first_name,
            last_name=last_name,
            email=email,
            phone=phone,
            message=message,
        )

        # Assuming you have a JavaScript function to display SweetAlert
        return JsonResponse({'status': 'success', 'form_data_id': form_data.id})
    else:
        # Handle non-POST requests appropriately
        return JsonResponse({'status': 'error', 'message': 'Invalid request method'})

from django.shortcuts import render
from .models import FormData

def display_form_data(request):
    # Retrieve all form data from the FormData table
    form_data_list = FormData.objects.all()

    # Pass the form data to the template
    return render(request, 'display_form_data.html', {'form_data_list': form_data_list})
from django.shortcuts import render, get_object_or_404
from django.http import JsonResponse
from .models import FormData

def delete_form_data(request, form_data_id):
    form_data = get_object_or_404(FormData, id=form_data_id)
    form_data.delete()
    return JsonResponse({'status': 'success', 'message': 'Record deleted successfully'})
# models.py
from django.db import models

class FormData(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField()
    phone = models.CharField(max_length=15)
    message = models.TextField()

    def __str__(self):
        return f"{self.first_name} {self.last_name}"

urls.py

    path('save-form-data/', views.save_form_data, name='save_form_data'),
    path('display-form-data/', views.display_form_data, name='display_form_data'),
    path('delete-form-data/<int:form_data_id>/', views.delete_form_data, name='delete_form_data'),

this is my form

<div class="formbold-main-wrapper">
  <!-- Author: FormBold Team -->
  <!-- Learn More: https://formbold.com -->
  <div class="formbold-form-wrapper">
<form action="{% url 'save_form_data' %}" method="POST">
              {% csrf_token %}

        <div class="formbold-input-flex">
          <div>
              <input
              type="text"
              name="firstname"
              id="firstname"
              placeholder="Jane"
              class="formbold-form-input"
              />
              <label for="firstname" class="formbold-form-label"> First name </label>
          </div>
          <div>
              <input
              type="text"
              name="lastname"
              id="lastname"
              placeholder="Cooper"
              class="formbold-form-input"
              />
              <label for="lastname" class="formbold-form-label"> Last name </label>
          </div>
        </div>

        <div class="formbold-input-flex">
          <div>
              <input
              type="email"
              name="email"
              id="email"
              placeholder="jhon@mail.com"
              class="formbold-form-input"
              />
              <label for="email" class="formbold-form-label"> Mail </label>
          </div>
          <div>
              <input
              type="text"
              name="phone"
              id="phone"
              placeholder="(319) 555-0115"
              class="formbold-form-input"
              />
              <label for="phone" class="formbold-form-label"> Phone </label>
          </div>
        </div>

        <div class="formbold-textarea">
            <textarea
                rows="6"
                name="message"
                id="message"
                placeholder="Write your message..."
                class="formbold-form-input"
            ></textarea>
            <label for="message" class="formbold-form-label"> Message </label>
        </div>


            <button class="formbold-btn" type="submit">Send Message</button>

    </form>
  </div>
</div>

can anyone tell if i logout as guest also the data should be saved into the database and i didn’t kept any login required to any method in views.py so please tell me the solution to save the data

Couple of side notes here:

  • Please don’t try to describe the entire issue in the title. Use the title field for the shortest possible identification of the issue and post the complete description in the body of the post.

  • When you’re 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.

(I’ve taken the liberty of editing your original post for this.)

First, I’d suggest you restructure your code to work with the form in the common Django pattern. This means you have one view to handle your form - both the GET and POST. (See the docs and examples in Working with forms.)

This also means you should be rendering your form as a form and not as individual HTML elements.

Additionally, every time you submit this form, you’re going to create a new instance of FormData, even if the information being saved is the same as a previous submission. This means you can create duplicate records, creating a potentially confusing situation.

Side question: Have you worked your way through the Official Django Tutorial? If not, you should. Is demonstrates the Django-appropriate way of handling forms.

You’ll probably also need to provide a more detailed description of the situation you’re encountering - including more specific information about what you’re submitting and what errors may be appearing in the runserver console.