@
目录图片验证码
1、安装
pip install django-simple-captcha
2、设置
settings.py配置
INSTALLED_APPS = [
.........
'captcha'
]
# 使用django-simple-captcha验证码
CAPTCHA_IMAGE_SIZE = (80, 30) # 设置 captcha 图片大小
CAPTCHA_lENGTH = 4 # 设置字符个数
CAPTCHA_TIMEOUT = 1 # 超时(minutes)
# 输入格式:输入框 验证码图片 隐藏域
CAPTCHA_OUTPUT_FORMAT = '%(text_field)s %(image)s %(hidden_field)s'
CAPTCHA_NOISE_FUNCTIONS = (
'captcha.helpers.noise_null',
'captcha.helpers.noise_arcs', # 线
'captcha.helpers.noise_dots', # 点
)
# 随机字符验证码
CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.random_char_challenge'
#CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.random_match_challenge'
主路由中配置
path('captcha/', include('captcha.urls')),
做数据库迁移
python manage.py migrate
3、建立表单
#应用下的forms.py
from django import forms
from captcha.fields import CaptchaField
class LoginForm(forms.Form):
captcha = CaptchaField() # 验证码字段
4、实现
#应用下的urls.py
app_name='App'
urlpatterns = [
#图片验证码
path('yzm/',views.yzm,name='yzm')
]
#views.py
from App import forms
# 图形验证码
def yzm(request):
if request.method == 'POST':
form = forms.LoginForm(request.POST)
if form.is_valid():
print('验证通过')
return HttpResponse('验证通过')
else:
return render(request, 'verifycode.html', locals())
else:
form = forms.LoginForm()
return render(request, 'verifycode.html', context=locals())
<!--verifycode.html-------->
<body>
<form action="{% url 'App:yzm' %}" method="post">
{% csrf_token %}
{% for foo in form %}
<p>{{ foo.label }}:{{ foo }}</p>
<span>{{ foo.errors.0 }}</span>
{% endfor %}
<input type="submit">
</form>
</body>
<script>
$(function () {
$('.captcha').css({
'cursor': 'pointer'
});
// ajax刷新
$('.captcha').click(function () {
console.log('click');
$.get("{% url 'captcha-refresh' %}",
function (result) {
$('.captcha').attr('src', result['image_url']);
$('#id_captcha_0').val(result['key'])
});
});
})
</script>
</html>
邮件发送
1、settings.py配置
# smtp服务的邮箱服务器
EMAIL_HOST = 'smtp.163.com'
# smtp服务固定的端⼝是25
EMAIL_PORT = 25
#发送邮件的邮箱
EMAIL_HOST_USER = '[email protected]'
#在邮箱中设置的客户端授权密码
EMAIL_HOST_PASSWORD = 'xxxxxxx'
#收件⼈看到的发件⼈ <此处要和发送邮件的邮箱相同>
EMAIL_FROM = 'python<[email protected]>'
2、发送邮件
要配置路由哦
#⼀封邮件
from django.core.mail import send_mail
from django.conf import settings
def sendone(request):
send_mail('标题', '内容', settings.EMAIL_FROM,
['[email protected]']) #目标邮箱
return HttpResponse("发⼀封邮件")
# 发多封邮件
def sendmany(request):
message1 = ('Subject here', '<b>Here is the message</b>',
settings.EMAIL_FROM, ['[email protected]'])
message2 = ('Subject here', '<b>Here is the message</b>',
settings.EMAIL_FROM, ['[email protected]'])
send_mass_mail((message1,message2), fail_silently=False)
return HttpResponse('发送多封邮件')
#渲染模板进⾏邮件发送
def send_mail(request):
subject, from_email, to = 'html', settings.EMAIL_FROM,'[email protected]'
html_content =
loader.get_template('active.html').render({'username': '⼩花猫'})
msg = EmailMultiAlternatives(subject, from_email=from_email, to=
[to])
msg.attach_alternative(html_content, "text/html")
msg.send()
return HttpResponse('发送html的⽂件内容')
富文本编辑器
1、安装
pip install django-tinymce
2、settings.py设置
INSTALLED_APPS = [
...
'tinymce',
]
#富⽂本编辑器的配置
TINYMCE_DEFAULT_CONFIG = {
'theme':'advanced',
'width':600,
'height':400
}
3、实现
<head>
<script src="/static/tinymce/tinymce.min.js"></script>
<script>
tinyMCE.init({
'mode': 'textareas',
'width': 800,
'height': 600,
})
</script>
</head>
<body>
<form action="{% url 'App:edit' %}" method="POST">
{% csrf_token %}
<p>标题 <input type="text" name="title" placeholder="请输⼊标题"
maxlength="20" required></p>
<textarea name="content" id="" cols="30" rows="10"></textarea>
<input type="submit">
</form>
</body>
</html>
app_name='App'
urlpatterns = [
path('edit/',views.edit,name='edit'),
]
def edit(request):
if request.method == 'POST':
print(request.POST.get('content'))
return render(request,'article.html')
标签:文本编辑,settings,request,验证码,captcha,html,邮箱,EMAIL
From: https://www.cnblogs.com/zaosong/p/17048443.html