Django form not saving or emailing as it should - django

I've built a simple form that should send an email on form submit, as well as save to a table in the database. I've done this by following different tutorials, as I am still learning Django. At the moment, it should be configured to output the email to the console rather than actually send an email, as per the tutorial I followed.
I don't get any errors and the console shows a HTTP 200 success code when the form is posted, so it seems to be working, but since nothing is saved and no email is printed to the console, I'm not sure what is going on.
My models.py:
from __future__ import unicode_literals
from django.db import models
from multiselectfield import MultiSelectField
class Booking(models.Model):
timeslot = (
('EM', 'Early Morning'),
('LM', 'Late Morning'),
('EA', 'Early Afternoon'),
('LA', 'Late Afternoon'),
)
services = (
('gutters', 'Gutter Cleaning'),
('windows', 'Window Cleaning'),
('decks', 'Deck Staining'),
('painting', 'Painting'),
)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.CharField(max_length=30)
phone = models.CharField(max_length=30)
booking_time = models.CharField(max_length=1, choices=timeslot)
selected_services = MultiSelectField(choices=services)
The relevant part of my views.py:
def booking_new(request):
form_class = BookingForm
# new logic!
if request.method == 'POST':
form = form_class(data=request.POST)
if form.is_valid():
first_name = request.POST.get(
'first_name'
, '')
last_name = request.POST.get(
'last_name'
, '')
email = request.POST.get(
'email'
, '')
phone = request.POST.get(
'phone'
, '')
booking_time = request.POST.get(
'booking_time'
, '')
selected_services = request.POST.get(
'selected_services'
, '')
form_content = request.POST.get('content', '')
template = get_template('email_template.txt')
context = {
'first_name': first_name,
'last_name': last_name,
'email': email,
'form_content': form_content,
}
content = template.render(context)
email = EmailMessage(
"New contact form submission",
content,
"Your website" +'',
['youremail#gmail.com'],
headers = {'Reply-To': contact_email }
)
email.send()
form.save()
return redirect('booking_new')
return render(request, 'home/booking_edit.html', {
'form': form_class,
})
My forms.py:
from django import forms
from .models import Booking
class BookingForm(forms.ModelForm):
class Meta:
model = Booking
fields = ('first_name', 'last_name', 'email', 'phone', 'booking_time', 'selected_services')
And the end of my settings.py:
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
DEFAULT_FROM_EMAIL = 'testing#example.com'
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = False
EMAIL_PORT = 1025
I'm not getting any errors about missing imports or URLs or templates etc, so I think I have included all the relevant info. As far as I can see everything is configured correctly.
Why when my form is submitted is no email sent, and no record saved to the database?

