For this url, the thing is
this view is executed instead of def create_appointment()
this one, try one thing go to http://127.0.0.1:8000/create_appointment
this url in your browser and try to submit form and see if data is being saved or not
For this url, the thing is
this view is executed instead of def create_appointment()
this one, try one thing go to http://127.0.0.1:8000/create_appointment
this url in your browser and try to submit form and see if data is being saved or not
i followed this http://127.0.0.1:8000/create_appointment url, found a form there and filled it in, the data was saved in the database and i got this [27/Jun/2023 03:05:16] “POST /create_appointment/ HTTP/1.1” 302 0
[27/Jun/2023 03:05:16] “GET /success HTTP/1.1” 301 0 in my terminal, but it never redirected in browser to the /succes. also another issue i found is that there are two segments of forms. the first one has this
Appointment form date:
Appointment form time:
Appointment form full name:
Appointment form email:
Appointment form phone:
Appointment form address:
Appointment form message:
and the second one has this
SERVICE DATE AND TIME
DD/MM/YYYY
10:00 AM
PERSONAL INFO
Full name
Your email
Phone number
Your address
Your Message
the data being saved is from the first segment but the standard form must be the second segment, how do i resolve this, do i need to delet my forms.py? and also not redirecting to the success_view also note; here are models
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
and also i want to use the appointment modal in my index on url http://127.0.0.1:8000/
not http://127.0.0.1:8000/create_appointment
First to this one, update def home(request)
view method by this
def home(request):
if request.method == 'POST':
form = AppointmentForm(request.POST)
if form.is_valid():
form.save()
else:
print(form.errors)
return redirect("success_view")
else:
form = AppointmentForm()
return render(request, 'index.html', {'form': form})
I’ve included this to solve the above issue
return redirect("success_view")
this is because of your template
here you are passing {{ form.as_p }}
as well as other input also that’s why
so if i erase the {{ form.as_p }}
i will only have one segment? also, i would want to enter information using the html form and not the first segment i mentioned earlier
It depends upon what you want, {{ form.as_p }}
is for your model form and save data for that particular model but the other segment is not related to that model so you have to handle that manually
alright thank you so much for your help.
i am sorry for taking you back, the information is being saved to database correctly but still does not redirect.
my views
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_view")
else:
form = AppointmentForm()
return render(request, 'index.html', {'form': form})
In redirect, you can pass url /success
or either name success_view
from urls.
In your case this return redirect("/success")
should work
I did this and still wont work 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()
else:
print(form.errors)
return redirect("/success")
else:
form = AppointmentForm()
return render(request, 'index.html', {'form': form})
I see you still have that old code that we have discussed earlier, did you changed your url to point create_appointment()
view to /
or you have the same urls as you have shared
i still have the same code as shared