Y
Y
yura2017-03-29 17:14:17
Django
yura, 2017-03-29 17:14:17

How to send an email in Django 1.10?

Modal form window

<form id="callback" class="get-info" action="{% url 'callback:callback' %}" method="POST">
    {% csrf_token %}
    <input type="hidden" name="_subject" value="karpaty-perezvonite-mne" />
    <div class="form-group">
    <input id="callback" name="phone" type="tel" class="form-control" placeholder="+ 380 ___-__-__">
    </div>
    <input type="text" class="form-control" placeholder="" name="lastpath" value="/" style="display: none"/>
    <button type="submit" class="btn btn-default" value="Send">Отправить</button>
</form>

View
@requires_csrf_token
def callback(request):
    phone = request.POST.get('phone', None)
    text = u'Вас просят перезвонить по номеру: ' + phone
    if request.method == 'post':
        c = {}
        send_mail('Заказ обратного звонка', text, '[email protected]',
                  ['[email protected]'], fail_silently=False)
        return render(request, 'index.html', c)

main path
url(r'^callback/', include('callback.urls', namespace="callback")),

The path to the app
from django.conf.urls import url
urlpatterns = [
    url(r'^callback/$', views.callback, name='callback'),
]

Settings
CSRF_COOKIE_SECURE = True

MIDDLEWARE = [
    'django.middleware.csrf.CsrfViewMiddleware',
]
TEMPLATES = [{
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.csrf',
            ],
        },
    },
]

When submitting the form, I get a 403 error, the token is in the cookie, but the hidden field with the token is not created

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question