前记:知识无处不在,要懂得珍惜,找到适合自己的方法高效地学习有价值的知识,不念过去,不畏将来。
Django对待静态资源,在非前后端分离时的常识
Django会对项目app下的static文件夹的静态资源进行收集,同名则按优先级指向,要自己加资源可以在settings.py的STATICFILES_DIRS进行额外指定,STATIC_URL=‘/static/',会对外监听例如:127.0.0.1:8000/static/*,STATIC_ROOT = os.path.join(BASE_DIR,'all_static')用于项目生产部署,在项目的开发过程中作用不大,平时都是前后端分离的,对生产环境的STATIC_ROOT没有感知,还不如好好学一下vue。
在云服务器部署项目时,Nginx正向代理
代理静态资源时,如果是build完的文件夹放服务器要注意位置,绝对路径或正确的相对路径,相对路径是基于nginx.conf进行相对的,include的配置文件别搞错了,还是绝对路径稳,然后要注意Windows和Centos7的文件资源路径为\和/的区别。
关于Django的Form校验json格式
在上传购物车之类的复杂数据时,可用json进行包装,Form对json进行校验,使用json_schema,之前都是根据检验目标自己写json_schema,后来发现可以在线生成的。例如:https://jsonschema.net/
class FormAddOrder(forms.Form): # 新增订单校验json
json_data_str = forms.CharField(max_length=1024)
def clean(self): # 重写clean,可以覆盖jaon_data_str,这里没覆盖
cleaned_data = self.cleaned_data
json_data = json.loads(cleaned_data["json_data_str"])
json_schema = {
"type": "object",
"required": ["type", "client", "warehouse", "send_way", "count_type", "real_price",
"need_price", "orders"],
"properties": {
"type": {"type": "string"},
"client": {"type": "integer"},
"warehouse": {"type": "integer"},
# "cabinets": {"type": "string"},
"send_way": {"type": "string"},
"count_type": {"type": "string"},
"real_price": {"type": "number"},
"need_price": {"type": "number"},
"orders": {
"type": "array",
"items": {
"type": "object",
"required": ["num", "need_price", "new_need_price", "batch", "goods"],
"properties": {
"num": {"type": "integer"},
"need_price": {"type": "number"},
"new_need_price": {"type": "number"},
"batch": {"type": "string"},
"goods": {"type": "integer"},
}
}
},
}
}
try:
validate(json_data, json_schema)
except Exception as e:
raise forms.ValidationError(e)
return cleaned_data
校验对象:
{
"type": "销售订单",
"client": 11,
"warehouse": 1,
"cabinets": "aa564879564",
"send_way": "快递",
"count_type": "现结付清",
"real_price": 3000,
"need_price": 3888,
"orders": [{
"num": 100,
"need_price": 110,
"new_need_price": 100,
"batch": "2019888",
"goods": 1
}, {
"num": 200,
"need_price": 220,
"new_need_price": 200,
"batch": "2019888",
"goods": 1
}]
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:杂记:Django和static,Nginx配置路径,json_schema - Python技术站