基於url的get傳參方式html
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'
},vue
'DEFAULT_VERSION': 'v1', # 默認版本(從request對象裏取不到,顯示的默認值)
'ALLOWED_VERSIONS': ['v1', 'v2'], # 容許的版本
'VERSION_PARAM': 'version' # URL中獲取值的key
}python
基於url的正則方式jquery
url(r'^(?P<version>[v1|v2]+)/publishs/', views.PublishView.as_view()),ajax
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 os import time import django from django.core.cache import cache os.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) pass if __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)
}
}
}django
views:json
from django.shortcuts import render,HttpResponse # Create your views here. from django.views.decorators.cache import cache_page import time # 單頁面緩存緩存5秒 # @cache_page(20) def test(request): print('我來了') ctime=time.time() return render(request,'test.html',locals())
templates:跨域
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="/static/jquery-3.3.1.js"></script> <title>Title</title> </head> <body> 不緩存的 {{ ctime }} <br> 緩存的的 {#{% load cache %}#} {#第一個參數是超時時間,緩存時間,第二個參數是key值,別名#} {#{% cache 10 'test' %}#} {# {{ ctime }}#} {#{% endcache %}#} <button id="btn">點我發請求</button> </body> <script> $("#btn").click(function () { $.ajax({ url:'http://127.0.0.1:8000/v1/publishs/', type:'get', {# type:'delete',#} {# type:'put',#} {# contentType:'application/json',#} {# data:JSON.stringify({'name':'lqz'}),#} success:function (data) { console.log(data) } }) }) </script> </html>
from django.utils.deprecation import MiddlewareMixin class 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