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