Guest User

Contact form django 3.0

a guest
Jun 6th, 2021
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.01 KB | None | 0 0
  1. # Добрый день. Есть 2 проблемы:
  2. # 1 мне в contact форме мне приходят письма на почту Reverse for 'chat.views.SuccessView' not found. 'chat.views.SuccessView' is not a valid view function or pattern name.
  3. # 2 нужно сделать категорию для формы, т.е. сделать выбор к какой категории отнописьмо (категорию выбирает пользователь)
  4.  
  5.  
  6. views
  7. from django.core.mail import send_mail, BadHeaderError
  8. from django.http import HttpResponse, HttpResponseRedirect
  9. from django.shortcuts import render, redirect
  10. from .forms import FeedBackForm
  11.  
  12. def contact(request):
  13.     if request.method == 'GET':
  14.         form = FeedBackForm()
  15.     else:
  16.         form = FeedBackForm(request.POST)
  17.         if form.is_valid():
  18.             subject = form.cleaned_data['name']
  19.             from_email = form.cleaned_data['email']
  20.             message = form.cleaned_data['description']
  21.             try:
  22.                 send_mail(subject, message, from_email, ['[email protected]'])
  23.             except BadHeaderError:
  24.                 return HttpResponse('Invalid header found.')
  25.             return redirect(SuccessView)
  26.     return render(request, "chat/comment.html", {'form': form})
  27.  
  28. """ Письмо отправляется, но не приходит
  29. Content-Type: text/plain; charset="utf-8"
  30. MIME-Version: 1.0
  31. Content-Transfer-Encoding: 7bit
  32. Subject: Name
  33. Date: @"""
  34.  
  35. # Выходит ошибка django.urls.exceptions.NoReverseMatch: Reverse for 'chat.views.SuccessView' not found.
  36. #'chat.views.SuccessView' is not a valid view function or pattern name.
  37. # "POST /home/comment/ HTTP/1.1" 500 89420
  38.  
  39. def SuccessView(request):
  40.     return HttpResponseRedirect(contact)
  41.  
  42. #forms
  43.  
  44. from django import forms
  45. """
  46. CATEGORY = {
  47.     ('1', '1'),
  48.     ('2', '2'),
  49.     ('3', '3'),
  50.     ('4', '4')
  51. }
  52. """
  53. class FeedBackForm(forms.Form):
  54.     name = forms.CharField(label="Имя пользователя", max_length=150, required=True)
  55.     email = forms.EmailField(label="email", required=True)
  56.     description = forms.CharField(label="Текст", widget=forms.Textarea, required=True)
  57. #   category = forms.ChoiceField(label="Категория", choices=CATEGORY, required=True)
  58.  
  59. # HTML
  60. <div id="contact-form">
  61.           <form action="{% url 'chat:comment' %}" method="POST">
  62.             {% csrf_token %}
  63.             <input type="hidden">
  64.               <li><label for="id_name">Имя</label><input type="text" name="name" maxlength="120" required id="id_name"></li>
  65.               <li><label for="id_email">Email</label><input type="email" name="email" maxlength="120" id="id_email"></li>
  66.               <li>
  67.                 <label for="id_category">Как я могу помочь вам?</label>
  68.                 <!--<select name="category" id="id_category">
  69.                   <option value="">Выберите!</option>
  70.                   <option value="1" class="btn-info">Нужна помощь в финансовой грамотносте</option>
  71.                   <option value="2" class="btn-info">Давай выпьем кофе</option>
  72.                   <option value="3" class="btn-info">У меня есть личный вопрос</option>
  73.                   <option value="4" class="btn-info">Есть ошибка на сайте</option>
  74.                 </select>-->
  75.               </li>
  76.               <li>
  77.                 <label for="id_description">Сообщение</label><textarea type="text" name="description" maxlength="1500" id="id_description" cols="40" rows="10"></textarea>
  78.               </li>
  79.             <button type="submit" class="btn btn-primary">Отправить</button>
  80.           </form>
  81.         </div>
  82.  
  83. # URls
  84.  
  85. from django.urls import path, include
  86. from . import views
  87.  
  88.  
  89. app_name = 'chat'
  90. urlpatterns = [
  91.     path("home/comment/", views.contact, name='comment'),
  92.     path("home/success/", views.SuccessView, name="SuccessView"),
  93. ]
  94.  
  95.  
  96. # Спасибо
Advertisement
Add Comment
Please, Sign In to add comment