django models 设置默认的查询排序方式:
class Meta:
ordering = ("-create_time")
form 继承models
from django import forms
from apps.user import UserModel
class User(forms.Form):
class Meta:
models = UserModel
# -*- coding: utf-8 -*-
import re
from django import forms
from django.core.exceptions import ValidationError
def mobile_validate(value):
mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
if not mobile_re.match(value):
raise ValidationError('手机号码格式错误')
def username_validate(value):
mobile_re = re.compile(r'^[0-9A-Za-z.@\-\_]{2,16}$')
if not mobile_re.match(value):
raise ValidationError('用户名格式错误(不能是中文)')
class UserForm(forms.Form):
"""user 表单"""
userName = forms.CharField(required=True, min_length=2, max_length=60, validators=[username_validate,])
depart_id = forms.CharField(required=False)
depart_name = forms.CharField(required=False)
role = forms.CharField(required=False)
userPwd = forms.CharField(max_length=20, min_length=6,
error_messages={'required':'密码不能为空','min_length':'密码长度不小于6',"max_length":'密码长度不能大于12'})
userEmail = forms.EmailField( required=True, error_messages={'required': '邮箱不能为空.','invalid':"邮箱格式错误"})
Name = forms.CharField( required=False)
phone = forms.CharField(required=False, validators=[mobile_validate, ])
area_level = forms.CharField(validators=[
RegexValidator(r'^[123]$', '请检查级别'),
]
)
area_status = forms.CharField(validators=[
RegexValidator(r'^[01]$', '请检查状态'),
])
area_order = forms.CharField(validators=[
RegexValidator(r'^[0-9]{1,2}$', '请输入2位数字'),
])
views 装饰器的使用
def pre_fun(func):
def inner(request, *args, **kwargs):
print(request.path)
kwargs = {
"name": "你好",
"kw": "asd"
}
return func(request, *args, **kwargs)
return inner
def my_decorator(func):
def wrapper(request, *args, **kwargs):
print('自定义装饰器被调用了')
print('请求路径%s' % request.path)
return func(request, *args, **kwargs)
return wrapper
@pre_fun
def index(request, *args, **kwargs):
print(kwargs)
return render(request, 'index.html', {'kw': kwargs})
@method_decorator(pre_fun, name="get")
class Index2View(View):
def get(self, request, *args, **kwargs):
return render(request, 'index.html', {'kw': kwargs})
class Index2View(View):
@method_decorator(pre_fun)
def get(self, request, *args, **kwargs):
return render(request, 'index.html', {'kw': kwargs})
urlpatterns = [
path('index', my_decorator(IndexView.as_view()), name='index'),
path('', index, name='index1'),
path('index2', Index2View.as_view(), name='index2'),
]
django模板全局变量的使用
第一步:写好全局变量的函数方法:
def global_lang(request):
“”“
这里可以做一些用户权限的权限的处理
request.user
request.path
request.session['key']
”“”
return {
'header_json':header_json
}
第二步:在settings 中进行配置
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates'), ],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'XXX.XXX.global_lang'
],
},
},
]
第三步:在HTML中使用全局变量
{% include 'header.html' with header=header_json%}
或者
{{header_json}}