I am trying to use built-in send_email in a Ticket form. Everything is working fine, but when I link a field to another model via foreign key, Data doesnt get pulled. Please kindly help me in syntax.
views.py
def createTicket(request):
form = TicketForm(request.POST)
if request.method == 'POST':
if form.is_valid():
subject = "Ticket Email"
body = {
'customer': form.cleaned_data.get('customer'),
'subject': form.cleaned_data.get('subject'),
'priority': form.cleaned_data.get('priority'),
'details': form.cleaned_data.get('details'),
}
message = "\n".join(body.values())
form.save()
try:
send_mail(subject, message, 'from_email', [form.cleaned_data.get('technician_email')])
except BadHeaderError:
return HttpResponse('Invalid Header')
return redirect('/')
context = {'form': form}
return render(request, 'ticket_form.html', context)
models.py
class Ticket(models.Model):
PRIORITY = (
('normal', 'Normal'),
('urgent', 'Urgent')
)
STATUS = (
('pending', 'Pending'),
('hold', 'Hold'),
('closed', 'Closed')
)
customer = models.ForeignKey(Customer, null=True, on_delete=models.SET_NULL)
technician = models.CharField(max_length=255, null=True)
technician_email = models.CharField(max_length=255, null=True)
subject = models.CharField(max_length=255, blank=True, null=True)
priority = models.CharField(max_length=255, blank=True, null=True, choices=PRIORITY)
details = models.CharField(max_length=2000, blank=True, null=True)
class Customer(models.Model):
company_name = models.CharField(max_length=255, null=True)
Customer_type = models.ManyToManyField(Customertype, blank=True)
first_name = models.CharField(max_length=255, null=True)
last_name = models.CharField(blank=True, max_length=255, null=True)
TRACEBACK ERROR:
Environment:
Request Method: POST
Request URL: http://localhost:8000/create_ticket/
Django Version: 3.2.4
Python Version: 3.7.9
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'accounts.apps.AccountsConfig',
'django_filters',
'widget_tweaks',
'datetimewidget',
'crispy_forms']
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',
'django.middleware.locale.LocaleMiddleware']
Traceback (most recent call last):
File "D:\development\website\venv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "D:\development\website\venv\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\development\website\venv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "D:\development\demo\accounts\decorators.py", line 21, in wrapper_function
return view_function(request, *args, **kwargs)
File "D:\development\demo\accounts\views.py", line 259, in createTicket
message = "\n".join(body.values())
Exception Type: TypeError at /create_ticket/
Exception Value: sequence item 0: expected str instance, NoneType found
Is this the right way for below code to put in the message body ?
'customer': form.cleaned_data.get('customer'),
Please kindly suggest, Thanks.