unable to save data to database and redirect

i was unabled to post data to my database using my form. the form doesnt redirect me when i enter data and submit even though i believe I have configured the form correctly. here is what i get in my terminal

There is not enough information regarding the issue you are asking, give more info like your views code, html code or forms code if you are using django forms.

my views
‘’’
from pyexpat.errors import messages
from django.shortcuts import redirect, render
from django.http import HttpResponse

from .forms import AppointmentForm
from .models import *
from django.core.mail import send_mail
from datetime import datetime
from django import forms
from django.core.exceptions import ValidationError
from django.utils.dateparse import parse_date, parse_time
from django.http import HttpResponseRedirect

def home(request):
return render(request,‘index.html’)

def about(request):
return render(request,‘about.html’)

def services(request):
return render(request,‘services.html’)

def pricing(request):
return render(request,‘pricing.html’)

def contact(request):
return render(request,‘contact.html’)

def success_view(request):
return render(request, ‘success.html’)

def create_appointment(request):
if request.method == ‘POST’:
form = AppointmentForm(request.POST)
if form.is_valid():
form.save()
# Log the form data
print(form.cleaned_data)
return render(request, ‘success_view’)
else:
form = AppointmentForm()

return render(request, 'index.html', {'form': form})

‘’’
my models.py
‘’’
from django.db import models

class Appointment(models.Model):
appointment_form_date = models.DateTimeField(blank=True, null=True)
appointment_form_time = models.TimeField()
appointment_form_full_name = models.CharField(max_length=100)
appointment_form_email = models.EmailField()
appointment_form_phone = models.CharField(max_length=20, null=True)
appointment_form_address = models.CharField(max_length=100)
appointment_form_message = models.TextField()

def __str__(self):
    return self.appointment_form_full_name

‘’’
my forms.py
from django import forms
from django.forms import ModelForm
from .models import Appointment

class AppointmentForm(forms.ModelForm):
class Meta:
model = Appointment
fields = ‘all
‘’’
my html
‘’’

×

Appointment For Hair Color

{% csrf_token %} {{ form.as_p }}
Service Date and Time
      <div class="timeSelect appointment-timeSelect form-half clearfix">
        <select id="guiest_id1" name="appointment-form-time" class="select-drop">
          <option value="0">10:00 AM</option>
          <option value="1">9:00 AM</option>
          <option value="2">8:00 AM</option>
          <option value="3">11:00 AM</option>
        </select>
      </div>

      <div class="form-group categoryTitle">
        <h5>Personal info</h5>
      </div>
      <div class="form-group form-half form-left">
        <input type="text" id="appointment-form-full-name" name="appointment-form-full-name" class="form-control" placeholder="Full name" required>
      </div>
      <div class="form-group form-half form-right">
        <input type="email" id="appointment-form-email" name="appointment-form-email" class="form-control" placeholder="Your email" required>
      </div>
      <div class="form-group form-half form-left">
        <input type="text" id="appointment-form-phone" name="appointment-form-phone"  class="form-control" placeholder="Phone number" required>
      </div>
      <div class="form-group form-half form-right">
        <input type="text" id="appointment-form-address" name="appointment-form-address" class="form-control" placeholder="Your address" required>
      </div>
      <div class="form-group">
        <textarea class="form-control" id="appointment-form-message" name="appointment-form-message"  placeholder="Your Message" required></textarea>
      </div>
      <div class="form-group">
        <button type="submit" id="appointment-submit-btn" class="btn btn-primary first-btn">Submit Now</button>
      </div>
    </form>
  </div>
</div>

‘’’

In forms.py fields = 'all’ instead of this use fields = '__all__’
Side note: use triple backtick to share code snippet

from pyexpat.errors import messages
from django.shortcuts import redirect, render
from django.http import HttpResponse

from .forms import AppointmentForm
from .models import *
from django.core.mail import send_mail
from datetime import datetime
from django import forms
from django.core.exceptions import ValidationError
from django.utils.dateparse import parse_date, parse_time
from django.http import HttpResponseRedirect




# Create your views here.
def home(request):
    return render(request,'index.html')

def about(request):
    return render(request,'about.html')

def services(request):
    return render(request,'services.html')

def pricing(request):
    return render(request,'pricing.html')

def contact(request):
    return render(request,'contact.html')

def success_view(request):
    return render(request, 'success.html')      



