You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Django/doc/oauth/forms.py

26 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# 导入Django表单基础类和控件模块
from django.contrib.auth.forms import forms
from django.forms import widgets
class RequireEmailForm(forms.Form):
"""
用于收集用户电子邮箱的表单类
通常在OAuth第三方登录时若用户未提供邮箱信息用于补充收集
"""
# 电子邮箱字段使用EmailField进行格式验证标签为"电子邮箱",且为必填项
email = forms.EmailField(label='电子邮箱', required=True)
# OAuth用户ID字段隐藏控件HiddenInput用于关联第三方登录用户非必填
oauthid = forms.IntegerField(widget=forms.HiddenInput, required=False)
def __init__(self, *args, **kwargs):
"""
重写初始化方法,自定义表单字段的控件属性
主要用于设置邮箱输入框的占位符和CSS样式类
"""
# 调用父类的初始化方法,确保表单正常初始化
super(RequireEmailForm, self).__init__(*args, **kwargs)
# 为email字段设置自定义控件EmailInput
# 添加placeholder提示文本和form-control的CSS类通常用于Bootstrap样式
self.fields['email'].widget = widgets.EmailInput(
attrs={'placeholder': "email", "class": "form-control"})