1.添加精确搜索功能的时候报错:
views.py
from django_filters.rest_framework import DjangoFilterBackend #精确搜索
class GoodsListViewSet(mixins.ListModelMixin,viewsets.GenericViewSet):
'''
第六版,完美返回JASON数据,代码简洁加强版,加深度定制分页,动态简便绑定http提交方法:router.register(r'goods',views.GoodsListViewSet)
'''
queryset = Goods.objects.all()
serializer_class = GoodsSerializer
pagination_class = GoodsPagination #分页
#精确过滤搜索
filter_backends = (DjangoFilterBackend,)
filter_fields = ('name', 'shop_price',)
settings.py
#精确搜索
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',)
}
报错信息:
django.template.exceptions.TemplateDoesNotExist: django_filters/rest_framework/crispy_form.html
添加该APP后解决问题:
INSTALLED_APPS = [
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'users.apps.UsersConfig',
'DjangoUeditor',
'goods',
'trade',
'user_operation',
'xadmin',
'crispy_forms',
#DRF配置
'rest_framework',
#精确搜索
'django_filters',
]
2.Vue项目,单个接口调试的时候
let local_host = 'http://127.0.0.1:8000/';
//获取商品类别信息
export const getCategory = params => {
if('id' in params){
return axios.get(`${local_host}/categorys/`+params.id+'/');
}
else {
return axios.get(`${local_host}/categorys/`, params);
}
};
mock.js:8359 GET http://127.0.0.1:8000//categorys/ net::ERR_CONNECTION_REFUSED
Failed to load http://127.0.0.1:8000//categorys/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 404.
#跨域
'corsheaders',
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
...
]
CORS_ORIGIN_ALLOW_ALL = True