2014-04-14 81 views
0

我有一個聯繫表單,所有的工作都沒有任何錯誤,我唯一不明白的地方是當我點擊發送按鈕沒有消息接收時,有人能告訴我爲什麼或什麼是錯誤的嗎? 我只有一個叫做聯繫人的頁面,不用謝謝! 感謝django中的聯繫表格

這裏是我的代碼: models.py 從django.db進口車型

class Subject(models.Model): 
    question_ = 0 
    question_one = 1 
    question_two = 2 
    question__three = 3 

    STATUS_CHOICES = (
     (question_, ''), 
     (question_one, 'I have a question'), 
     (question_two, 'Help/Support'), 
     (question__three, 'Please give me a call'), 
     ) 

class Contact(models.Model): 
    name = models.CharField(max_length=100) 
    email = models.EmailField(max_length=150) 
    subject = models.CharField(choices=Subject.STATUS_CHOICES, default=1, max_length=100) 
    phone_number = models.IntegerField() 
    message = models.TextField() 

    def save(self, *args, **kwargs): 
     super(Contact, self).save(*args, **kwargs) 
     return 'Contact.save' 

froms.py

from crispy_forms.helper import FormHelper 
from crispy_forms.layout import Submit 
import floppyforms as forms 

from django_enumfield import enum 

class SubjectEnum(enum.Enum): 
    question_ =0 
    question_one = 1 
    question_two = 2 
    question__three = 3 

    STATUS_CHOICES = (
     (question_, ''), 
     (question_one, 'I have a question'), 
     (question_two, 'Help/Support'), 
     (question__three, 'Please give me a call'), 
     ) 


class ContactForm(forms.Form): 
    name = forms.CharField(required=True) 
    email = forms.EmailField(required=True) 
    subject = forms.TypedChoiceField(choices=SubjectEnum.STATUS_CHOICES, coerce=str) 
    phone_number = forms.IntegerField(required=False) 
    message = forms.CharField(widget=forms.Textarea) 


    def __init__(self, *args, **kwargs): 
     self.helper = FormHelper() 
     self.helper.add_input(Submit('submit', 'Submit')) 
     super(ContactForm, self).__init__(*args, **kwargs) 

views.py

from django.conf import settings 
from django.core.mail import send_mail 
from django.views.generic import FormView 
from .forms import ContactForm 

class ContactFormView(FormView): 
    form_class = ContactForm 
    template_name = "contact/email_form.jade" 
    success_url = '/email-sent/' 

    def form_valid(self, form): 
     message = "{name}/{email} said: ".format(
      name=form.cleaned_data.get('name'), 
      email=form.cleaned_data.get('email')) 
     message += "\n\n{0}".format(form.cleaned_data.get('message')) 
     send_mail(
      subject=form.cleaned_data.get('subject').strip(), 
      message=message, 
      from_email="[email protected]", 
      recipient_list=[settings.LIST_OF_EMAIL_RECIPIENTS], 
     ) 
     return super(ContactFormView, self).form_valid(form) 
+1

表單驗證?即是form_valid()被調用還是調用form_invalid()來代替? – rockingskier

+0

從我的理解是被調用的有效表單。由於測試已經創建了聯繫人模型和下拉主題,因此我得到了該結果。這個想法是保存從表單中收到的所有電子郵件。 – DilMac

回答

0

有可能是這部分的問題:

recipient_list=[settings.LIST_OF_EMAIL_RECIPIENTS], 

如果settings.LIST_OF_EMAIL_RECIPIENTS已經是一個列表,那麼您將它嵌套在另一個列表中。

無論如何,作爲一般規則,當某些東西不起作用時,您應該逐句通過調試器中的代碼或將print語句放在代碼運行時看看發生了什麼。這會讓你的過程變得更容易。

+0

謝謝你的回覆,不幸的是,當我單獨運行這個文件時,我得到:File「/home/vagrant/.virtualenvs/base/local/lib/python2.7/site-packages/django/conf/__init__.py 「,第47行,在_setup %(desc,ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured:請求的設置DEFAULT_INDEX_TABLESPACE,但設置未配置。在訪問設置之前,您必須定義環境變量DJANGO_SETTINGS_MODULE或調用settings.configure()。 – DilMac

+0

你不應該單獨運行這個文件。將打印語句放在該文件中,然後通過在瀏覽器中加載該頁面來運行它。或者,更好的是,使用PyCharm或PyDev之類的IDE,並且可以使用斷點而不是打印語句。 – RexE

+0

嗨RexE,關於我有的錯誤是forms.py文件和views.py文件是沒有任何設置或以上的問題。我已經問過給我這個表格的人了。如果有人有任何想法或任何幫助,非常感激。 – DilMac

1

還有就是做這個的短期和更好的方式,

class Subject(models.Model): 
     question_ = 0 
     question_one = 1 
     question_two = 2 
     question__three = 3 

    STATUS_CHOICES = (
     (question_, ''), 
     (question_one, 'I have a question'), 
     (question_two, 'Help/Support'), 
     (question__three, 'Please give me a call'), 
     ) 

你不需要新的類,只是這一點,在那裏你看到0,1,2,3是什麼將相認選擇,你可以把任何東西放在第一部分,例如0,1,2,3或「IHAQ」是「我有問題」的簡稱

STATUS_CHOICES = (
     ("0", ""), 
     ("1", "I have a question"), 
     ("2", "Help/Support"), 
     ("3", "Please give me a call"), 
     )