1. Add accurate search function when the error:
views.py
From django_filters.rest_framework import djangofilterbackend #精确搜索
Class Goodslistviewset (mixins. Listmodelmixin,viewsets. Genericviewset):
'
Sixth Edition, the perfect return of Jason Data, code concise enhanced version, depth of custom paging, dynamic and easy binding HTTP submission method: 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 ',)
}
Error message:
Django.template.exceptions.templatedoesnotexist:django_filters/rest_framework/crispy_form.html
The reason is that the tutorial is not registered in the setting.py file: Crispt_form
Solve the problem after adding the 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 configuration
' rest_framework ',
#精确搜索
' django_filters ',
]
2.Vue project, when a single interface is debugged
Change the domain name of the Vue interface to local
Let Local_host = ' http://127.0.0.1:8000/';
Get catalog Information
Export Const GetCategory = params => {
if (' id ' in params) {return
axios.get (' ${local_host}/ categorys/' +params.id+ '/');
}
else {return
axios.get (' ${local_host}/categorys/', params);
}
};
Page Error:
mock.js:8359 Get Http://127.0.0.1:8000//categorys/net::ERR_CONNECTION_REFUSED
Reason: The local server is not turned on, and continue to error after opening:
Failed to load http://127.0.0.1:8000//categorys/: No ' Access-control-allow-origin ' header is present on the requested Reso Urce. Origin ' http://localhost:8080 ' is therefore not allowed access. The response had HTTP status code 404.
Reason: Browser and server do not allow Cross-domain Workaround: Set server cross domain installation pip install django-cors-headers server settings.py Settings app
#跨域
' Corsheaders ',
settings.py
middleware = [
' Corsheaders.middleware.CorsMiddleware ',
...
]
Add under middleware of settings.py
Cors_origin_allow_all = True
Solve