Капча не будет отображаться в моем файле html в Django - PullRequest
0 голосов
/ 07 апреля 2020

Я пытался ввести капчу для моей контактной формы. Я следовал инструкциям, Установил Django -простую-капчу. Подушка была уже установлена. Сделал миграцию, добавил путь к urls.py и создал соответствующий класс в forms.py. Я поставил капчу в разделе УСТАНОВЛЕННЫЕ ПРИЛОЖЕНИЯ в настройках.

Мой urls.py - -

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

Мой views.py - -

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"))

Мой forms.py -

from django import forms
from captcha.fields import CaptchaField

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

Мой html файл

{% 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>

Как вы можете видеть, я поместил {{forms.captcha}} прямо над кнопкой отправки.

Я пытался "manage.py" test captcha ", и он дал мне ошибку:

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

Я не знаю, является ли это проблемой или если проблема в моем коде. Пожалуйста, помогите!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...