django-学习-验证码

写一个前后端分离的小项目,一边看django,一边看vue.
在写用户登录注册的时候,需要验证码,这时候用到了django-simple-captcha
下面是这个其中的一个模型类:

>>> 

class CaptchaStore(django.db.models.base.Model)
 |  CaptchaStore(*args, **kwargs)
 |
 |  CaptchaStore(id, challenge, response, hashkey, expiration)
 |
 |  Method resolution order:
 |      CaptchaStore
 |      django.db.models.base.Model
 |      builtins.object
 |
 |  Methods defined here:
 |
 |  __str__(self)
 |      Return str(self).
 |
 |  challenge = <django.db.models.query_utils.DeferredAttribute object>
 |  expiration = <django.db.models.query_utils.DeferredAttribute object>
 |  get_next_by_expiration = _method(self, *, field=<django.db.models.fields.DateTimeField: expiration>, is_next=True, **kwargs)
 |
 |  get_previous_by_expiration = _method(self, *, field=<django.db.models.fields.DateTimeField: expiration>, is_next=False, **kwargs)
 |
 |  hashkey = <django.db.models.query_utils.DeferredAttribute object>
 |  id = <django.db.models.query_utils.DeferredAttribute object>
 |  response = <django.db.models.query_utils.DeferredAttribute object>
 |  save(self, *args, **kwargs)
 |      Save the current instance. Override this in a subclass if you want to
 |      control the saving process.
 |
 |      The 'force_insert' and 'force_update' parameters can be used to insist
 |      that the "save" must be an SQL insert or update (or equivalent for
 |      non-SQL backends), respectively. Normally, they should not be set.
 |
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |
 |  create_pool(count=1000) from django.db.models.base.ModelBase
 |	
 |  generate_key(generator=None) from django.db.models.base.ModelBase
 |
 |  pick() from django.db.models.base.ModelBase
 | 
 |  remove_expired() from django.db.models.base.ModelBase
    调用它可以一次删除过期的数据库中的行
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用第三方库django-simple-captcha来实现AdminLTE图形验证码的功能。具体实现方法可以参考以下步骤: 1. 安装django-simple-captcha库 可以使用pip命令进行安装: ``` pip install django-simple-captcha ``` 2. 在settings.py中添加captcha应用 在INSTALLED_APPS中添加captcha应用: ``` INSTALLED_APPS = [ ... 'captcha', ... ] ``` 3. 配置captcha应用 在settings.py中添加captcha应用的配置: ``` CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.random_char_challenge' CAPTCHA_LENGTH = 4 CAPTCHA_FONT_SIZE = 30 CAPTCHA_TIMEOUT = 5 ``` 其中,CAPTCHA_CHALLENGE_FUNCT指定验证码生成函数,CAPTCHA_LENGTH指定验证码长度,CAPTCHA_FONT_SIZE指定验证码字体大小,CAPTCHA_TIMEOUT指定验证码过期时间。 4. 在表单中添加captcha字段 在表单中添加captcha字段: ``` from captcha.fields import CaptchaField class MyForm(forms.Form): ... captcha = CaptchaField() ... ``` 5. 在模板中显示captcha字段 在模板中使用form.captcha来显示captcha字段: ``` {% load captcha %} ... <form method="post"> {% csrf_token %} {{ form.as_p }} <button type="submit">Submit</button> </form> ... {% captcha_script %} ``` 6. 验证captcha字段 在视图中验证captcha字段: ``` from captcha.fields import CaptchaField def my_view(request): if request.method == 'POST': form = MyForm(request.POST) if form.is_valid(): # 验证码验证通过 ... else: # 验证码验证失败 ... else: form = MyForm() return render(request, 'my_template.html', {'form': form}) ``` 至于AdminLTE的相关内容,可以参考官方文档进行学习

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值