我正在尝试在DetailView Django页面中设置Twilio集成。他们查看详细视图页面,可以通过POST方法请求预订。在预订时,我想发送短信给预订的主人,就我而言,老师。
forms.py
from django.core.validators import RegexValidator
class BookingForm(forms.Form):
phone_regex = RegexValidator(regex=r'^\+?1?\d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
Phone = forms.CharField(max_length=15,validators=[phone_regex],label='Phone')
views.py
def post(self,request,**kwargs):
form = BookingForm(self.request.POST)
account_sid = "xxx"
auth_token = "xxx"
teacher = Teacher.objects.get(id=self.kwargs['pk'])
client = Client(account_sid, auth_token)
user_phone = form['Phone'].value
#user_phone = self.request.POST['Phone']
client.messages.create(
to=teacher.phone_number,
from_="+442033225719",
body="You have a new student" +
" Student phone number:{}".format(user_phone))
我有一个模板表单,其中有1个输入 - 电话号码。
但是,当我提交表单时,我收到以下短信:
学生电话号码:<bound method BoundField.value of <django.forms.boundfield.Boundfield object at 0x104e5f0b8>>
请注意,如果我不使用表单,而是使用简单的user_phone = self.request.POST ['Phone']而不是使用if form.is_valid()验证表单,那么一切正常。
答案 0 :(得分:0)
您需要验证表单,然后从cleaning_data
获取值form = BookingForm(self.request.POST)
form.is_valid()
user_phone = form.cleaned_data['Phone']