emmm,继续刚毕设呀:
由于Client端是微信小程序,所以得提供接口:
先修改模板:
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', "django.template.context_processors.i18n", "django.template.context_processors.media", ], }, }, ]
这里有个坑就是 后面那个行因为版本不一样可能也会不一样!
引入rest_framework :
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # USE OWN PART 'tools', # USE RestFUL API 'rest_framework', 'dj_pagination' ]
对了,最后那行是分页用的,顺便配置log:
# USE LOG : LOGGING = { 'version': 1, 'disable_existing_loggers': True, 'formatters': { 'standard': { 'format': '%(asctime)s [%(threadName)s:%(thread)d] [%(name)s:%(lineno)d] [%(module)s:%(funcName)s] [%(levelname)s]- %(message)s'} # Log Format }, 'filters': { }, 'handlers': { 'mail_admins': { 'level': 'ERROR', 'class': 'django.utils.log.AdminEmailHandler', 'include_html': True, }, 'default': { 'level':'DEBUG', 'class':'logging.handlers.RotatingFileHandler', 'filename': 'log/all.log', # log Output file 'maxBytes': 1024*1024*5, # file MAX size 'backupCount': 5, # The file MAX saved 'formatter':'standard', # USE format ways? }, 'error': { 'level':'ERROR', 'class':'logging.handlers.RotatingFileHandler', 'filename': 'log/error.log', 'maxBytes':1024*1024*5, 'backupCount': 5, 'formatter':'standard', }, 'console':{ 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'standard' }, 'request_handler': { 'level':'DEBUG', 'class':'logging.handlers.RotatingFileHandler', 'filename': 'log/script.log', 'maxBytes': 1024*1024*5, 'backupCount': 5, 'formatter':'standard', }, 'scprits_handler': { 'level':'DEBUG', 'class':'logging.handlers.RotatingFileHandler', 'filename':'log/script.log', 'maxBytes': 1024*1024*5, 'backupCount': 5, 'formatter':'standard', } }, 'loggers': { 'django': { 'handlers': ['default', 'console'], 'level': 'DEBUG', 'propagate': False }, 'django.request': { 'handlers': ['request_handler'], 'level': 'DEBUG', 'propagate': False, }, 'scripts': { 'handlers': ['scprits_handler'], 'level': 'INFO', 'propagate': False }, 'py_code.tools.views': { 'handlers': ['default', 'error'], 'level': 'DEBUG', 'propagate': True } } } # Django 's Default passWord is pbkdf_sha256 # It is simple to use #REST SETTING JUST use by ADMIN REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',), 'PAGE_SIZE': 10 } #118N-China # I don't want use the CN # LANGUAGE_CODE = 'zh-hans' TIME_ZONE = 'Asia/Shanghai'
最后还加入了管理台的国际化,对了:分页配置
# QuerFix Page # PAGINATION_DEFAULT_PAGINATION = 10 #The num of every page # PAGINATION_DEFAULT_WINDOW = 2 # PAGINATION_DEFAULT_ORPHANS = 0 PAGINATION_INVALID_PAGE_RAISES_404 = True
创建序列化文件:
from django.contrib.auth.models import User, Group from rest_framework import serializers from tools.models import product class UserSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = User fields = ('url', 'username', 'email', 'groups') class GroupSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Group fields = ('url', 'name') # Product Serializer --> class ProductSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = product fields = ('id','no','name','comment','creatTime','updateTime')
其中 fields是返回的外露字段,而权限暂时做成这样
在View里面写:
# RESTFUL API -ALL # used to Mobile # And Only in Product #--------------------------- class ProuctsList(APIView): """ USE Products List Get """ def get(self,request): products = product.objects.all() serializer = ProductSerializer(products, many=True) return Response(serializer.data)
# User In class UserViewSet(viewsets.ModelViewSet): """ Check And Edit User's page """ queryset = User.objects.all().order_by('-date_joined') serializer_class = UserSerializer class GroupViewSet(viewsets.ModelViewSet): """ Check and Edit Gruop's View page """ queryset = Group.objects.all() serializer_class = GroupSerializer
配置url:
router = routers.DefaultRouter() router.register(r'users', views.UserViewSet) router.register(r'groups', views.GroupViewSet) urlpatterns = [ #RESTFUL API - System url(r'^', include(router.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), #ADMIN url(r'^admin/', admin.site.urls), #QR_CODE --index url(r'^product/qrcode/(.+)$', views.generate_qrcode, name='qrcode'), url(r'^product/index.html', views.index, name='index'), #RESTFUL API- USER PART url(r'^api-taozi/wechat/products-all',views.ProuctsList.as_view()), ]
这样就得到了带权限验证登入的api。暂时到这
求个链接可好?
@DDA 是项目的链接吗 这里:https://github.com/KelovpString/QR-Code_security_verification