I am trying to send the url of a model entry to via e-mail when my form is submitted
However when I reference my form.id like so
`'(http://localhost:8000/zipherJobCards/viewJobCard/' form.cleaned_data['id'] ')',`
I get the following error
KeyError: 'id'
Is there any right way to reference the id of an entry like this ?
Please see the full send_mail
function here
def jobCard(request ):
form = jobCardForm()
if request.method == 'POST':
form = jobCardForm(request.POST)
if form.is_valid():
form.save()
send_mail(
'ZTS JOB CARD' form.cleaned_data['jobNumber'],
'A new job card has been loaded for ' form.cleaned_data['customerName'] ' with a Total Cost of ' form.cleaned_data['totalCostOfJob'] '(http://localhost:8000/zipherJobCards/viewJobCard/' form.cleaned_data['id'] ')',
'[email protected]',
['[email protected]'],
fail_silently=False,
)
return redirect('home')
else:
print(form.errors)
content = {'form':form}
return render(request, 'main/jobCard.html', content)
CodePudding user response:
id
(or pk
) is not part of the form, it is the primary key that is set when the object is saved. You need to do obj = form.save()
, then you can reference the saved object, like obj.id
.