Files
santeh-ray/home/views.py
Azimkin 4f4a1a55e5
All checks were successful
Build and Push Docker Image / build (push) Successful in 43s
feat: add FAQ question form with email notifications and validations
- Created `FaqQuestionForm` in `forms.py` with custom field validations
- Added FAQ form to the FAQ section with CSRF protection and error handling
- Integrated email notifications to administrator upon FAQ submission
- Updated styles and scripts for form inputs, validation messages, and messages UI
- Adjusted FAQ section layout in `faq.html` and related CSS for responsiveness
- Implemented tests for FAQ form submission and email notifications
2026-04-19 22:51:56 +02:00

62 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import logging
from django.conf import settings
from django.core.mail import send_mail
from django.shortcuts import redirect, render
from django.urls import reverse
from django.contrib import messages
from home.forms import FaqQuestionForm
logger = logging.getLogger(__name__)
def index(request):
form = FaqQuestionForm(request.POST or None)
if request.method == "POST" and form.is_valid():
subject = f"Новый вопрос с сайта {settings.SITE_NAME}"
message = "\n".join(
[
f"Имя: {form.cleaned_data['name']}",
f"Телефон: {form.cleaned_data['phone']}",
f"Вопрос: {form.cleaned_data['question']}",
"",
f"Страница: {request.build_absolute_uri('/')}",
]
)
try:
send_mail(
subject=subject,
message=message,
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[settings.ADMINISTRATOR_EMAIL],
fail_silently=False,
)
except Exception:
logger.exception("Failed to send FAQ question email")
form.add_error(None, "Не удалось отправить вопрос. Попробуйте позже.")
else:
messages.success(request, "Спасибо. Ваш вопрос отправлен, мы свяжемся с вами в ближайшее время.")
return redirect(f"{reverse('home')}#faq-section")
return render(request=request, template_name='home/index.html', context={"faq_form": form})
def services(request):
return render(request=request, template_name='home/services.html', context={})
def shipping(request):
return render(request=request, template_name='home/shipping.html', context={})
def warranty(request):
return render(request=request, template_name='home/warranty.html', context={})
def contacts(request):
return render(request=request, template_name='home/contacts.html', context={})