首页 > 其他分享 >图片验证码

图片验证码

时间:2023-05-30 09:34:55浏览次数:32  
标签:username form admin request 验证码 django captcha 图片

图片验证码

Captcha is one of the modern ways used for verification in websites;
it is very cool way and every second website is using it. You can use
Google captcha but it is really a headache to apply it; however in
Django, we have a simpler method to do so.

In this article, we are going to learn how to create a captcha in a Django website. So, let's get started.

Example

First of all, create a Django project and an app.

Now install the django-simple-captcha library −

pip install django-simple-captcha

Go to settings.py and inside INSTALLED_APPS, add your app and "captcha": −

INSTALLED_APPS = [
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   "captchaproject", #My app name
   "captcha" # the module name
]

It will add captcha as an app.

In project's root directory, add the following lines −

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
   path('admin/', admin.site.urls),
   path('', include("captchaproject.urls")),
   path('/captcha',include("captcha.urls"))
]

It will add url for my app and the captcha url where verification will occur.

In app's main directory, create a forms.py and type −

from django import forms
from captcha.fields import CaptchaField
class MyForm(forms.Form):
   captcha=CaptchaField()

Here we created a form with captcha field.

Don't forget to run "python manage.py migrate"

Now in app's view.py, add the following lines −

from django.shortcuts import render
from .forms import MyForm

Create your views here.

def home(request):
   if request.method=="POST":
      form=MyForm(request.POST)
      if form.is_valid():
         print("success")
      else:
         print("fail")
   form=MyForm()
   return render(request,"home.html",{"form":form})

We render the form in home view and in POST handler, we verify the
form or we can say captcha and after verification, we print the result.

Create an HTML file in the templates directory (directory where you add every HTML or CSS file that you
render, I assume you know and already configure it) and add the
following lines −

          Tut              
         {%csrf_token%}          {{form.captcha}}                
   

This is the frontend where we are rendering the captcha form.

We are done; now you can proceed to check the output.

Output


按照上述步骤,注意captcha URL路由按照上述指示在settings.py配置
然后无需做其他,在views方法中
def account_login(request):
# if request.method == 'GET':
# return render(request,'account_login.html')
# username = request.POST.get('username')
# password = encrypt(request.POST.get('password'))
# print(password)
# admin_object = models.Admin.objects.filter(username=username, password=password).first()
# if not admin_object:
# return render(request,'account_login.html')
# request.session['info'] = {'id':admin_object.id,'username':admin_object.username}
# return redirect('/admin/list/')
if request.method == 'GET':
form = LoginForm()
print(form)
return render(request,'account_login.html',{'form':form})
form = LoginForm(data=request.POST)
if form.is_valid():
admin_object = models.Admin.objects.filter(username=form.cleaned_data.get('username'),password=form.cleaned_data.get('password') ).first()
if not admin_object:
form.add_error('password','用户名或者密码错误')
return render(request,'account_login.html',{'form':form})
else:
request.session['info'] = {'id':admin_object.id,'username':admin_object.username}
return redirect('/admin/list/')
return render(request,'account_login.html',{'form':form})

标签:username,form,admin,request,验证码,django,captcha,图片
From: https://www.cnblogs.com/jason-huawen/p/17442266.html

相关文章

  • 如何在图片上编辑文字?图片编辑文字软件分享!​
    如何在图片上编辑文字?在现代社交媒体和广告营销中,图片已经成为一个非常重要的媒介。在图片上传到互联网后,为了更好地传达信息,有时需要在图片上添加文字,这些文字可以用于解释图片中的内容,提供附加信息,或者吸引观众的注意力,那么该用什么软件进行操作呢?很多小伙伴不知道,下面感兴趣的就......
  • 图片png怎么转换成jpg格式?图片格式转换软件推荐!​
    图片png怎么转换成jpg格式,PNG和JPG都是非常流行的格式,因为JPG是一种有损压缩格式,相对于PNG格式,它可以将图像的大小缩小很多,这意味着您可以在不牺牲太多图像质量的情况下节省更多的存储空间,所以很多小伙伴想转换却不知道使用什么软件,下面感兴趣的小伙伴一起来看看吧!选择:图片编辑助手......
  • web基础漏洞-验证码爆破
    1、介绍这里的验证码是指在注册、登录、找回密码、重要操作验证身份时,服务端向用户的手机或者邮箱发送验证码,用户输入匹配成功以验证身份。验证码爆破漏洞,是服务端未进行次数和时间限制,或者允许的范围过大。导致攻击者可以反复尝试不同的验证码,以获取到正确的验证码。2、测试......
  • 颠覆性新工具:瞬间生成相似图片!
    根据给定图片生成类似的图片,如果有这样的工具就太方便了。比如你是一名设计师,给客户设计了一张图,如果能快速生成几张类似的图片,是不是就给客户提供了更大的选择空间。再比如你平时写文章、做PPT,需要用到配图,使用网上的图片会有什么问题?可不可以用这个工具呢?可以发挥自己想象力......
  • 直播app开发搭建,Vue Element UI Upload 上传多张图片
    直播app开发搭建,VueElementUIUpload上传多张图片<template> <div>  <el-card>   <h1>轮播图-图片上传管理</h1>   <el-divider></el-divider>   <!--注意:使用:model="uploadImgForm"不要使用v-model="uploadImgForm&q......
  • 3万多条含图片的菜谱资料ACCESS\EXCEL数据库
    我记得之前我采集、提取过两份菜谱资料数据库,一份是菜谱数据不上万的带图片的,详情点击这里,一份是2万多条记录但格式未整理并且没有图片的。而今天的这个3万多条含图片的菜谱资料ACCESS数据库包含了3万多条常菜谱做法,并且结构简洁,最主要的是每个菜谱都包含图片。菜谱分50个大类......
  • uniapp避坑指南-图片预览
    uniapp避坑指南-图片预览(本地预览)需求描述有一个类似以下的数组,通过v-for循环,现在需要在用户点击图片后使用uniapp的图片预览Api。//view<viewv-for:'(item,index)inpicList':key="index"><image:src="item.url"@click='imgPreview(item.url,id)'>......
  • 发布-优化图片和js文件的存放路径
    把JavaScript文件同意生成到js目录中在webpack.config.js配置文件的output节点中,进行如下的配置:{test:/\.jpg|png|gif$/,use:'url-loader?limit=470&outputPath=images'}......
  • 验证码模型训练与识别
    1.训练模型代码importnumpyasnpimporttensorflowastf#importtensorflow.compat.v1astf#tf.disable_v2_behavior()fromcaptcha.imageimportImageCaptchaimportnumpyasnpimportmatplotlib.pyplotaspltfromPILimportImageimportrandomnumber=......
  • TransformMine图片表格化安卓APP
    TransformMine图片表格化安卓APP展示: 部分代码:<?xmlversion="1.0"encoding="utf-8"?><com.scwang.smart.refresh.layout.SmartRefreshLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://s......