Captcha wouldn't show up in my html file in Django

336 Views Asked by At

I was trying to implement Captcha for my contact form. I followed the instructions, Installed Django-simple-captcha. Pillow was already installed. Made migrations, added the path to urls.py and created the appropriate class in forms.py. I put captcha in the INSTALLED APPS sections in settings.

My urls.py is -

urlpatterns = [
    path('', include('calc.urls')),
    path('admin/', admin.site.urls),
    path(r'captcha/', include('captcha.urls')),
]

My views.py is -

def contact_sub(request):
  if(request.method == 'POST'):
    form = CapForm(request.POST)
    if(form.is_valid()):
      x = 3
    else:  
      form = CapForm()
  else:
    form = CapForm(request.GET)
    if(form.is_valid()):
      x = 3
    else:
      form = CapForm()
  txtarea = request.GET['sum_inq']
  fname = request.GET['fname']
  cont_list = "First Name: " + fname
  lname = request.GET['lname']
  cont_list = cont_list + '\n\nLast Name: ' + lname   
  email_id = request.GET['email_id']
  cont_list = cont_list + '\n\nEmail Id: ' + email_id
  are_you = request.GET['are_you']
  cont_list = cont_list + '\n\nWhich of the following most accurately describes your organization?: ' + are_you

  if(are_you == "Other"):
    specify1 = request.GET['ay_specify']
    cont_list = cont_list + '\n\nSpecify: ' + specify1
  cont_list = cont_list + '\n\nSummary of Inquiry:\n' + txtarea

  try:
    validate_email(email_id)
    send_mail('Contact Form', cont_list, settings.EMAIL_HOST_USER, ['emailaddress'], fail_silently=False)
    #context = {'message': "Sent"}
    request.session['message'] = "Sent"
    return HttpResponseRedirect(reverse("contact_us"))
    #return render(request, "contact.html", context)
  except ValidationError:
    #context = {'message': "Not Send"}
    request.session['message'] = "Not Send"
    return HttpResponseRedirect(reverse("contact_us"))

My forms.py is

from django import forms
from captcha.fields import CaptchaField

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

My html file is

{% load static %}
<!DOCTYPE html>
<html>
<head>
  <script src="{% static 'files/jquery-1.8.0.js' %}"></script>
{% load bootstrap4 %}
{% bootstrap_css %}
{% bootstrap_javascript jquery='full' %}
{% bootstrap_messages %}

    <link rel="stylesheet" type="text/css" href="{% static 'files/calcucont.css' %}">
</head>
<body>
<form id="contact_form" method="get" action="contact_sub" name="contact_form">

<div class="container border border-dark expand-lg disp3 dsp3">
<a id="sas"></a>  
<h1 class="bg-primary hj">Contact Us</h1>
  <div class="form-group space">
    <label><b>First Name</b></label>
    <input type="text" class="form-control col-md-3" placeholder="Enter first name" id="fname" name="fname" onkeydown="return contactform(event)" autocomplete="off" required><span id="gspan" class="gspan"><span>&#9888;</span>Enter a valid name</span></input>
  </div>
  <div class="form-group space">
    <label><b>Last Name</b></label>
    <input type="text" class="form-control col-md-3" placeholder="Enter last name" id="lname" name="lname" onkeydown="return contactform(event)" autocomplete="off" required><span id="cspan" class="gspan"><span>&#9888;</span>Enter a valid name</span></input>
  </div>
  <div class="form-group space">
    <label><b>Email Address</b></label>
    <input type="email" class="form-control col-md-3" id="email_id" autocomplete="off" name="email_id" placeholder="Enter email" onkeydown="return contactform2(event)" required><span id="aspan" class="gspan"><span>&#9888;</span>Enter a valid email</span></input>
  </div> 

    <label class="space"><b>Which of the following most accurately describes your organization?</b></label>
    <div class="form-check">
     <label class="form-check-label">   
     <input type="radio" class="form-check-input" onclick="areyourad(this.id)" id="c_o" value="Contractor" name="are_you">Contractor</label>
    </div>
    <div class="form-check">
     <label class="form-check-label">   
     <input type="radio" class="form-check-input" onclick="areyourad(this.id)" id="ind" value="State Agency" name="are_you">State Agency</label>
    </div>
    <div class="form-check">
     <label class="form-check-label">   
     <input type="radio" class="form-check-input" onclick="areyourad(this.id)" id="lag" value="Local Agency" name="are_you">Local Agency</label>
    </div>
        <div class="form-check">
     <label class="form-check-label">   
     <input type="radio" class="form-check-input" onclick="areyourad(this.id)" id="fag" value="Federal Agency" name="are_you">Federal Agency</label>
    </div>
    <div class="form-check">
     <label class="form-check-label">   
     <input type="radio" class="form-check-input" onclick="areyourad(this.id)" id="cag" value="Consultant Agency" name="are_you">Consultant Agency</label>
    </div>
    <div class="form-check">
     <label class="form-check-label">   
     <input type="radio" class="form-check-input" onclick="areyourad(this.id)" id="ind" value="Industry" name="are_you">Industry</label>
    </div>
    <div class="form-check">
     <label class="form-check-label">   
     <input type="radio" class="form-check-input" onclick="areyourad(this.id)" id="ri" value="Research Institution" name="are_you">Research Institution</label>
    </div>
      <div class="form-check">
     <label class="form-check-label">   
     <input type="radio" class="form-check-input" onclick="areyourad(this.id)" id="aca" value="Academic" name="are_you">Academic</label>
    </div>
    <div class="form-check">
     <label class="form-check-label">   
     <input type="radio" class="form-check-input" onclick="areyourad(this.id)" id="ay_other" value="Other" name="are_you" checked>Other:</label>
    </div>
    <div class="form-group">
     <input type="text" class="form-control col-md-8" placeholder="Specify" autocomplete="off" onkeydown="return contactform3(event)" name="ay_specify" id="ay_specify" required>
    </div>
    <div class="form-group space">
     <label><b>Please briefly summarize your inquiry</b></label>
     <textarea class="form-control col-md-8" id="sum_inq" name="sum_inq" autocomplete="off" onkeydown="return contactform3(event)" rows="5"></textarea>
    </div>
   {% csrf_token %}   
   {{ form.captcha }}   
  <button type="submit" class="btn btn-primary btn-lg mx-auto d-block btn-cont" onclick="return subut()">Submit</button>
</div>
<div class="container expand-lg disp3"></div>
</form>


<p id="invis" value="{{ request.session.message }}" hidden>    
<script> 
   var cont = '{{ request.session.message }}'; 
</script>    
<script src="{% static 'files/script7.js' %}"> 
</script>

As you can see I've put in the {{forms.captcha}} right above the submit button.

I tried "manage.py test captcha" and it gave me an error:

django.template.exceptions.TemplateDoesNotExist: captcha_test/image.html

I don't know if that's the problem or if the problem is in my code. Please Help!

0

There are 0 best solutions below