the console shows a HTTP 200 success code
This suggests that the form is not valid. If the form was valid, you would redirect after sending the email, so you would see status code 302.
In the view, you should pass the form to the template instead of the form class. That way you should see any errors rendered in the form template (assuming you use {{ form }}).
if request.method == 'POST':
form = form_class(data=request.POST)
...
else:
form = form_class() # Blank form for GET request
return render(request, 'home/booking_edit.html', {
'form': form,
})
You may also find it useful to add print statements (e.g. print(form.is_valid()), print(form.errors). The output in the console will help you debug what is happening when the view runs.

Related

Django - Keep specific fields on form after submit

I have a view that has a simple "save and add another" functionality, that redirects the user to the same page after submit the form.
View:
def new_planning(request):
form = PlanningForm(request.POST)
if form.is_valid():
form.save()
if 'another' in request.POST:
messages.success(request, ('Success!'))
return redirect('new_planning')
else:
return redirect('list_planning')
return render(request, 'pages/planning/new_planning.html', context={
'form': form,
})
Form:
class PlanningForm(forms.ModelForm):
accountplan = ModelChoiceField(
queryset=AccountsPlan.objects.filter(active=True).order_by('code'),
)
month = forms.DateField(
required=True,
error_messages={'required': '', },
)
amount = forms.DecimalField(
max_digits=9,
decimal_places=2,
required=True,
validators=[
error_messages={'required': '', },
)
class Meta:
model = Planning
fields = '__all__'
The function works as expected and after the submit, the same page is rendered with a blank form. What I want is to keep just the "amount" field blank and keep the data typed in the "accountplan" and "month" fields. Is there a way to do this?
I read about instance in the docs, but it doesn't seem to be what I looking for, since I don't want to get the data from the database (if that's possible), but simply keep the last inputs typed in both fields.
If you rewrite the "ModelForm" to a "Model" class, you can get the values of the posted datas, and can be rendered to the page.
For example:
# views.py
def ContactPageView(request):
if request.method == "POST":
email = request.POST.get('email')
message = request.POST.get('message')
message_element = ContactFormObject(email=email, message=message)
message_element.save()
else:
name, message = '', ''
form_data = name, message
return render(request, 'contact.html', {'form_data': form_data})
# models.py
class ContactFormObject(models.Model):
email = models.CharField(max_length=100) #....
ModelForm is more comfortable, but I don't recommend it if you have extra 10 minutes to code some HTML in order to the possibilities of more customization.

Send to different emails based on ChoiceField

I want to have my contact form send to different emails based on the department the user chooses. Right now I'm just trying to print the department the user has picked but I'd like to set up some conditionals to send to different emails based on the department value.
This gives me KeyError at /contact/ 'DEALERSHIP ENQUIRIES'
forms.py
from django import forms
class ContactForm(forms.Form):
DEPARTMENT_CHOICES = (
('SALES ENQUIRIES', 'Sales Enquiuries'),
('DEALERSHIP ENQUIRIES', 'Dealership Enquiuries'),
('JOB ENQUIRIES', 'Job Enquiuries'),
('ALL OTHER QUESTIONS', 'All Other Questions'),
)
name = forms.CharField(max_length=255)
sender_email = forms.CharField(max_length=255)
phone = forms.CharField(max_length=255)
subject = forms.CharField(max_length=255)
department = forms.ChoiceField(choices = DEPARTMENT_CHOICES, widget=forms.Select(), required=True)
message = forms.CharField(required=True, widget=forms.Textarea(attrs={'rows': 8}))
views.py
def contact_view(request):
form = ContactForm
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
subject = f'Message from {form.cleaned_data["name"]}'
message = form.cleaned_data["message"]
sender = form.cleaned_data["sender_email"]
phone = form.cleaned_data["phone"]
department = form.cleaned_data["department"]
print(form.cleaned_data[department])
context = {
"form": form,
}
return render(request=request, template_name='main/contact.html', context=context)

Test UpdateView for useraccounts application

Test doesn't give status_code 302 in user profile UpdateView and so there's no updates occurs on the object
the model code
class User(AbstractBaseUser, PermissionsMixin):
'''
This a replaced user profile instead of the default django one
'''
language_choices=[('en',_('English')),('se',_('Swedish'))]
email=models.CharField(verbose_name=_('Email'), max_length=128, blank=False, unique=True)
first_name=models.CharField(verbose_name=_('First Name'), max_length=128)
last_name=models.CharField(verbose_name=_('Last Name'), max_length=128)
joined_at=models.DateField(
verbose_name=_('Joined at'),
auto_now_add=True,
blank=False
)
language=models.CharField(
verbose_name=_('Language'),
max_length=2,
choices=language_choices,
default=language_choices[0][0]
)
active=models.BooleanField(verbose_name=_('Active'), default=False)
is_superuser=models.BooleanField(verbose_name=_('Is Superuser'), default=False)
is_active=models.BooleanField(verbose_name=_('Is Active'), default=True)
is_staff=models.BooleanField(verbose_name=_('Is Staff'), default=False)
The form code
class EditUserForm(UserChangeForm):
'''
Profile form to update existing user information
'''
# error message for email matches
error_messages = {
'email_mismatch': _("The two email fields didn't match."),
}
# create field for email
email1 = forms.EmailField(
label=_("Email"),
widget=forms.EmailInput,
help_text=_("If you change your email your account will be inactive untill your reactivate by email link."),
)
# get the email from confirmed email field
email2 = forms.EmailField(
label=_("Confirm Email"),
widget=forms.EmailInput,
help_text=_("Enter the same email as before, for verification."),
)
# hide password field
password = ReadOnlyPasswordHashField(label="Password")
class Meta:
'''
Initial fields and model for the form
'''
model = models.User
fields = ('first_name','last_name','email1','email2', 'language')
def clean_email2(self):
'''
Method for if email and confirmed email are the same
This method works when confirmed email cleared
'''
# get the email from email field
email1 = self.cleaned_data.get("email1")
# get the email from confirmed email field
email2 = self.cleaned_data.get("email2")
# check if both emails are equal
if email1 and email2 and BaseUserManager.normalize_email(email1) != BaseUserManager.normalize_email(email2):
# give an error message if emails not matches
raise forms.ValidationError(
self.error_messages['email_mismatch'],
code='email_mismatch')
# return the confirmed email
return BaseUserManager.normalize_email(email2)
def save(self, commit=True):
'''
Method tosave the edited user data
'''
# get the initial method
user = super().save(commit=False)
# set the email on the model field
user.email = self.cleaned_data["email1"]
# save edited user data
if commit:
user.save()
return user
def __init__(self, *args, **kwargs):
'''
Method for initial values and functions for the SignUp form class
'''
# get user data from User model
user = get_user_model().objects.get(email=kwargs['instance'])
# get the initial form class values
super(EditUserForm, self).__init__(*args, **kwargs)
# Add the current email as the inital email
self.fields['email1'].initial = user.email
# Add the current email as the intial confirmed email
self.fields['email2'].initial = user.email
# Add help text in the password field for change
self.fields['password'].help_text=(
_("Raw passwords are not stored, so there is no way to see "
"this user's password, but you can change the password "
"using this form.")
.format(reverse(
'core:ChangePassword',
kwargs={'pk':user.pk})))
and the view code
class EditUser(UserPassesTestMixin, UpdateView):
'''
Class view to update user details
'''
# used template
template_name = 'core/edit.html'
# View model
model = models.User
# View form
form_class = forms.EditUserForm
def test_func(self):
return self.request.user == get_user_model().objects.get(pk=self.kwargs['pk'])
def get_success_url(self):
'''
Metho to redirect after a valid form
'''
# check if the email is verified
if self.request.user.active:
# get the user key
pk=self.request.user.pk
# redirect to profile details
return reverse_lazy('core:details', kwargs={'pk':pk})
else:
# send a verification email
return SendActivationEmail(self.request, self.request.user)
the test code
self.viewuser_url = reverse('core:details', kwargs={'pk':self.user.pk})
self.edituser_url = reverse('core:edit', kwargs={'pk':self.user.pk})
def test_edit_user_post(self):
first_name = 'Osama'
response = self.client.post(self.edituser_url,
data={
'first_name': first_name,
'last_name': self.last_name,
'email': self.email,
})
self.assertRedirects(response, self.viewuser_url)
self.user.refresh_from_db()
self.assertEqual(self.user.first_name, first_name)
I tried to get assertEqual for the status code and it gitves me 200 instead of 302
also I tried to enter the form details instead of model details and it gives me an error
The get test works fine and also permission test works great.. all the models, forms and urls test works perfect.
I don't know how I can test this..
If the form isn't valid, then the form will be re-rendered with errors and you'll get a 200 response.
To debug the problem, check response.context['form'].errors in your test to see what the problem is.
response = self.client.post(self.edituser_url,
data={
'first_name': first_name,
'last_name': self.last_name,
'email': self.email,
})
print(response.context['form'].errors
Your view uses EditUserForm, but you are not posting any values for email1 or email2, so there is probably something in the errors about missing data.

Django: send email on form submition

I need to send an email after user submits a form on my page (this is still in development, but it'll be in production soon).
I've read this other answer but after my form is submitted I'm not reciving any email (I've used my personal email as the sender and the receiver, for testing).
What I need to modify?
PLUS: How to send images on emails? Any tutorial on this? I need to send professional emails after form submition.
settings.py
EMAIL_HOST = 'smtp.gmail.com' # since you are using a gmail account
EMAIL_PORT = 587 # Gmail SMTP port for TLS
EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'oma.oma#gmail.com' #Not my actual email
EMAIL_HOST_PASSWORD = 'MyPassword' #Not my actual password
views.py:
# here we are going to use CreateView to save the Third step ModelForm
class StepThreeView(CreateView):
form_class = StepThreeForm
template_name = 'main_app/step-three.html'
success_url = '/'
def form_valid(self, form):
form.instance.tamanios = self.request.session.get('tamanios') # get tamanios from session
form.instance.cantidades = self.request.session.get('cantidades') # get cantidades from session
del self.request.session['cantidades'] # delete cantidades value from session
del self.request.session['tamanios'] # delete tamanios value from session
self.request.session.modified = True
return super(StepThreeView, self).form_valid(form)
form.py:
from django.core.mail import send_mail
class StepThreeForm(forms.ModelForm):
instrucciones = forms.CharField(widget=forms.Textarea)
class Meta:
model = TamaniosCantidades
fields = ('imagenes', 'instrucciones')
def __init__(self, *args, **kwargs):
super(StepThreeForm, self).__init__(*args, **kwargs)
self.fields['instrucciones'].required = False
def send_email(self):
send_mail('Django Test', 'My message', 'oma.oma#gmail.com',
['oma.oma#gmail.com'], fail_silently=False)
You can override the save method like this in your Last Form:
class StepThreeForm(forms.ModelForm):
def save(self, commit=True):
instance = super(StepThreeForm, self).save(commit=commit)
self.send_email()
return instance
def send_email(self):
image = self.cleaned_data.get('imagenes', None)
msg = EmailMessage(
'Hello',
'Body goes here',
'from#example.com',
['to1#example.com', 'to2#example.com'],
headers={'Message-ID': 'foo'},
)
msg.content_subtype = "html"
if image:
mime_image = MIMEImage(image.read())
mime_image.add_header('Content-ID', '<image>')
msg.attach(mime_image)
msg.send()
You can check this SO Answer for more details on how to send image from Django

How to add additional user information while the user is logged in?

I have created a login in page and now want to redirect the user to a page to add additional user information. How do I ensure that the user is "logged in" and that the information is added to that user's profiles? I know I need to use something like user=User.objects.get(email=email) to identify the user but I don't want to have the user enter their email on every single page to identify themselves. Is there something I can pass to keep the user Logged-in?
Here is the view:
def add_user_profile(request):
# if this is a POST request we need to process the form data
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = AddUserProfileForm(request.POST)
# check whether it's valid:
if form.is_valid():
# process the data in form.cleaned_data as required
# ...
# redirect to a new URL:
return HttpResponseRedirect('/summary/')
# if a GET (or any other method) we'll create a blank form
else:
form = AddUserProfileForm()
return render(request, 'registration/add_user_profile.html', {'form': form})
Here is the form:
class AddUserProfileForm(forms.ModelForm):
this_year = datetime.date.today().year
Years = range(this_year-70, this_year+1)
required_css_class = 'required'
first_name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'First Name'}), label="")
middle_name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Middle Name'}), label="")
last_name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Last Name'}), label="")
date_of_birth = forms.DateField(widget=forms.extras.widgets.SelectDateWidget(years=Years))
Primary_address = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Primary address'}), label="")
Primary_address_zipcode = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Zip Code'}), label="")
class Meta:
model = User
fields = ('first_name', 'middle_name', 'last_name', 'date_of_birth', 'Primary_address', 'Primary_address_zipcode')
Here is the url.py
url(r'^add_user_profile/$',
views.add_user_profile,
name='add_user_profile'),
You just need to create a view which will deal with request.user. If your update_profile functionality is rather simple you may take a look at UpdateView. You just need to overwrite get_object() to look like this:
def get_object(self, queryset=None):
return self.request.user
After this you just need to set up you UpdateView and create a proper template. You can read about this in the docs.