WitaloBenicio
WitaloBenicio

Reputation: 3692

Add Django-Simple-Captcha in contact area

Im not using forms.py to generate my contact area of website, but i want to add captcha in my form. I try to create only the captcha form in forms.py by doing this:

from django import forms
from captcha.fields import CaptchaField

class CaptchaTestForm(forms.Form):
    captcha = CaptchaField()

But this do not seems to work. Any solutiosn?

I'm just using the tag {{ form.as_p}} inside the form

EDIT

This is my view:

def home(request):

    if request.method == 'POST':
        form = CaptchaTestForm(request.POST)
        nome = request.POST.get('nome', None)
        assunto = request.POST.get('assunto', None)
        email = request.POST.get('email', None)
        mensagem = request.POST.get('mensagem', None)
        if form.is_valid():
            if nome and assunto and email and mensagem:
                try:
                    conteudo = 'Nome: %s\nAssunto: %s\nEmail: %s\nMensagem: %s' % (nome, assunto, email, mensagem.strip())
                    send_mail('Contato via site Dona Rita', conteudo, EMAIL_HOST_USER,
                              ['[email protected]'], fail_silently=True)
                    messages.success(request, 'Recebemos o seu contato. Em breve entraremos retornaremos.')
                except SMTPException:
                    messages.error(request, 'Ocorreu um erro ao enviar o e-mail mas já estamos solucionando,\
                    tente novamente mais tarde.')
            else:
                messages.error(request, 'Preencha os campos corretamentes.')

    slider = Slider.objects.all()
    promocoes = Promocao.objects.all()
    categorias = Categoria.objects.all()
    produtos = Produto.objects.all()
    cardapio = {}

    for categoria in categorias:
        myproducts = []
        cardapio[categoria.name] = ''
        for produto in produtos:
            if produto.category == categoria:
                myproducts.append(produto)
                cardapio[categoria.name] = myproducts

    od = collections.OrderedDict(sorted(cardapio.items()))

    return render_to_response('signups.html', {'produtos':od,'promocoes':promocoes, 'slider':slider}, context_instance=RequestContext(request))

And this is my template

<form role="form" method="POST">
    {% csrf_token %}
    <div class="form-group">
        <label for="name">Nome</label>
        <input type="text" class="form-control" id="name" placeholder="Seu nome" name="nome">
    </div>
    <div class="form-group">
        <label for="subject">Assunto</label>
        <input type="text" class="form-control" id="subject" placeholder="Assunto da Mensagem" name="assunto">
    </div>
    <div class="form-group">
        <label for="email">Email</label>
        <input type="email" class="form-control" id="email" placeholder="Digite seu email" name="email">
    </div>
    <div class="form-group">
        <label for="message">Mensagem</label>
        <textarea class="form-control" rows="3" placeholder="Digite sua Mensagem" id="message" name="mensagem"></textarea>
    </div>
    {{ form }}
    <button type="submit" class="btn btn-default">Enviar</button>
</form>

Upvotes: 0

Views: 490

Answers (1)

Chillar Anand
Chillar Anand

Reputation: 29554

You are not sending CaptchaTestForm to user when user makes a get request.

To do that, import CaptchaTestForm in your view.

from .forms import CaptchaTestForm

Replace last line of your view with this to send that form.

form = CaptchaTestForm()
return render_to_response('signups.html', {'produtos':od,'promocoes':promocoes,
                          'slider':slider, 'form': CaptchaTestForm}, 
                          context_instance=RequestContext(request))

Upvotes: 1

Related Questions