table of Contents
1. render the principle of
2.json knowledge
End is returned to the 2.1 format string json
- Why give end is returned to
json
the format string?
》》前后端分离
》》基于json格式传输数据
》》后端就专门写接口
》》前端调用接口
》》拿到json格式的字符串
》》然后前端利用序列化反序列化
》》转换成前端对应的数据类型
How front-end serialization deserialization?
#前端 JSON.stringify >>> json.dumps JSON.parse >>> json.loads #后端 json.dumps >>> JSON.stringify json.loads >>> JSON.parse #js常用数据类型 数值、字符、数组[]、自定义对象、undefined、null、布尔值 true/false(小写)、symbol
2.2 shape json
2.3 Chinese garbled
Way Out:
- method one:
Plus a
ensure_ascii
Method Two:
To the source code in the
json_dumps_parms
default empty dictionary plus a key-value pair
2.4 json serialization time type
Note: default is the default serialization does not support datetime
The Solution: Let json
support serialization time type
- method one:
dumps
Where thecls
default can only do the function that table- We redefine a class
- Override the default method
- If
o
notjson
default can be serialized - Other methods only need to rewrite
datetime
changed the way you want
2.5 JsonResponse
JsonResponse
Only supports default dictionary
Note: If you want to serialize other types (json can support type), just add a safe = False.
3. form form to upload files
problem:
- CRSF error - Turn the MIDDLEWARE
django.middleware.csrf.CsrfViewMiddleware
- You can only get the file name
analysis:
- The file name can not get, because for the file data is not put inside request.POST
- But on the inside request.FILES
Way Out:
Join in the single form
enctype="multipar/form-data"
insert
<input type="file" name="myfile">
Get File Object
Note: To distinguish POST / GET
- Obtain
Storage
note! ! ! There are two points to note! ! !
First, I am not Chu Yu Qian!
Second, I do not call Hey!
fine, joke, ha ha ha
- First, the submission must be post
- Second, enctype parameter must have a default urlencoded become formdata
Knock knock blackboard blackboard!
4. FBV and CBV (i.e., source code analysis CBV)
4.1 FBV (Function Based View) based on the view function
4.2 CBV (Class Based View) based on the view class
Before urls views correspondence function, like how to write now?
url(r'^login/', views.MyReg.as_view())
By analyzing as_view () source, we found
类.方法
FBV corresponds to the previous文件名.函数
, corresponding to a closure function
Why will perform front-end came GET GET, POST will be executed came POST?
- CBV route
url(r'^reg/',views.MyReg.as_view()) @classonlymethod def as_view(cls, **initkwargs): def view(request, *args, **kwargs): self = cls(**initkwargs) # cls就是我们自己的写的MyReg类 if hasattr(self, 'get') and not hasattr(self, 'head'): self.head = self.get self.request = request self.args = args self.kwargs = kwargs # 上面的一通操作 就是给我们自己写的类的对象赋值 return self.dispatch(request, *args, **kwargs) # 对象在查找属性或方法的时候 顺序是什么? 先从自己找 再从产生对象的类中找 再去类的父类中找... """也就意味着你在看源码的时候 你一定要牢记上面的话""" return view # views.py from django.views import View
class MyReg(View): def get(self,request): return render(request,'reg.html') def post(self,request): return HttpResponse("我是MyReg类中post方法")
- The most part of the essence CBV
def dispatch(self, request, *args, **kwargs): if request.method.lower() in self.http_method_names: # 判断当前请求方式在不在默认的八个请求方式中 handler = getattr(self, request.method.lower(), self.http_method_not_allowed) # handler = getattr(自己写的类产生的对象,'小写的请求方法(get\post)','获取不到对应的方法就报错') # handler就是我们自己定义的跟请求方法相对应的方法的函数内存地址 else: handler = self.http_method_not_allowed return handler(request, *args, **kwargs) # 在调用获取到的方法
5. django settings source analysis and practical
5.1 django profile
Is exposed to a user can customize the configuration
One is the default global profile
The user specifies the user to use the
user does not specify to use the default
from django.conf import settings
settings = LazySettings()
class LazySettings(LazyObject):
def _setup(self, name=None):
# os.environ你可以把它看成是一个全局的大字典
settings_module = os.environ.get(ENVIRONMENT_VARIABLE) # 从大字典中取值
# settings_module = 'day59.settings'
self._wrapped = Settings(settings_module) # Settings('day59.settings')
class Settings(object):
def __init__(self, settings_module): # settings_module = 'day59.settings'
for setting in dir(global_settings): # 循环获取global_settings文件中所有的名字
if setting.isupper(): # 在判断名字是否是大写
# 如果是大写 利用反射 获取到大写的名字所对应的值 不停地添加到对象中
setattr(self, setting, getattr(global_settings, setting))
# store the settings module in case someone later cares
self.SETTINGS_MODULE = settings_module
mod = importlib.import_module(self.SETTINGS_MODULE) # 'day59.settings'
# from day59 import settings
# mod 指代的就是暴露给用户的配置文件模块名
for setting in dir(mod): # 循环获取暴露给用户配置文件中所有的名字
if setting.isupper(): # 判断是否是大写
setting_value = getattr(mod, setting) # 如果是大写 获取大写的变量名所对应的值
setattr(self, setting, setting_value) # 不停的给对象设置值
Collated blog, and then review it again, wash sleep!