Hello everyone I am keep getting this error and I don’t understand why, I want to send an email with django when user fills out the form and I want to select recipents with the form, I made a model form and when I try to select recipents I get the error. Here is my code:
models.py:
class State(models.Model):
profile = models.ForeignKey(Profile, on_delete=models.CASCADE)
country = models.CharField(max_length=100)
capital = models.CharField(max_length=100)
president = models.ManyToManyField(President)
city = models.ManyToManyField(Cities)
date = models.DateTimeField()
slug = models.SlugField(unique=True)
def save(self, *args, **kwargs):
self.slug = slugify(self.country)
super(State, self).save(*args, **kwargs)
def __str__(self):
return self.country
def get_absolute_url(self):
return reverse('mailapp2:details', args=[self.slug])
and my profile model has user object which has OneToOne relation with the django’s User model,
forms.py:
class StateForm(forms.ModelForm):
city = forms.ModelMultipleChoiceField(queryset=Cities.objects.all(), required=True)
president= forms.ModelMultipleChoiceField(queryset=President.objects.all(), required=True)
profile = forms.ModelMultipleChoiceField(queryset=Profile.objects.all(), required=True)
class Meta:
model = State
fields = ('country','capital','city', 'date', 'president','profile')
widgets = {
'country':forms.TextInput(attrs={"placeholder":"Country", "required":"True", "style":"width:40%;"}),
'capital':forms.TextInput(attrs={"placeholder":"Capital", "required":"True", "style":"width:40%;"}),
'city':forms.CheckboxSelectMultiple(attrs={"placeholder":"choose a city"}),
'date':forms.DateTimeInput(attrs={"type":"text", "onfocus":"(this.type='datetime-local')","placeholder":"Date","id":"date"}),
'president':forms.CheckboxSelectMultiple(attrs={"placeholder":"President","class":"katılımcıselect"}),
'profile':forms.CheckboxSelectMultiple(attrs={"placeholder":"Recipents","class":"recipentselect"}),
}
labels = {
'country':'country',
'capital':'capital',
'city':'city',
'date':'date',
'president':'president',
'profile':'profile'
}
(this is a dummy project to understand how django’s email backend works so the object’s names are pointless)
and my views.py:
def addState(request):
if request.method == 'POST':
form = StateForm(data=request.POST)
if form.is_valid():
date = form.cleaned_data['date']
city= form.cleaned_data['city']
profile = form.cleaned_data['profile']
new_state = form.save(commit=False)
user_profile = Profile.objects.get(user=request.user)
new_state.profile = user_profile
new_state.save()
form.save_m2m()
template = render_to_string('mailapp2/email.html', {
'name':request.user.username,
'date':date,
'city':city,
'profile':profile
})
plain_message = strip_tags(template)
email = EmailMessage(
'State Notification',
plain_message,
settings.EMAIL_HOST_USER,
[profile.user.email],
)
email.fail_silently=False
email.send()
return redirect('http://127.0.0.1:8000/')
else:
print(form.errors)
else:
form = StateForm(data=request.GET)
return render(request, 'mailapp2/add_state.html',{
'form':form
})
I cannot see where is my problem, thank you from now
PS. I translated its language so there may be little spelling errors but it is working in main project, this is why all objects are silly