博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
drf版本控制 和django缓存,跨域问题,
阅读量:7125 次
发布时间:2019-06-28

本文共 3703 字,大约阅读时间需要 12 分钟。

drf版本控制

 

基于url的get传参方式

REST_FRAMEWORK={

# "DEFAULT_AUTHENTICATION_CLASSES":["app01.auth.AuthLogin",],
# "DEFAULT_PERMISSION_CLASSES":['app01.auth.MyPer'],
# "DEFAULT_THROTTLE_CLASSES":["app01.auth.VisitThrottle"],
'DEFAULT_PARSER_CLASSES':['rest_framework.parsers.JSONParser'],
'DEFAULT_THROTTLE_RATES': {
'luffy': '3/mmmmmm'
},

'DEFAULT_VERSION': 'v1', # 默认版本(从request对象里取不到,显示的默认值)

'ALLOWED_VERSIONS': ['v1', 'v2'], # 允许的版本
'VERSION_PARAM': 'version' # URL中获取值的key
}

基于url的正则方式

 url(r'^(?P<version>[v1|v2]+)/publishs/', views.PublishView.as_view()),

 

 

django缓存

 

 

CACHES = {    'default': {        # 1. MemCache 基于MemCache的缓存        # 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',        # 'LOCATION': '127.0.0.1:11211',        # 2. DB Cache 基于数据库的缓存        # 'BACKEND': 'django.core.cache.backends.db.DatabaseCache',        # 'LOCATION': 'my_cache_table',        # 3. Filesystem Cache 基于文件的缓存        # 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',        # 'LOCATION': '/var/tmp/django_cache',        # 4. Local Mem Cache 基于内存的缓存        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',        'LOCATION': 'backend-cache'    }}import osimport timeimport djangofrom django.core.cache import cacheos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')django.setup()def basic_use():    s = 'Hello World, Hello Django Cache.'    cache.set('key', s)    cache_result = cache.get('key')    print(cache_result)    s2 = 'Hello World, Hello Django Timeout Cache.'    cache.set('key2', s2, 5) #5是超时时间    cache_result = cache.get('key2')    print(cache_result)    time.sleep(5) #5秒后就查不到了    cache_result = cache.get('key2')    print(cache_result)    passif __name__ == '__main__':    basic_use()#实例# 星座运势def constellation(request):    data = []    if already_authorized(request):        user = get_user(request)        constellations = json.loads(user.focus_constellations)    else:        constellations = all_constellations    for c in constellations:        result = cache.get(c)        if not result:            result = thirdparty.juhe.constellation(c)            timeout = timeutil.get_day_left_in_second()            cache.set(c, result, timeout)            logger.info("set cache. key=[%s], value=[%s], timeout=[%d]" %(c, result, timeout))        data.append(result)    response = CommonResponseMixin.wrap_json_response(data=data, code=ReturnCode.SUCCESS)    return JsonResponse(response, safe=False)

  

 

 

 

settings:

CACHES = {

'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', # 指定缓存使用的引擎
'LOCATION': r'D:\lqz\vue', # 指定缓存的路径
'TIMEOUT': 300, # 缓存超时时间(默认为300秒,None表示永不过期)
'OPTIONS': {
'MAX_ENTRIES': 300, # 最大缓存记录的数量(默认300)
'CULL_FREQUENCY': 3, # 缓存到达最大个数之后,剔除缓存个数的比例,即:1/CULL_FREQUENCY(默认3)
}
}
}

 

views:

from django.shortcuts import render,HttpResponse# Create your views here.from django.views.decorators.cache import cache_pageimport time# 单页面缓存缓存5秒# @cache_page(20)def test(request):    print('我来了')    ctime=time.time()    return render(request,'test.html',locals())

  

templates:

    
Title不缓存的{
{ ctime }}
缓存的的{#{% load cache %}#}{#第一个参数是超时时间,缓存时间,第二个参数是key值,别名#}{#{% cache 10 'test' %}#}{# {
{ ctime }}#}{#{% endcache %}#}

  跨域问题

 

 

from django.utils.deprecation import MiddlewareMixinclass CORSMiddle(MiddlewareMixin):    def process_response(self,request,response):        response['Access-Control-Allow-Origin'] = 'http://127.0.0.1:8001'        if request.method == 'OPTIONS':            response['Access-Control-Allow-Methods'] = 'PUT,DELETE'            response['Access-Control-Allow-Headers']='Content-Type'        return response

 

转载于:https://www.cnblogs.com/du-jun/p/10450567.html

你可能感兴趣的文章
2019年学习博文分享
查看>>
用python实现九九乘法表
查看>>
Mindscape WebWorkbench 支持Visual Studio 2012 最新破解
查看>>
大型网站技术架构(四)网站的高性能架构
查看>>
Log4j 1使用教程
查看>>
详解haproxy
查看>>
资深老师给小学生家长的建议
查看>>
MyBatis学习总结(14)——Mybatis使用技巧总结
查看>>
Windows server 2012 /2008从Windows Server Core 模式服务器转换为完全安装(图形模式)...
查看>>
<org manual>翻译--3.5.9 高级特性
查看>>
awk && sed (1)====积累取ip以及sed 查找替换
查看>>
SQL 内链接Inner Join和外连接Outer Join
查看>>
常用HTML正则表达式
查看>>
Tomcat,JDK,MySQL 官方历史版本下载链接
查看>>
Linux lsof命令详解
查看>>
我是如何破解你的WINDOWS密码的 ?(1)
查看>>
SQL Server: Top 10 Secrets of a SQL Server Expert
查看>>
loop循环
查看>>
laravel完美部署与六种解决报错高效方法
查看>>
iscsi多路径客户端的配置
查看>>