联系表格在django [英] Contact Form in django
问题描述
我只有一个页面叫联系人,谢谢!
谢谢
这是我的代码:
models.py
from django.db import models
class Subject(models.Model):
question_ = 0
question_one = 1
question_two = 2
question__three = 3
STATUS_CHOICES =(
(question_,''),
(question_one,'我有一个问题'),
(question_two,'帮助/支持'),
(question__three,'请给我打电话'),
)
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提交
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,'我有问题'),
(question_two,'Help / Support'),
(question__three,'请给我打电话'),
)
class ContactForm
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,*参考,** k wargs):
self.helper = FormHelper()
self.helper.add_input(Submit('submit','Submit'))
super(ContactForm,self).__ init __(* args ,** kwargs)
views.py
从django.conf导入设置
从django.core.mail导入send_mail
从django.views.generic导入FormView
从.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}说:.format(
name = form.cleaned_data.get('name'
email = form.cleaned_data.get('email'))
message + =\\\
\\\
{0}format(form.cleaned_data.get('message'))
send_mail(
subject = form.cleaned_data.get('subject')。strip(),
message = message,
from_email =info@example.com,
recipient_list = [settings.LIST_OF_EMAIL_RECIPIENTS],
)
返回超级(ContactFormView,self).form_valid(form )
class Subject(models.Model):
question_ = 0
question_one = 1
question_two = 2
question__three = 3
STATUS_CHOICES =(
(question_,''),
(question_one,'我有个问题'),
(question_two,'Help / Support'),
(question__three,'请给我打电话'),
)
你不需要新课程,只是这个,你看到0,1,2,3是什么会识别每个选择,你可以在第一部分放任何东西,例如0,1,2,3或IHAQ缩写为我有问题
STATUS_CHOICES =(
(0,),
(1,我有问题),
(2,帮助/支持),
,请给我打电话),
)
I have a contact form which all working without any error, the only thing I don't understand is when I click on send button no message receive, could anyone tell me why or what is wrong, please? I have only one page called contact, no thanks page! Thanks
Here is my code: models.py from django.db import models
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="info@example.com",
recipient_list=[settings.LIST_OF_EMAIL_RECIPIENTS],
)
return super(ContactFormView, self).form_valid(form)
There is short and better way of doing this,
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'),
)
You don't need new class, just this, where you see 0,1,2,3 is what will recognise each choice, you can put anything in the first section, e.g. 0,1,2,3, or "IHAQ" short for "I have a question"
STATUS_CHOICES = (
("0", ""),
("1", "I have a question"),
("2", "Help/Support"),
("3", "Please give me a call"),
)
这篇关于联系表格在django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!