如何修复联系表格?

时间:2017-09-05 02:28:59

标签: python django api django-rest-framework contact-form

我有一个从旧设计到联系表格的脚本,我已经无法工作几天了。我对Django Rest Framework和API一无所知。有人可以帮助我使这个脚本在没有所有验证码的情况下工作。无论如何,必须从此脚本中删除验证码。问题是在填写并发送联系表单后,我不会将邮件发送到电子邮件地址。

views.py

from django.utils.translation import ugettext_lazy as _
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from django.conf import settings
from django.core.mail import send_mail
from django.template.loader import render_to_string

from visualcaptcha import Captcha, Session
import os


class ContactView(APIView):
    u"""Widok wysyłający wiadomość email"""

    def post(self, request):
        u"""Metoda wysyłająca wiadomość email.

        Parameters
        ----------
        request : rest_framework.request.Request
            Obiekt żądania HTTP

        Returns
        -------
        rest_framework.response.Response
            Obiekt odpowiedzi HTTP.
        """

        assetsPath = os.path.join(settings.BASE_DIR,
                                  r'visualcaptcha', r'assets')
        visualCaptcha = Captcha(Session(request.session), assetsPath)
        frontendData = visualCaptcha.getFrontendData()
        imageField = request.data.pop(frontendData['imageFieldName'], None)
        audioField = request.data.pop(frontendData['audioFieldName'], None)

        captcha_valid = False

        if imageField is not None:
            captcha_valid = visualCaptcha.validateImage(imageField)
        if audioField is not None:
            captcha_valid = visualCaptcha.validateAudio(audioField)

        if not captcha_valid:
            print("hakuna matata")
            return Response({
                'status': 'Bad request',
                'message': (_('The captcha sent was not valid')),
                'errors': {
                    'invalid_captcha': 'The captcha sent was not valid'
                }
            }, status=status.HTTP_400_BAD_REQUEST)
        try:
            subject = (request.data['subject'])
            message = (
                u'''coins-sell.pl wiadomość od użytkownika %s
                email: %s
                wiadomość:
                %s''') % (
                request.data['name'], request.data['email'],
                request.data['message'])
            from_email = request.data['email']
            recipient_list = ['kontakt@coins-sell.pl']
            html_message = render_to_string(
                'emails/contactEmail.html',
                {'name': request.data['name'],
                    'email': request.data['email'],
                    'message': request.data['message']
                 })
            send_mail(
                subject=subject,
                message=message,
                from_email=from_email,
                recipient_list=recipient_list,
                html_message=html_message
            )
            return Response({
                'status': 'OK',
                'message': (
                    _('Email was sent succesfully'))
            }, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({
                'status': 'Bad request',
                'message': (
                    _('Message could not be sent')),
                'errors': {'exception': str(e)}
            }, status=status.HTTP_400_BAD_REQUEST)

form.html

  <form name="vm.contact_form">
    <label for="contact__name">Imię</label>
      <input type="text" name="contact_name" id="contact__name">
        <strong>Pole wymagane.</strong><br><br>

    <label for="contact__email">Email</label>
      <input type="email" name="contact_email" id="contact__email" required>
        <strong>Pole wymagane.</strong><br><br>

    <!-- <label for="contact__subject">Temat</label>
      <select name="contact_subject" id="contact__subject" required></select>
        <strong>Pole wymagane.</strong><br><br> !-->

    <label for="contact__message">Wiadomość</label>
      <textarea name="contact_message" id="contact__message" required></textarea>
        <strong>Pole wymagane.</strong><br><br>

    <button type="submit">Wyślij</button>
  </form>

settings.py

EMAIL_USE_SSL = True
EMAIL_HOST = 'mail0.mydevil.net'
EMAIL_PORT = 465
EMAIL_HOST_USER = '************'
EMAIL_HOST_PASSWORD = 'my_password'
DEFAULT_FROM_EMAIL = 'example@example.com'
MANAGERS = (('George Example', 'example@example.com'),)

请为此,请耐心等待我的英语不好和缺乏知识,因为我是学生。

0 个答案:

没有答案