def create_appointment(request):
    if request.method == 'POST':
        form = AppointmentForm(request.POST)
        if form.is_valid():
            form.save()
            # Log the form data
            print(form.cleaned_data)
            return render(request, 'success_view')
    else:
        form = AppointmentForm()

    return render(request, 'index.html', {'form': form})
my models.py
from django.db import models

class Appointment(models.Model):
    appointment_form_date = models.DateTimeField(blank=True, null=True)
    appointment_form_time = models.TimeField()
    appointment_form_full_name = models.CharField(max_length=100)
    appointment_form_email = models.EmailField()
    appointment_form_phone = models.CharField(max_length=20, null=True)
    appointment_form_address = models.CharField(max_length=100)
    appointment_form_message = models.TextField()

    def __str__(self):
        return self.appointment_form_full_name
forms.py
from django import forms
from django.forms import ModelForm
from .models import Appointment

class AppointmentForm(forms.ModelForm):
    class Meta:
        model = Appointment
        fields = '__all__'

html

<!-- APPOINMENT MODAL -->
<div id="appoinmentModal" class="modal fade modalCommon" role="dialog" tabindex="-1">
  <div class="modal-dialog">
    <!-- MODAL CONTENT-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title appointment-modal-title">Appointment For Hair Color</h4>
      </div>
      <div class="modal-body">
      <div id="appointment-alert" class="my-alert"></div>
      <form action="{% url 'create_appointment' %}" method="post" id="appoinmentModalForm">
            {% csrf_token %}
            {{ form.as_p }}
          <!--Response Holder-->
          <div class="form-group categoryTitle">
            <h5>Service Date and Time</h5>
          </div>
          <div class="dateSelect custom-dateSelect">
            <div class="input-group date ed-datepicker filterDate" data-provide="datepicker">
              <input type="text" class="form-control" placeholder="DD/MM/YYYY">
              <div class="input-group-addon">
                <span class="fa fa-calendar"></span>
              </div>
            </div>
          </div>
          

          <div class="timeSelect appointment-timeSelect form-half clearfix">
            <select id="guiest_id1" name="appointment-form-time" class="select-drop">
              <option value="0">10:00 AM</option>
              <option value="1">9:00 AM</option>
              <option value="2">8:00 AM</option>
              <option value="3">11:00 AM</option>
            </select>
          </div>

          <div class="form-group categoryTitle">
            <h5>Personal info</h5>
          </div>
          <div class="form-group form-half form-left">
            <input type="text" id="appointment-form-full-name" name="appointment-form-full-name" class="form-control" placeholder="Full name" required>
          </div>
          <div class="form-group form-half form-right">
            <input type="email" id="appointment-form-email" name="appointment-form-email" class="form-control" placeholder="Your email" required>
          </div>
          <div class="form-group form-half form-left">
            <input type="text" id="appointment-form-phone" name="appointment-form-phone"  class="form-control" placeholder="Phone number" required>
          </div>
          <div class="form-group form-half form-right">
            <input type="text" id="appointment-form-address" name="appointment-form-address" class="form-control" placeholder="Your address" required>
          </div>
          <div class="form-group">
            <textarea class="form-control" id="appointment-form-message" name="appointment-form-message"  placeholder="Your Message" required></textarea>
          </div>
          <div class="form-group">
            <button type="submit" id="appointment-submit-btn" class="btn btn-primary first-btn">Submit Now</button>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>


I am sorry for not adding the proper quotes. this is my code with the quotes

In this part of code are you getting the print statement print(form.cleaned_data) executed like are you getting cleaned_data in terminal?

no i don’t get the cleaned data in my terminal

so after if condition put else condition which is like

else:
    print(form.errors)

and see if it gives anything

after changing that,

def create_appointment(request):
    if request.method == 'POST':
        form = AppointmentForm(request.POST)
        if form.is_valid():
            form.save()
        
        else:
         print(form.errors)
   
        return render(request, 'success_view')
    else:
        form = AppointmentForm()

    return render(request, 'index.html',{'form': form})

