需求:实现三种通知功能 #方式一: #notify.py def send_email(content): print('邮箱通知:%s'% content) def send_msg(content): print('短信通知:%s'% content) def send_wechat(content): print('微信通知:%s'% content) #start.py from first.notify import * def send_all(content): send_msg(content) send_email(content) send_wechat(content) if __name__ == '__main__': send_all('后天是周末') #方式二: #lib文件夹 #bbb.py name='from bbb' #aaa.py import importlib res='lib.bbb' #利用字符串形式导入模块 md=importlib.import_module(res) #这句至关于 from lib import bbb print(md) #该模块字符串最小单位只能到文件名 #settings.py NOTIFY_LIST=[ 'notify.email.Email', 'notify.msg.Msg', 'notify.qq.Qq', 'notify.WeChat.WeChat', ] #start.py from notify import * send_all('周末了,哈哈哈哈') #notify 包文件 #email.py class Email(object): def __init__(self): pass def send(self,content): print('邮件通知:%s'%content) #msg.py class Msg(object): def __init__(self): pass def send(self, content): print('短信通知:%s' % content) #qq.py class Qq(object): def __init__(self): pass def send(self, content): print('qq通知:%s' % content) #wechat.py class WeChat(object): def __init__(self): pass def send(self, content): print('微信通知:%s' % content) #__init__.py import settings import importlib def send_all(content): for path in settings.NOTIFY_LIST: #拿到一个个的路径字符串 'notify.email.Email' module_path,cls_name=path.rsplit('.',maxsplit=1) #字符串'notify.email' 类cls_name='Email' md=importlib.import_module(module_path) #from notify import email cls=getattr(md,cls_name) #获取到文件夹中类的名字 obj=cls() #产生一个个类对象 obj.send(content) #鸭子类型
如何实现钓鱼网站html
模拟该现象的产生 建立两个django项目python
#正经网站 #transfer.py def transfer(request): if request.method=='POST': username=request.POST.get('username') target_user=request.POST.get('target_user') money=request.POST.get('money') print('%s 给 %s 转了 %s钱'%(username,target_user,money)) return render(request,'transfer.html') #transfer.html <p>这是正经网站</p> <form action="" method="post"> <p>username:<input type="text" name="username"></p> <p>target_account:<input type="text" name="target_user"></p> <p>money:<input type="text" name="money"></p> <input type="submit"> #钓鱼网站 #transfer.py def transfer(request): return render(request,'transfer.html') #transfer.html <p>这是假冒的网站</p> <form action="http://127.0.0.1:8000/transfer/" method="post"> <p>username:<input type="text" name="username"></p> <p>target_account: <input type="text"> <input type="text" name="target_user" value="ldd" style="display: none"></p> <p>money:<input type="text" name="money"></p> <input type="submit">
的value是一个随机字符串 可是网站可以记住每个浏览器发送的随机字符串ajax
之后在写form表单的时候 只须要在表单中写一个{% csrf_token %} <p>这是正经网站</p> <form action="" method="post"> {% csrf_token %} <p>username:<input type="text" name="username"></p> <p>target_account:<input type="text" name="target_user"></p> <p>money:<input type="text" name="money"></p> <input type="submit">
{% csrf_token %} <button id="d1">发送ajax</button> <script src="/static/setup.js"></script> //js文件相关 <script> $('#d1').click(function () { $.ajax({ url:'', type:'post', // 第一种 data:{'username':'jason','csrfmiddlewaretoken':$('input[name="csrfmiddlewaretoken"]').val()}, // 第二种 data:{'username':'jason','csrfmiddlewaretoken':'{{ csrf_token }}'}, // 第三种 利用脚本文件 data:{'username':'jason'}, success:function (data) { alert(data) } }) }) </script>
静态文件夹里的js文件: function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie !== '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) === (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } var csrftoken = getCookie('csrftoken'); function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie !== '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) === (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } var csrftoken = getCookie('csrftoken');
将这些代码的文件配置到你的Django项目的静态文件中,在html页面上经过导入该文件便可自动帮咱们解决ajax提交post数据时校验csrf_token的问题,(导入该配置文件以前,须要先导入jQuery,由于这个配置文件内的内容是基于jQuery来实现的)django
from django.views.decorators.csrf import csrf_exempt,csrf_protect # @csrf_exempt # 不校验 csrf def index(request): return HttpResponse('index') @csrf_protect # 校验 def login(request): return HttpResponse('login')
# @method_decorator(csrf_exempt,name='post') # csrf_exempt不支持该方法 @method_decorator(csrf_exempt,name='dispatch') # csrf_exempt class MyIndex(views.View): # @method_decorator(csrf_exempt) # 能够 def dispatch(self, request, *args, **kwargs): return super().dispatch(request,*args,**kwargs) def get(self,request): return render(request,'transfer.html') # @method_decorator(csrf_exempt,name='post') # csrf_exempt不支持该方法 def post(self,request): return HttpResponse('OK') # csrf_exempt这个装饰器只能给dispatch装才能生效 """ csrf_protect方式全均可以 跟你普通的装饰器装饰CBV一致 """ # @method_decorator(csrf_protect,name='post') # 能够 class MyIndex(views.View): @method_decorator(csrf_protect) def dispatch(self, request, *args, **kwargs): return super().dispatch(request,*args,**kwargs) def get(self,request): return render(request,'transfer.html') # @method_decorator(csrf_protect) # 能够 def post(self,request): return HttpResponse('OK')
from django.conf import global_settings from django.conf import settings
使用auth模块的时候,要用就用它的全套后端
如何建立超级用户(root)浏览器
python manage.py createsuperuser
1.建立用户 from django.contrib.auth.models import User # User.objects.create(username=username,password=password) # 不可用 密码不是加密的 # User.objects.create_user(username=username,password=password) # 建立普通用户 密码自动加密 #User.objects.create_superuser(username=username,password=password,email='123@qq.com') # 建立超级用户 须要邮箱数据 2.校验用户名和密码是否正确 from django.contrib import auth user_obj = auth.authenticate(request,username=username,password=password) # 必须传用户名和密码两个参数缺一不能 3.保存用户登陆状态 auth.login(request,user_obj) # 只要这句话执行了 后面在任意位置 只要你能拿到request你就能够经过request.user获取到当前登陆的用户对象 4.判断当前用户是否登陆 request.user.is_authenticated() 5.校验原密码是否正确 request.user.check_password(old_password) 6.修改密码 request.user.set_password(new_password) request.user.save() # 千万不要忘了 7.注销 auth.logout(request) 8.校验用户是否登陆装饰器 from django.contrib.auth.decorators import login_required 局部配置 @login_required(login_url='/login/') def index(request): pass 全局配置 settings配置文件中 直接配置 LOGIN_URL = '/login/' @login_required def index(request): pass # 若是全局配置了 局部也配置 以局部的为准
方式1 利用一对一外键字段关系 class UserDetail(models.Model): phone = models.BigIntegerField() user = models.OneToOneField(to='User') 方式2 利用继承关系 from django.contrib.auth.models import AbstractUser class Userinfo(AbstractUser): phone = models.BigIntegerField() register_time = models.DateField(auto_now_add=True) # 必定要注意 还须要去配置文件中配置 AUTH_USER_MODEL = 'app01.Userinfo' # 应用名.表名 #以前全部的auth模块功能也全适用于扩展好的表