I'm building my first Django app, and have a simple contact form with the usual fields (recipient's address, subject, sender's address, and message) that I submit via a POST request to the home page view. If the form is valid, it sends the email and redirects to a success page. Searching through the PythonAnywhere forums, it seems that best practice for sending email on PythonAnywhere is to use Google's SMTP servers, so I set up a gmail account and updated my settings accordingly. Code for view, form, HTML template, and settings is below. The URL for the app is here: http://www.doorstepstudios.com/
Whenever I try to submit, I get a "405 Not Allowed" error. My error logs are empty, and the server logs show the following:
DAMN ! worker 3 (pid: 27518) died, killed by signal 9
I've already tried reloading the app, which doesn't resolve the error. I've also confirmed that the redirect URL exists (http://www.doorstepstudios.com/launched)
So, two questions:
-
Has anyone else had a similar 405 problem, and/or have any insight on what's going on? I found one other related post in the forums here that the user solved themselves, but didn't post their solution, and the only other suggestion was about Flask routes, which as far as I can tell from the docs doesn't apply to Django
-
Is the best practice for submitting emails on a PythonAnywhere hosted app still to use Google SMTP servers, or is that outdated and therefore causing problems?
views.py
def home_page(request):
if request.method == 'POST':
form = ContactForm(data=request.POST)
if form.is_valid():
recipients = list(form.cleaned_data['to_field'])
subject = form.cleaned_data['subject_field']
sender = form.cleaned_data['from_field']
message = form.cleaned_data['message_field']
send_mail(subject, message, sender, recipients, fail_silently=False)
return HttpResponseRedirect('/launched')
return render(request, 'home.html', {'dream_form' : DreamsForm(), 'contact_form' : ContactForm()})
forms.py
ContactForm(forms.Form):
to_field = forms.CharField(max_length=100, label="")
subject_field = forms.CharField(max_length=100, label="")
from_field = forms.EmailField(label="",
widget=forms.TextInput(attrs={'placeholder' : "Your email..."}))
message_field = forms.CharField(label="",
widget=forms.Textarea(attrs={'placeholder': "Say it like you mean it"}))
home.html
<form method="post" id="contact_form">
{% csrf_token %}
{% for field in contact_form %}
<div class="field_wrapper">
{{ field.errors }}
</div>
{% endfor %}
{{ contact_form }}
<input type="submit" id="contact_submit" value="Send">
</form>
settings.py
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = "doorstepstudios@gmail.com"
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_PASSWORD = '[password]'