diff --git a/snippets/urls.py b/snippets/urls.py index 2e73bc86448333633ef681a2153005cd2fe39c08..f9afe4433bb8a37750d4c2fa90e89c56577bab45 100644 --- a/snippets/urls.py +++ b/snippets/urls.py @@ -65,6 +65,7 @@ from snippets import views from django.conf.urls import url, include +from django.urls import path from rest_framework.routers import DefaultRouter from snippets import views diff --git a/snippets/views.py b/snippets/views.py index f3233919010685fff8d3719e93d6bb67e4a67012..dc5c12755216728b20200f00dcb7b89b129c74ac 100644 --- a/snippets/views.py +++ b/snippets/views.py @@ -24,7 +24,6 @@ def api_root(request, format=None): ) - # class SnippetList(generics.ListCreateAPIView): # queryset = Snippet.objects.all() # serializer_class = SnippetSerializer @@ -47,7 +46,7 @@ def api_root(request, format=None): # def get(self, request, *args, **kwargs): # snippet = self.get_object() # return Response(snippet.highlighted) - + # class UserList(generics.ListAPIView): # queryset = User.objects.all() # serializer_class = UserSerializer @@ -67,7 +66,6 @@ class UserViewSet(viewsets.ReadOnlyModelViewSet): lookup_field = 'username' - class SnippetViewSet(viewsets.ModelViewSet): """ 这个视图集自动提供 `list`,`create`,`retrieve`,`update`和`destroy`操作。 @@ -77,13 +75,13 @@ class SnippetViewSet(viewsets.ModelViewSet): queryset = Snippet.objects.all() serializer_class = SnippetSerializer permission_classes = ( - permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,) + permissions.IsAuthenticatedOrReadOnly, IsOwnerOrReadOnly,) # lookup_url_kwarg = 'code' # lookup_field = 'code' filter_backends = [DjangoFilterBackend, OrderingFilter, SearchFilter] filter_fields = ['code'] search_fields = ['code', 'owner__username'] - ordering_fields = ['owner', 'code'] # 这里写'url'会报错,报错会提示有哪些字段可以排序 + ordering_fields = ['owner', 'code'] # 这里写'url'会报错,报错会提示有哪些字段可以排序 # 使用ordering指定默认排序(不输入上面的指端指定排序则使用以下默认字段) ordering = ['code'] @@ -97,11 +95,13 @@ class SnippetViewSet(viewsets.ModelViewSet): def perform_create(self, serializer): serializer.save(owner=self.request.user) + class SnippetList(generics.ListAPIView): """ 此视图应返回当前已验证用户的所有 purchases 列表。 """ serializer_class = SnippetSerializer + def get_queryset(self): user = self.request.user return Snippet.objects.filter(owner=user) @@ -114,4 +114,3 @@ class SnippetList(generics.ListAPIView): def perform_create(self, serializer): serializer.save(owner=self.request.user) - diff --git a/tutorial/settings.py b/tutorial/settings.py deleted file mode 100644 index 63c6eb36cc15d8eb31ecd7fc36b119325e839f5b..0000000000000000000000000000000000000000 --- a/tutorial/settings.py +++ /dev/null @@ -1,154 +0,0 @@ -""" -Django settings for tutorial project. - -Generated by 'django-admin startproject' using Django 2.2.13. - -For more information on this file, see -https://docs.djangoproject.com/en/2.2/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/2.2/ref/settings/ -""" - -import os - -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) -BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'cae%!%j9d^9(u8*z#nzijn(rzpi*t#p8q$np^8)01auj_45m6%' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = [] - - -# Application definition - -INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', - 'rest_framework', - 'snippets', - 'django_filters' -] - -# 相关可产看:https://juejin.im/post/6844903869336518664#heading-70 -REST_FRAMEWORK = { - 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', - 'PAGE_SIZE': 10, - # 异常处理 - # 'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler', - 'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',), - 'DEFAULT_PERMISSION_CLASSES': ( - 'rest_framework.permissions.IsAuthenticated', # 授权访问 - ), - 'DEFAULT_AUTHENTICATION_CLASSES': ( - 'rest_framework.authentication.BasicAuthentication', # 基本表单认证 - 'rest_framework.authentication.SessionAuthentication', # session认证 - ), - 'DEFAULT_THROTTLE_CLASSES': ( - 'rest_framework.throttling.AnonRateThrottle', - 'rest_framework.throttling.UserRateThrottle', - # 限制用户对于每个视图的访问频次,使用ip或user id - # 'rest_framework.throttling.ScopedRateThrottle', - ), - 'DEFAULT_THROTTLE_RATES': { - 'anon': '4/day', # 匿名用户每天访问4次 - 'user': '100/day', # 登录用户访问100次 - # 这里是视图里使用的字段,如 throttle_scope = 'contacts' - # 'contacts': '1000/day', - # 'uploads': '20/day' - }, -} - - -MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', -] - -ROOT_URLCONF = 'tutorial.urls' - -TEMPLATES = [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - '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', - ], - }, - }, -] - -WSGI_APPLICATION = 'tutorial.wsgi.application' - - -# Database -# https://docs.djangoproject.com/en/2.2/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), - } -} - - -# Password validation -# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators - -AUTH_PASSWORD_VALIDATORS = [ - { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', - }, -] - - -# Internationalization -# https://docs.djangoproject.com/en/2.2/topics/i18n/ - -# TODO -LANGUAGE_CODE = 'zh-cn' - -TIME_ZONE = 'Asia/shanghai' - -USE_I18N = True - -USE_L10N = True - -USE_TZ = True - - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/2.2/howto/static-files/ - -STATIC_URL = '/static/'