I am trying to store phone numbers in django models and forms. Charfield and IntegerField have character problems. Integerfield will only accept values up to a certain point and charfield is a string. I don’t know of any way to validate char field. So I looked for alternatives. I came across Phone Field which is explained in this stack overflow article.
Instructions for phone field.
Did I make a mistake in setup? The error message at the bottom says
Exception Type: TemplateDoesNotExist at /quote/customer/create/
Exception Value: phone_field/phone_widget.html
The thing is I don’t have a phone_widget.html file.
I get the following traceback error:
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',
'debug_toolbar']
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']
Template loader postmortem
Django tried loading these templates, in this order:
Using engine django:
* django.template.loaders.filesystem.Loader: E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\forms\templates\phone_field\phone_widget.html (Source does not exist)
* django.template.loaders.app_directories.Loader: E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\contrib\admin\templates\phone_field\phone_widget.html (Source does not exist)
* django.template.loaders.app_directories.Loader: E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\contrib\auth\templates\phone_field\phone_widget.html (Source does not exist)
* django.template.loaders.app_directories.Loader: E:\programming\django\django_projects\django_insurance\django_insurance\quote\templates\phone_field\phone_widget.html (Source does not exist)
* django.template.loaders.app_directories.Loader: E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\debug_toolbar\templates\phone_field\phone_widget.html (Source does not exist)
Template error:
In template E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\forms\templates\django\forms\p.html, error at line 9
phone_field/phone_widget.html
1 : {{ errors }}
2 : {% if errors and not fields %}
3 : <p>{% for field in hidden_fields %}{{ field }}{% endfor %}</p>
4 : {% endif %}
5 : {% for field, errors in fields %}
6 : {{ errors }}
7 : <p{% with classes=field.css_classes %}{% if classes %} class="{{ classes }}"{% endif %}{% endwith %}>
8 : {% if field.label %}{{ field.label_tag }}{% endif %}
9 : {{ field }}
10 : {% if field.help_text %}
11 : <span class="helptext"{% if field.auto_id %} id="{{ field.auto_id }}_helptext"{% endif %}>{{ field.help_text|safe }}</span>
12 : {% endif %}
13 : {% if forloop.last %}
14 : {% for field in hidden_fields %}{{ field }}{% endfor %}
15 : {% endif %}
16 : </p>
17 : {% endfor %}
18 : {% if not fields and not errors %}
19 : {% for field in hidden_fields %}{{ field }}{% endfor %}
Traceback (most recent call last):
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\backends\django.py", line 33, in get_template
return Template(self.engine.get_template(template_name), self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\engine.py", line 177, in get_template
template, origin = self.find_template(template_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\engine.py", line 163, in find_template
raise TemplateDoesNotExist(name, tried=tried)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The above exception (phone_field/phone_widget.html) was the direct cause of the following exception:
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\test\utils.py", line 111, in instrumented_test_render
return self.nodelist.render(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\defaulttags.py", line 242, in render
nodelist.append(node.render_annotated(context))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 1065, in render
return render_value_in_context(output, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 1042, in render_value_in_context
value = str(value)
^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\forms\utils.py", line 79, in __str__
return self.as_widget()
^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\forms\boundfield.py", line 108, in as_widget
return widget.render(
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\forms\widgets.py", line 279, in render
return self._render(self.template_name, context, renderer)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\forms\widgets.py", line 284, in _render
return mark_safe(renderer.render(template_name, context))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\forms\renderers.py", line 28, in render
template = self.get_template(template_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\forms\renderers.py", line 34, in get_template
return self.engine.get_template(template_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\backends\django.py", line 35, in get_template
reraise(exc, self)
^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\backends\django.py", line 84, in reraise
raise new from exc
^^^^^^^^^^^^^^^^^^
The above exception (phone_field/phone_widget.html) was the direct cause of the following exception:
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\test\utils.py", line 111, in instrumented_test_render
return self.nodelist.render(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\loader_tags.py", line 159, in render
return compiled_parent._render(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\test\utils.py", line 111, in instrumented_test_render
return self.nodelist.render(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\loader_tags.py", line 65, in render
result = block.nodelist.render(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 1000, in render
return SafeString("".join([node.render_annotated(context) for node in self]))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 961, in render_annotated
return self.render(context)
^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 1059, in render
output = self.filter_expression.resolve(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 710, in resolve
obj = self.var.resolve(context)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 842, in resolve
value = self._resolve_lookup(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\base.py", line 909, in _resolve_lookup
current = current()
^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\forms\utils.py", line 87, in as_p
return self.render(self.template_name_p)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\forms\utils.py", line 55, in render
return mark_safe(renderer.render(template, context))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\forms\renderers.py", line 29, in render
return template.render(context, request=request).strip()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\backends\django.py", line 63, in render
reraise(exc, self.backend)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\backends\django.py", line 84, in reraise
raise new from exc
^^^^^^^^^^^^^^^^^^
The above exception (phone_field/phone_widget.html) was the direct cause of the following exception:
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 220, in _get_response
response = response.render()
^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\response.py", line 114, in render
self.content = self.rendered_content
^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\response.py", line 92, in rendered_content
return template.render(context, self._request)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\backends\django.py", line 63, in render
reraise(exc, self.backend)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\programming\django\django_projects\django_insurance\venv\Lib\site-packages\django\template\backends\django.py", line 84, in reraise
raise new from exc
^^^^^^^^^^^^^^^^^^
Exception Type: TemplateDoesNotExist at /quote/customer/create/
Exception Value: phone_field/phone_widget.html
models.py
from django.db import models
from quote import constants
from phone_field import PhoneField
from phonenumber_field.modelfields import PhoneNumberField
# makemigrations and migrate from manage.py
# Create your models here.
# Tie everything with Quote ID
# customer model
class Customer(models.Model):
# columns would be the variables
first_name = models.CharField(max_length=50)
middle_name = models.CharField(max_length=50, blank=True)
last_name = models.CharField(max_length=50)
suffix = models.CharField(blank=True, max_length=10)
apt_number = models.CharField(blank=True, max_length=10)
date_of_birth = models.DateField()
address = models.CharField(max_length=50)
zip_code = models.CharField(default=00000, max_length=10)
telephone_number = PhoneField(blank=True, help_text='Contact phone number')
email_address = models.CharField(max_length=50)
quote_id = models.CharField(max_length=11, blank=True)
home_ownership_options = (("OWN", "Owns_Property"), ("RENT", "Rents_Property"))
home_ownership = models.CharField(max_length=50, choices=home_ownership_options)
# credit
# vehicle model
#
class Vehicle(models.Model):
Vehicle_Identification_Number = models.CharField(max_length=30)
# Enumerate Technique The first item in the tuple is what will be listed in the database.
# The second item in the tuple is what will display for the user.
Usage_Type_Options = (
("PLEASURE", "Pleasure Vehicle"),
("WORK", "Work/School"),
("BUSINESS", "Business"),
("COMMERCIAL", "Commercial"),
)
# Note: Use for ride sharing ideally should be like a check box
Used_For_Ride_Sharing = models.CharField(max_length = 10, blank=True)
Usage_Type = models.CharField(choices=Usage_Type_Options, max_length=11, blank=True)
Annual_Mileage_Options = (
("0 - 3,999", "0 - 3,999"),
("4,000 - 5,999","4,000 - 5,999"),
("6,000 - 7,999", "6,000 - 7,999"),
("8,000 - 9,999", "8,000 - 9,999"),
("10,000 - 11,999", "10,000 - 11,999"),
("12,000 - 13,999", "12,000 - 13,999"),
("14,000 - 15,999", "14,000 - 15,999"),
("16,000 - 17,999", "16,000 - 17,999"),
("18,000 - 19,999", "18,000 - 19,999"),
("20,000 OR MORE", "20,000 or more"),
)
Annual_Mileage = models.CharField(choices=Annual_Mileage_Options, max_length=15)
Year = models.IntegerField()
Make = models.CharField(max_length=30)
quote_id = models.CharField(max_length=11, blank=True)
Model = models.CharField(max_length=30)
Ownership_Options = (
("FINANCE", "Finance"),
("OWN", "Own"),
("LEASE", "Lease"),
)
Vehicle_Ownership = models.CharField(choices=Ownership_Options, max_length=11)
Vehicle_Ownership_Timeframe_Options = (
("LESS THAN 1 MONTH", "Less than 1 month"),
("1 TO 6 MONTHS", "1 to 6 months"),
("6 MONTHS TO 1 YEAR", "6 months to 1 year"),
("1 YEAR TO 3 YEARS", "1 year to 3 years"),
("3 YEARS TO 5 YEARS", "3 years to 5 years"),
("5 YEARS OR MORE", "5 years or more"),
)
Vehicle_Ownership_Timeframe = models.CharField(max_length=15)
class Driver(models.Model):
driver_first_name = models.CharField(max_length=50, null=True)
driver_last_name = models.CharField(max_length=50, null=True)
driver_relation = models.CharField(max_length=16, choices=constants.DRIVER_RELATION_OPTIONS, default="SELF", null=True)
quote_id = models.CharField(max_length=11, blank=True)
drivers_license_state = models.CharField(choices=constants.STATE_OPTIONS, max_length=30, null=True)
drivers_license_number = models.CharField(max_length=30, null=True)
drivers_license_status = models.CharField(choices=constants.DRIVERS_LICENSE_STATUS, max_length=50, null=True)
gender = models.CharField(choices=constants.GENDER_OPTIONS, max_length=30)
date_of_issuance = models.DateField()
job_status = models.CharField(max_length=50, choices=constants.JOB_STATUS_OPTIONS)
education = models.CharField(choices=constants.EDUCATION_OPTIONS, max_length=35)
affilation_options = (("DFW_PYTHONEERS", "Dfw_Pythoneers"),
("PY_TEXAS", "Py_Texas"),)
affilation = affilation_options
# many to many relationship QUOTE to PRODUCT
# state of texas requirements (LIABLITY,PIP,)
class Product(models.Model):
product_type_options = (
("LIABILITY", "Liability"),
("RECCOMENDED_COVERAGE", "Reccomended Coverage"),
("CUSTOM", "Custom"),
)
coverage_options = (("FULL_COVERAGE", "Full Coverage"),)
# adding products to customers
# if not defined in a table before than you must use quotes for the model not defined.
# since everything is already defined already we do not need to worry about that.
class Quote(models.Model):
Customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
Vehicle = models.ForeignKey(Vehicle, on_delete=models.CASCADE)
Driver = models.ForeignKey(Driver, on_delete=models.CASCADE)
Reference_Number = models.CharField(max_length=30)
Price = models.DecimalField(max_digits=7, decimal_places=2)
# url for vehcile we want to build a vehicle or for this quote
forms.py
from django import forms
from quote.models import Driver, Vehicle
from phonenumber_field.modelfields import PhoneNumberField
class Customer_Form(forms.Form):
first_name = forms.CharField(
max_length=20, label="What is your first name?", required=True
)
middle_name = forms.CharField(
max_length=20, label="What is your middle name?"
)
last_name = forms.CharField(
max_length=20, label="What is your last name?", required=True
)
suffix = forms.CharField(
max_length=5, label="If you have a suffix in your name what is it?", required=False
)
address = forms.CharField(
max_length=50, label="What is your address?", required=True
)
zip_code = forms.IntegerField(label="What is your zip code?")
# dashes between the number
telephone_number = PhoneNumberField()
email = forms.EmailField(
max_length=50, required=False, label="What is your email address?"
)
date_of_birth = forms.DateField(required=True, label="What is your date of birth?")
home_ownership = forms.CharField(label="Do you own or rent your home?")
class VehicleForm(forms.ModelForm):
class Meta:
model = Vehicle
fields = [
"Vehicle_Identification_Number",
"Usage_Type",
"Year",
"Make",
"Model",
]
quote_id = forms.CharField(
widget=forms.HiddenInput(),
required=False
)
class DriverForm(forms.ModelForm):
class Meta:
model = Driver
fields = [
"driver_first_name",
"driver_last_name",
"driver_relation",
"drivers_license_number",
"drivers_license_status",
'drivers_license_state',
"date_of_issuance",
"job_status",
"gender",
"education",
]
widgets = {
'date_of_issuance': forms.widgets.DateInput(attrs={'type': 'date'})
}
quote_id = forms.CharField(
widget=forms.HiddenInput(),
required=False
)