1

So i was this application in which when a user fill the form, I get an email with their details so that i can contact them. But when ever the form is submitted, it shows me this error.

enter image description here

I couldn't find whats bugging the code. All the credentials are in place. I am using SendingBlue smtp server for this project. Have a look at my code:

views.py

from django.shortcuts import render, redirect
from django.core.mail import send_mail, BadHeaderError
from django.http import HttpResponse, HttpResponseRedirect

# Create your views here.

def home(request):
    return render(request, 'index.html')


def register(request):

    if request.method == 'GET':
        return render(request, 'register.html')

    else:
        name = request.POST['full-name']
        email = request.POST['email']
        phone = request.POST['phone']
        nationality = request.POST['nationality']

        try:
            send_mail("Trial Application", name + email + phone + nationality, '*my email*', ['* same email*'])

        except BadHeaderError:
            return HttpResponse('Something Went Wrong')
        
        return redirect(request, 'index.html')
    return render(request, 'register.html')

settings.py

EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
EMAIL_HOST = "smtp-relay.sendinblue.com"
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = "*my email*"
EMAIL_HOST_PASSWORD = "password"

register.html


        <form action="" method="post" class="form">
            {% csrf_token %}
            <fieldset>
                <legend>Please fill in the required details</legend>
                <input type="text" name="full-name" placeholder="Full Name" id="">
                <input type="email" name="email" placeholder="Email" id="">
                <input type="tel" name="phone" placeholder="Phone Number" id="">
                <input type="text" name="nationality" placeholder="Nationality" id="">

                <input type="submit" value="Submit" id="submit">
            </fieldset>
        </form>

I would really appreciate if someone would help me eradicate this bug. Thanks in advance.

1 Answers1

0

the error you are having is not because of the send_mail function but because Django is not able to find the URL: '/apply/'. The action you are providing in the form's HTML is not present in urls.py. Please make sure the action matches according to your django's URL patterns.

for example:

<!--If this is your form tag:-->
<form method=post>

you need to add action to it which should match URL mapping to your django urls.py like this:

<form method=post action="/register/account">

action will take the form to this URL after clicking submit: http://localhost/register/account

Thus, in urls.py you should have a match pattern like:

urlpatterns = [
path('register/account', views.register, name='register')
]
SimbaOG
  • 460
  • 2
  • 8