0

I wrote code in basic HTML tags like and there was no problem. Mail was sent correctly. But when I changed its place and HTML tags mail function didn't work. What problem can be?

It works

<h1>Contact Us</h1>
<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <div class="form-actions">
      <button type="submit">Send</button>
    </div>
</form>

It doesn't work.

<form class="contact-form" action="" method="GET">
    {% csrf_token %}
    {% for field in form %}
    {{ field|add_class:"input" }}            
    {% endfor %}
    <button class="button" type="submit">Send</button> 
</form>

views.py

def index(request):
    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST or None)
        if form.is_valid():
            fullname = form.cleaned_data['fullname']
            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            try:
                send_mail(fullname, message, from_email, 
                           ['[email protected]'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('home')
    context['form'] = form
    return render(request, "index.html", context)
8
  • What is the error you are getting? Commented Apr 19, 2019 at 3:54
  • Also include your view Commented Apr 19, 2019 at 4:04
  • @Sanip unfortunately I don't get any error. When I push submit button I see Ewerything was sent with GET method. But I used POST method in in the form Commented Apr 19, 2019 at 6:49
  • @Gasanov I included Commented Apr 19, 2019 at 6:51
  • In your not working form, you have specified the method as post Commented Apr 19, 2019 at 6:56

1 Answer 1

1

The form as you have stated not working:

<form class="contact-form" action="" method="GET">
    {% csrf_token %}
    {% for field in form %}
    {{ field|add_class:"input" }}            
    {% endfor %}
    <button class="button" type="submit">Send</button> 
</form>

You can see that you have used method="GET". Then in your views:

def index(request):
    if request.method == 'GET':
        form = ContactForm()
    else:
        form = ContactForm(request.POST or None)
        if form.is_valid():
            fullname = form.cleaned_data['fullname']
            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            try:
                send_mail(fullname, message, from_email, 
                       ['[email protected]'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('home')
    context['form'] = form
    return render(request, "index.html", context)

Here you have used the code for sending mail inside POST method block as:

    else:
        form = ContactForm(request.POST or None)
        if form.is_valid():
            fullname = form.cleaned_data['fullname']
            from_email = form.cleaned_data['from_email']
            message = form.cleaned_data['message']
            try:
                send_mail(fullname, message, from_email, 
                       ['[email protected]'])
            except BadHeaderError:
                return HttpResponse('Invalid header found.')
            return redirect('home')

So while you submit the form, it is being sent as a get request. And the get request is being handled by this block:

    if request.method == 'GET':
        form = ContactForm()

Hence, the mailing function is not working.

Try changing the method as post in the html form.

Sign up to request clarification or add additional context in comments.

2 Comments

Great thank you. And one more question I have. Is there any differences between "post" and "POST". I mean is there any effect of Initial letters?
Well in the html form method attribute, you can use both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.