I am trying to make a function based view where the user clicks on the get quote button on the home page. Upon which they are taken to the customer page. At that point in time I want django, to evaluate if there is a quote id.
If there is not a quote id, I want django to create a new quote id. After it is created I want the user to proceed with filling out the application form and click next. Upon submission the quote will than take that quote id and have it apply to the rest of the pages. I have been told django programmers can pass information via cookies and urls. My question is this, do I need to create customer accounts and get my database involved with a query search? I am trying to fix situations in which the customer leaves the quote and returns and wants to resume from wehre they left off. when they do the quote for the first time, a quote id is generated. but than they leave and there needs to be a way to retrieve the quote id that they had generated from their previous session. a new quote id will not work.
I have a postgress database and I know django has models that store information. so is there a way to do a query search in django to get that quote id?
My project Github
urls.py
# django_insurance/quote/urls.py
from django.urls import path
from quote.views import (
AboutPageView,
HomePageView,
DriverListView,
customer_form_view,
driver_form,
vehicle_form_view,
)
urlpatterns = [
path("quote/customer/create/", customer_form_view, name="customer"),
# ... more code but not relevant. I think.
]
views.py
def customer_form_view(request):
if request.method == 'POST':
form = Customer_Form(request.POST)
# if the customer completes the form and there is no quote_id generated for them. great lets make a quote id.
if form.is_valid():
if quote_id is None:
quote_id = create_quote_id()
else:
quote_id = request.COOKIES.get("quote_id")
return redirect("driver/list/<str:quote_id>")
else:
form = Customer_Form()
return render(
request,
'customer.html',
{
'form': form,
'title' : 'Customer Form',
'quote_id' : request.COOKIES.get('quote_id'),
}
)
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/quote/customer/create/
Django Version: 5.0.7
Python Version: 3.12.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'quote']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\django_insurance\quote\views.py", line 79, in customer_form_view
form = Customer_Form()
^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\forms\models.py", line 360, in __init__
raise ValueError("ModelForm has no model class specified.")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Exception Type: ValueError at /quote/customer/create/
Exception Value: ModelForm has no model class specified.
def customer_form_view(request):
if quote_id is None:
create_quote_id()
elif quote_id is not None:
quote_id = request.COOKIES.get('quote_id')
# if the customer completes the form and there is no quote_id generated for them. great lets make a quote id.
if form.is_valid():
form = Customer_Form(request.POST)
print(f"form valid? : {form.is_valid()}")
if form.is_valid():
customer = form.save(commit=False)
customer.quote_id = quote_id
customer.save()
return redirect('f/driver/list{quote_id}')
form = Customer_Form()
return render(
request,
'driver.html',
{
'form':form,
'title':'Driver Page',
'quote_id': quote_id,
}
)
return render(
request,
'customer.html',
{
'form': form,
'title' : 'Customer Form',
'quote_id' : request.COOKIES.get('quote_id'),
}
)