Django returning ValueError

1

I am creating a project to send emails, however I am doing some tests and I came across the following problem:

  

The view send_mail.core.views.index did not return an HttpResponse object. It returned None instead.

Below are my lines of code:

#views
from django.shortcuts import render, render_to_response
from django.template.context import RequestContext
from .forms import SendMail


def index(request):
    if request.method == "POST":
        form = SendMail(request.POST)
        if form.is_valid():
            subject = form.cleaned_data['subject']
            print(form)
            return render_to_response('index.html', 
                context_instance=RequestContext(request, {"form": form},))
    else:
        print("ops!!")


#forms
from django import forms


class SendMail(forms.ModelForm):
    subject = forms.CharField(max_length=80)


#template
<!DOCTYPE html>
<html>
<head>
    <title>some title</title>
</head>
<body>
    <form action="." method="POST">
      {% csrf_token %}
      {{form.as_p}}
    </form>
</body>
</html>

I do not need to save the data in a database, so I'm not using a model, I just need it, what I write inside the textarea field is printed. In future I want to send emails with the same, the intention is to create an email sender, where it will contain a field with the client's email and a textarea with the desired subject.

Can anyone tell me why the above error?

    
asked by anonymous 17.08.2017 / 18:49

1 answer

1

Have you treated form when method is POST and GET ? just an "ops!"? Try to leave your view according to the code below, making the necessary additions.

from django.shortcuts import render
def index(request):

    if request.method == 'POST':
        form = SendMail(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            print (cd)
            form = SendMail()   # Limpando o form
        else:
            print ('Valores inválidos')
            print (form.errors)

    else:
        form = SendMail() # Inicicializando o form

    return render(request, 'caminho/sua_template.html', {'form': form})
    
17.08.2017 / 19:58