I get File “C:\Users\Mukelabai\Desktop\EXPERIENCE\env\Lib\site-packages\django\core\handlers\base.py”, line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Mukelabai\Desktop\EXPERIENCE\cl\myapp\views.py”, line 47, in create_appointment
return render(request, ‘success_view’)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Mukelabai\Desktop\EXPERIENCE\env\Lib\site-packages\django\shortcuts.py”, line 24, in render
content = loader.render_to_string(template_name, context, request, using=using)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Mukelabai\Desktop\EXPERIENCE\env\Lib\site-packages\django\template\loader.py”, line 61, in render_to_string
template = get_template(template_name, using=using)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Mukelabai\Desktop\EXPERIENCE\env\Lib\site-packages\django\template\loader.py”, line 19, in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: success_view
[27/Jun/2023 00:26:04] “POST /create_appointment/ HTTP/1.1” 500 80167

fix the indentation for this

You are rendering this form to index.html, are you opening the APPOINMENT MODAL at index.html. If yes than instead of action="{% url 'create_appointment' %}" this, try action="." this

html

<!-- APPOINMENT MODAL -->
<div id="appoinmentModal" class="modal fade modalCommon" role="dialog" tabindex="-1">
  <div class="modal-dialog">
    <!-- MODAL CONTENT-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title appointment-modal-title">Appointment For Hair Color</h4>
      </div>
      <div class="modal-body">
      <div id="appointment-alert" class="my-alert"></div>
      <form action="." method="post" id="appoinmentModalForm">
            {% csrf_token %}
            {{ form.as_p }}
          <!--Response Holder-->
          <div class="form-group categoryTitle">
            <h5>Service Date and Time</h5>
          </div>
          <div class="dateSelect custom-dateSelect"
views.py
def create_appointment(request):
    if request.method == 'POST':
        form = AppointmentForm(request.POST)
        if form.is_valid():
            form.save()
        
        else:
         print(form.errors)
   
        return render(request, 'success_view')
    else:
     form = AppointmentForm()

    return render(request, 'index.html', {'form': form})

i am getting this in my terminal [27/Jun/2023 00:54:42,961] - Broken pipe from (‘127.0.0.1’, 50171)
[27/Jun/2023 00:54:42] “GET / HTTP/1.1” 200 145428
[27/Jun/2023 00:58:37] “POST / HTTP/1.1” 200 145428 and after clicking submit, it does not redirect

In your view success_view what does it refers to, also you can use redirect instead of render if form is submitted like return redirect(".") to redirect at same page.

success_view is a view function that contains success.html

also, is this what you mean? by return redirect(".") to redirect at same page.

def create_appointment(request):
    if request.method == 'POST':
        form = AppointmentForm(request.POST)
        if form.is_valid():
            form.save()
        
        else:
         print(form.errors)
   
        return redirect(".")
    else:
     form = AppointmentForm()

    return render(request, 'index.html', {'form': form})

If this is a view then pass the url that you have created for this view like redirect("/success")

Yes

def create_appointment(request):
    if request.method == 'POST':
        form = AppointmentForm(request.POST)
        if form.is_valid():
            form.save()
        
        else:
         print(form.errors)
   
        return redirect("/success")
    else:
     form = AppointmentForm()

    return render(request, 'index.html', {'form': form})

is this how you mean?

You didn’t get the point.
You have created this success_view view and for that in urls.py you might have created a url in urlpatterns and pointed success_view to that particular url. I’m just giving example for this redirect("/success"). You have to use that url that you have created for success_view

if it is, when i runserver and fill in the form and click submit, I get
[27/Jun/2023 02:38:59] “GET / HTTP/1.1” 200 145428
[27/Jun/2023 02:39:07] “GET / HTTP/1.1” 200 145428
[27/Jun/2023 02:39:20] “GET / HTTP/1.1” 200 145428
[27/Jun/2023 02:40:21] “POST / HTTP/1.1” 200 145428 in my browser, I navigated to admin panel but in Appointments, nothing is being saved

okay I get you now and that’s the url in my urls.py. here are my urls

from django import views
from django.urls import path
from .views import *






urlpatterns = [
    path('', home, name='home'),
    path('about/', about, name='about'),
    path('services/', services, name='services'),
    path('pricing/', pricing, name='pricing'),
    path('contact/', contact, name='contact'),
    path('success/', success_view, name='success_view'),
    path('create_appointment/', create_appointment, name='create_appointment'),
    
   
]

I’ve seen in views,py for both home and create_appointment views you are using index.html, In your browser what is the url when you open the form within Modal template like is it http://127.0.0.1:8000/ or http://127.0.0.1:8000/create_appointment

it is http://127.0.0.1:8000/